You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
jemalloc has had a lock-free radix tree for tracking chunks since 4.0.0. It would be possible to adapt the radix tree to track pages rather than chunks, and look up metadata via the radix tree rather than via pointer bit masking to compute chunk header addresses. That would allow metadata to be stored independently of application data, and the entire concept of chunks could go away.
Potential advantages:
Reduction of metadata overhead, since sparsely utilized chunks currently cause disproportionately high metadata overhead, and chunk headers have to be large enough for the worst case (bitmaps for small region runs).
Laxer alignment interacts better with ASLR.
Enables cache-oblivious layout (aka cache index randomization) for huge size classes.
Potential disadvantages:
Radix tree lookups aren't quite as fast as the chunk-related bit masking/shifting lookups, so other optimizations would have to compensate.
API churn. Chunk size would go away as a concept separate from page size, but chunks would remain e.g. as part of the chunk hook API, so a lot of rototilling would result.
If the performance degradation is too pronounced, would it be possible to create a configure option to use one versus the other? This would allow the choice between a slightly better aslr, and pure performance where that doesn't matter.
Yes, it may be possible to conditionally enable radix tree lookups for chunks if we don't do away with chunks completely. This would also enable cache index randomization for huge size classes.
I'm maybe as much as halfway though implementing this right now on a private branch, but it's a huge amount of work, so completion may still take quite a while.
Random notes:
Large and huge size classes will have the same underlying implementation, based on extents.
Although we won't need naturally aligned chunks anymore, it will still be important to map large contiguous regions so that extents can coalesce within each arena. It becomes feasible to create progressively larger mappings as memory usage increases, so chunk size will probably lose its relevance as a tuning parameter.
The radix tree leaf elements each incorporate acquire/release capabilities to resolve radix tree update races. This imposes no synchronization overhead on fast-path reads, e.g. to look up the metadata for an extant allocation, but it does require care during writes to use consistent lock ordering protocols.
All uses of sorted (red-black) trees will become (pairing) heaps, since address-based search will be supported directly by the radix tree. The heaps may end up as the last remaining concurrency bottleneck for heavily utilized arenas, but regardless this transition will strictly be an improvement, and we can consider heap concurrency as a future problem that'll be good to have.
I'm still not sure how this project is going to impact overall performance, but my intuition is that it will marginally slow down small [de]allocation due to the slightly higher metadata lookup overhead, but dramatically speed up large/huge [de]allocation. I suspect the slowdown will only manifest in the tightest of microbenchmarks (e.g. I measured a slowdown from ~49 cycles to ~63 cycles for p=malloc(1) followed by a NULL check and free(p), but some of that loss will be regained once all the data structure changes are in place). Overall I'm cautiously optimistic that the overall result will be positive.
Status update: I've been working pretty constantly on this for the past couple of weeks, currently with git diff dev |wc -l → 11008 (~75% of which is preparatory refactoring/optimization), and perhaps another week of work before I can start running meaningful performance measurements. No show-stopping issues have arisen, so the only concerns I have are related to virtual memory fragmentation and fast path speed. Meanwhile some other work items are demanding my attention, so I may have to put this aside for a week or two.
Is it possible to push cache-index randomization up to this metadata level, vs. the current large allocation level? I had attempted this at one point - chunk metadata contained the cache-index offset, which was added to every allocated pointer. This way wastes only a single page per chunk vs. one per large allocation
Re: cache index randomization, I think you're suggesting basically the same thing I have in mind. Large allocation will be subsumed by huge allocation (though huge will probably renamed to "large"), and cache index offset will be stored in extent metadata.
Status update: I'm ~30 steps into the refactor, all but the last of which currently pass the test suite. Given the massive changes involved, there will likely be a lot of instability issues to work out over time, but I'm planning to have this work coherent enough to commit to the dev branch within a week. Hopefully it will prove viable; the internal APIs are much easier to work with, and relative to 4.2.0 it is a net code reduction of ~3 kloc and a library size reduction of ~25%.
As of 0c5cec8 this is basically done, though there are likely regressions lurking, and a lot of optimization opportunities, especially around synchronization protocols. Huge size classes are no longer distinct from large size classes; they are both directly backed by extents. Extents are tracked via a combination of the global radix tree and per size class pairing heaps.
Radix trees now have a lookup path cache (6f29a83), so the fast path performance difference versus the old chunk-based metadata lookup is only ~3 cycles per allocator invocation. In the long run we may be able to further optimize this by more explicitly specializing on x64 to retain the benefit of skipping the first level of the radix tree during lookups, without paying the current logic cost of assuring the cached path is compatible with the current lookup.
Here are the remaining issues I'm currently aware of. I'll probably break these out into separate issues at some point:
There are still a few vestiges of chunks remaining, but I don't want to remove them until ratio-based purging is gone (Remove ratio-based purging #385).
We still need a mechanism for creating progressively larger retained extents from which to carve allocations, so that arenas are operating on mostly contiguous virtual memory.
Arenas should probably have independent base allocators, so that their metadata are contiguous.
Base allocators should explicitly use huge pages if available.
The layout/encoding of extent_t could use some optimization.
FWIW, I'm somewhat shocked that the dev branch works at all in the context of Firefox, due to the amount of rototilling involved. ;-)
We still have several known performance issues to address here, so I'm going to keep despair in check until we run out of resolvable potential explanations.
Activity
cferris1000 commentedon Mar 23, 2016
If the performance degradation is too pronounced, would it be possible to create a configure option to use one versus the other? This would allow the choice between a slightly better aslr, and pure performance where that doesn't matter.
jasone commentedon Mar 23, 2016
Yes, it may be possible to conditionally enable radix tree lookups for chunks if we don't do away with chunks completely. This would also enable cache index randomization for huge size classes.
jasone commentedon Mar 28, 2016
I'm maybe as much as halfway though implementing this right now on a private branch, but it's a huge amount of work, so completion may still take quite a while.
Random notes:
p=malloc(1)followed by aNULLcheck andfree(p), but some of that loss will be regained once all the data structure changes are in place). Overall I'm cautiously optimistic that the overall result will be positive.jasone commentedon Apr 10, 2016
Status update: I've been working pretty constantly on this for the past couple of weeks, currently with
git diff dev |wc -l→ 11008 (~75% of which is preparatory refactoring/optimization), and perhaps another week of work before I can start running meaningful performance measurements. No show-stopping issues have arisen, so the only concerns I have are related to virtual memory fragmentation and fast path speed. Meanwhile some other work items are demanding my attention, so I may have to put this aside for a week or two.djwatson commentedon May 5, 2016
jasone commentedon May 6, 2016
Re: cache index randomization, I think you're suggesting basically the same thing I have in mind. Large allocation will be subsumed by huge allocation (though huge will probably renamed to "large"), and cache index offset will be stored in extent metadata.
jasone commentedon May 31, 2016
Status update: I'm ~30 steps into the refactor, all but the last of which currently pass the test suite. Given the massive changes involved, there will likely be a lot of instability issues to work out over time, but I'm planning to have this work coherent enough to commit to the dev branch within a week. Hopefully it will prove viable; the internal APIs are much easier to work with, and relative to 4.2.0 it is a net code reduction of ~3 kloc and a library size reduction of ~25%.
jasone commentedon Jun 6, 2016
As of 0c5cec8 this is basically done, though there are likely regressions lurking, and a lot of optimization opportunities, especially around synchronization protocols. Huge size classes are no longer distinct from large size classes; they are both directly backed by extents. Extents are tracked via a combination of the global radix tree and per size class pairing heaps.
Radix trees now have a lookup path cache (6f29a83), so the fast path performance difference versus the old chunk-based metadata lookup is only ~3 cycles per allocator invocation. In the long run we may be able to further optimize this by more explicitly specializing on x64 to retain the benefit of skipping the first level of the radix tree during lookups, without paying the current logic cost of assuring the cached path is compatible with the current lookup.
Here are the remaining issues I'm currently aware of. I'll probably break these out into separate issues at some point:
extent_tcould use some optimization.glandium commentedon Jun 9, 2016
Preliminary testing on Firefox suggests this is significantly slower than 4.2.1, and might have some issues about not releasing memory properly.
jasone commentedon Jul 7, 2016
FWIW, I'm somewhat shocked that the dev branch works at all in the context of Firefox, due to the amount of rototilling involved. ;-)
We still have several known performance issues to address here, so I'm going to keep despair in check until we run out of resolvable potential explanations.
jasone commentedon Oct 4, 2016
All remaining work is now tracked in separately created issues. Closing.