Skip to content

Optimizing TSD and thread cache layout #701

Closed

Description

@interwq

Fast path tcache performance is critical as it's expected to be accessed very frequently. We aim to improve the fast path significantly with the following two changes:

  1. Embed tcache into TSD:
    Currently each tsd stores a pointer to the automatic tcache:
    https://github.com/jemalloc/jemalloc/blob/dev/include/jemalloc/internal/tsd_structs.h#L19
    This adds a memory dereference on the fast path, and potentially a cache miss. We can avoid this by embedding the tcache struct into tsd directly. Since we do not allow switching auto tcache, this should work fine w/o breaking APIs.

  2. Optimize tcache and tsd for a more compact layout. We'd like have higher density of the tcache bins to improve cache locality. Currently tbins are 32 bytes, which will be cut down to 24. Also pack TSD tighter.

Activity

  1. added this to the 5.0.0 milestone on Mar 14, 2017
  2. self-assigned this
    on Mar 20, 2017
  3. interwq commented on Mar 28, 2017

    @interwq
    ContributorAuthor

    @jasone : to embed tcache into tsd, my current plan is to split the tbins into two bins: small and large, basically:

    tcache_t {
    ....
    tbin_t small_bins[NBINS];
    tbin_t *large_bins; /Dynamically sized/
    }
    So that we embed small bins for better locality, while keep the ability to set lg_tcache_max, i.e. nhbins. Does that make sense to you? Or you think we should just get rid of lg_tcache_max and make the size static?

  4. jasone commented on Mar 28, 2017

    @jasone
    Member

    I think we should make large_bins have NSIZES - NBINS elements so that no dynamic sizing is necessary, and probably place tcache at the end of tsd so that the unused elements don't break up the utilized cachelines.

  5. interwq commented on Mar 28, 2017

    @interwq
    ContributorAuthor

    That sounds reasonable. For the unused large bins (binind > nhbins), probably not allocating their avail arrays?

  6. jasone commented on Mar 28, 2017

    @jasone
    Member

    Ah right, definitely avoid allocating more avail slots than actually used.

  7. interwq commented on Apr 6, 2017

    @interwq
    ContributorAuthor

    The first half of this (embedding tcache into tsd) is implemented in #736. Tested with @davidtgoldblatt using test/stress/microbench, which shows a ~15% improvement on malloc / free / etc. Likely this made the fast path more pipeline friendly. A sample output w/ --enable-prof:
    With #736:

            100000000 iterations, malloc=2189009us, mallocx=2275009us, ratio=1:0.962
            test_malloc_vs_mallocx: pass
            100000000 iterations, free=2192009us, dallocx=2223009us, ratio=1:0.986
            test_free_vs_dallocx: pass
            100000000 iterations, dallocx=2190009us, sdallocx=2326009us, ratio=1:0.942
            test_dallocx_vs_sdallocx: pass
            100000000 iterations, malloc_usable_size=2679011us, sallocx=2683011us, ratio=1:0.999
            test_mus_vs_sallocx: pass
            100000000 iterations, sallocx=2710011us, nallocx=2636011us, ratio=1:1.028
            test_sallocx_vs_nallocx: pass
            --- pass: 5/5, skip: 0/5, fail: 0/5 ---
    

    Before (d4e98bc):

            100000000 iterations, malloc=2540010us, mallocx=2704011us, ratio=1:0.939
            test_malloc_vs_mallocx: pass
            100000000 iterations, free=2568010us, dallocx=2546010us, ratio=1:1.009
            test_free_vs_dallocx: pass
            100000000 iterations, dallocx=2528010us, sdallocx=2486010us, ratio=1:1.017
            test_dallocx_vs_sdallocx: pass
            100000000 iterations, malloc_usable_size=3018012us, sallocx=3235013us, ratio=1:0.933
            test_mus_vs_sallocx: pass
            100000000 iterations, sallocx=3254013us, nallocx=3004012us, ratio=1:1.083
            test_sallocx_vs_nallocx: pass
    

    I'll start working on splitting tcache into tcache_fast and tcache slow, to be placed at the beginning and end of tsd respectively. Also make tbin layout more compact.

  8. changed the title [-]Embed tcache struct into tsd[/-] [+]Optimizing TSD and thread cache layout[/+] on Apr 7, 2017
  9. interwq commented on Apr 7, 2017

    @interwq
    ContributorAuthor

    Closed via #736 and #752.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions