Refactor representation of thread local state. (#751)

* Lift checking for init to ThreadAlloc

The check init code was tightly integrated into LocalAllocator.  This commit pull that code out into ThreadAlloc, and passes a template parameter into the remaining LocalAllocator to perform the relevant TLS manipulations.  This removes some of the awkward layering around register_clean_up.

* Reduce size of test due to failures.

Fully disable lotsofthreads test

Need to investigate if the test is unreliable, or we have actually
regressed perf.  A quick mimalloc-bench didn't show any regressions.

* Simplify message queue initialisation

This introduces one additional branch on when processing a batch of messages, but it is likely to only be hit when a lot of messages are processed.

* Patch Domestication test.

* Refactor CoreAlloc/LocalAlloc

This combines the notion of CoreAlloc, LocalAlloc and LocalCache into a single class.  Previously, these were separated so that a more complex structure would be stored directly in the TLS.  This however, proved to be bad for compatibility if the allocator is part of the libc implementation.

This commit collapses all the stages of the allocator into a single class. This simplifies the sequencing and overall is a nice reduction in complexity.

* Re-enable lots of threads test.

* Reenable concept using alternative lazy checking for concepts.

* Self code review
This commit is contained in:
Matthew Parkinson
2025-03-21 15:13:32 +00:00
committed by GitHub
parent 3348bf9e56
commit dff1057db2
27 changed files with 1367 additions and 1562 deletions

View File

@@ -16,10 +16,10 @@ For simplicity, we assume that
Since this is the first allocation, all the internal caches will be empty, and so we will hit all the slow paths.
For simplicity, we gloss over much of the "lazy initialization" that would actually be implied by a first allocation.
1. The `LocalAlloc::small_alloc` finds that it cannot satisfy the request because its `LocalCache` lacks a free list for this size class.
The request is delegated, unchanged, to `CoreAllocator::small_alloc`.
1. The `Allocator::small_alloc` finds that it cannot satisfy the request because its lacks a fast free list for this size class.
The request is delegated, unchanged, to `Allocator::small_refill`.
2. The `CoreAllocator` has no active slab for this sizeclass, so `CoreAllocator::small_alloc_slow` delegates to `BackendAllocator::alloc_chunk`.
2. The `Allocator` has no active slab for this sizeclass, so `Allocator::small_refill_slow` delegates to `BackendAllocator::alloc_chunk`.
At this point, the allocation request is enlarged to one or a few chunks (a small counting number multiple of `MIN_CHUNK_SIZE`, which is typically 16KiB); see `sizeclass_to_slab_size`.
3. `BackendAllocator::alloc_chunk` at this point splits the allocation request in two, allocating both the chunk's metadata structure (of size `PAGEMAP_METADATA_STRUCT_SIZE`) and the chunk itself (a multiple of `MIN_CHUNK_SIZE`).