Skip to content

WebAssembly-only fast allocator#13513

Merged
andrewrk merged 8 commits intomasterfrom
faster-wasm-gpa
Nov 30, 2022
Merged

WebAssembly-only fast allocator#13513
andrewrk merged 8 commits intomasterfrom
faster-wasm-gpa

Conversation

@andrewrk
Copy link
Member

@andrewrk andrewrk commented Nov 11, 2022

This is a 160-line Allocator implementation that is simpler, faster, and more memory-efficient than GeneralPurposeAllocator. It similarly buckets allocations into size classes, however, unlike GPA, it additionally uses free lists, and does not track any metadata. The only extra information stored within each allocation is an intrusive free list node, so that the allocation can be added to the free list without possibility of failure. This allocator has zero external fragmentation, but a high amount of internal fragmentation. Large allocations are never split into multiple smaller allocations, and smaller allocations are never joined into a single larger allocation.

It is WebAssembly-only because it depends on single-threaded access to global variables to keep track of allocator state, it directly calls the @wasmMemoryGrow intrinsic, and it never gives memory back to the OS.

I think this strategy could be appropriate in general when the following options are enabled:

  • -fsingle-threaded
  • -OReleaseSmall
  • The OS has an API equivalent to sbrk for obtaining heap memory.

@andrewrk andrewrk changed the title experimental WebAssembly-only fast allocator WebAssembly-only fast allocator Nov 30, 2022
fast allocator for WebAssembly

eventually this is intended to be merged into
`std.heap.GeneralPurposeAllocator`
The previous version had a fatal flaw: it did ensureCapacity(1) on the
freelist when allocating, but I neglected to consider that you could
free() twice in a row. Silly!

This strategy allocates an intrusive freelist node with every
allocation, big or small. It also does not have the problems with resize
because in this case we can push the upper areas of freed stuff into the
corresponding freelist.
Now it can refuse to resize when it would disturb the metadata tracking
strategy, resulting in smaller code size, a simpler implementation, and
less fragmentation.
@andrewrk andrewrk merged commit 71038c4 into master Nov 30, 2022
@andrewrk andrewrk deleted the faster-wasm-gpa branch November 30, 2022 06:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant