Skip to content

Bucket allocations by size in the free list#12987

Open
fitzgen wants to merge 1 commit intobytecodealliance:mainfrom
fitzgen:no-quadratic-behavior-for-n-allocations
Open

Bucket allocations by size in the free list#12987
fitzgen wants to merge 1 commit intobytecodealliance:mainfrom
fitzgen:no-quadratic-behavior-for-n-allocations

Conversation

@fitzgen
Copy link
Copy Markdown
Member

@fitzgen fitzgen commented Apr 7, 2026

This allows us to avoid O(n) search for a block of a certain size in the worst case.

Follow up to #12969

This allows us to avoid `O(n)` search for a block of a certain size in the worst
case.
@fitzgen fitzgen requested a review from a team as a code owner April 7, 2026 20:31
@fitzgen fitzgen requested review from alexcrichton and removed request for a team April 7, 2026 20:31
Copy link
Copy Markdown
Member

@alexcrichton alexcrichton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How worth it do you feel chasing improvements in allocation/deallocation performance are? My impression is that BTreeMap is quite large in terms of code size, pretty wasteful in terms of resources, and not really all that efficient. I think it's quite good at its job of reducing things to O(log n) algorithmic complexity but my impression is that the constant factors are quite high for micro-algorithms like a malloc and free which this more-or-less is.

Mostly in the sense that I continue to be worried that we can layer on more and more Rust data structures to make malloc and free faster but I'm worried about unintended consequences about how these all interplay with each other. For example if there were a lot of 4-byte gaps in the heap it might end up meaning that the BTreeMap to represent the free list was larger than the heap itself (or something like that). With this there's multiple layers of BTreeMap to consider and personally I inevitably get lost in the details.

All that being said complexity can still be justified and it's not like I know of anything particularly wrong with this approach. Mostly I wanted to at least ask though to get your temperature on this axis too

@fitzgen
Copy link
Copy Markdown
Member Author

fitzgen commented Apr 7, 2026

How worth it do you feel chasing improvements in allocation/deallocation performance are?

This PR is just to address your comment here, I have no intention to continue pushing on free list performance from this point on. My hope is that between #12969 and this PR, we will have gotten it to the point where performance of the free list itself is acceptable enough for enabling GC by default.

@fitzgen
Copy link
Copy Markdown
Member Author

fitzgen commented Apr 7, 2026

My impression is that BTreeMap is quite large in terms of code size, pretty wasteful in terms of resources, and not really all that efficient. I think it's quite good at its job of reducing things to O(log n) algorithmic complexity but my impression is that the constant factors are quite high for micro-algorithms like a malloc and free which this more-or-less is.

Mostly in the sense that I continue to be worried that we can layer on more and more Rust data structures to make malloc and free faster but I'm worried about unintended consequences about how these all interplay with each other. For example if there were a lot of 4-byte gaps in the heap it might end up meaning that the BTreeMap to represent the free list was larger than the heap itself (or something like that). With this there's multiple layers of BTreeMap to consider and personally I inevitably get lost in the details.

This is all true, but note that the fast path is just bump allocation which doesn't touch the btrees; we only go to them when bump allocation fails.

Ultimately, I think there is definitely room for improvement here as far as allocators go, but this should get us to a decent state and then the self-hosting would be the next step for this code.

@github-actions github-actions bot added wasmtime:api Related to the API of the `wasmtime` crate itself wasmtime:ref-types Issues related to reference types and GC in Wasmtime labels Apr 8, 2026
@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 8, 2026

Subscribe to Label Action

cc @fitzgen

Details This issue or pull request has been labeled: "wasmtime:api", "wasmtime:ref-types"

Thus the following users have been cc'd because of the following labels:

  • fitzgen: wasmtime:ref-types

To subscribe or unsubscribe from this label, edit the .github/subscribe-to-label.json configuration file.

Learn more.

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

Labels

wasmtime:api Related to the API of the `wasmtime` crate itself wasmtime:ref-types Issues related to reference types and GC in Wasmtime

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants