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
{{ message }}
Repository navigation
Rust stdlib growth strategies and mimalloc realloc fight each other #1488
Standard containers in the stdlib (Vec) grow by doubling this isn't spec'd but is apparently unlikely to ever change (ref)
It is common to grow a buffer by appending and then 'finalize' it via a call to shrink_to_fit or into_boxed_slice, these both call GlobalAlloc::realloc which is bound (typically) to mi_realloc_aligned
In this scenario (growing a rust vec and then finalizing) you are guaranteed to hit this early return in mimalloc since waste will always be <50%.
I'm not sure what the right solution is (perhaps none), but in turbopack we are working around by intercepting realloc before it hits mimalloc to copy allocations when they change mimalloc size classes. See vercel/next.js#101395 which will save us a lot of memory in practice due to some large string allocations.
Obviously we should be more accurately presizing containers and we are pursuing that, but i was surprised to find that our basic finalization strategies were ineffective on mimalloc .
tl;dr; rust grows vecs by 2x and mimalloc only shrinks allocations in realloc if it would save more than half the space, so these two strategies defeat each other
request: consider a configurable shrinking policy?
Thanks Luke Sandberg (@lukesandberg) ! Just to clarify, the issue is not about performance, but more that we often waste a lot of memory ? Since after a doubling one may use just a little bit more memory but then it never actually shrinks it -- so using up to 50% too much. Is that right?
So one way might be to tighten the limit to not waste more than 12.5% (as normal in mimalloc), and copy otherwise. It seems your current workaround does something like this? (I guess, your current strategy calculates the bin size for the resize and only shrinks if the block stays in the same size class? (that would correspond to about 12.5% max waste). Thanks
Activity
lukesandberg commentedon Nov 10, 2026
I honestly don't know what a good solution to this problem is, perhaps our workaround is the best, but i wanted to flag it
daanx commentedon Nov 10, 2026
Thanks Luke Sandberg (@lukesandberg) ! Just to clarify, the issue is not about performance, but more that we often waste a lot of memory ? Since after a doubling one may use just a little bit more memory but then it never actually shrinks it -- so using up to 50% too much. Is that right?
So one way might be to tighten the limit to not waste more than 12.5% (as normal in mimalloc), and copy otherwise. It seems your current workaround does something like this? (I guess, your current strategy calculates the bin size for the resize and only shrinks if the block stays in the same size class? (that would correspond to about 12.5% max waste). Thanks
tighten shrink limit to 12.5% (from 50%), see issue #1488
for huge pages, on a realloc with a smaller size, try to avoid a copy…
fix realloc-shrink-huge test to avoid a glibc fortify abort when wri…
lukesandberg commentedon Nov 10, 2026
Yeah exactly. Not perf but heap.
A 12.5% heuristic would seem to solve this for us.
daanx commentedon Nov 10, 2026
Thank you for the report; I think the latest dev3 fixes the issue. (probably there will be a new versioned release later this week)
lukesandberg commentedon Nov 10, 2026
Amazing! Thank you so much 👍
I am looking into switching us to the recently added rust bindings in this repo so we can track your releases closer.
turbo-tasks-malloc: bump mimalloc to v3.7.0 (#102029)
Vendor Microsoft mimalloc 3.7.0