Skip to content

Rust stdlib growth strategies and mimalloc realloc fight each other #1488

Closed

Description

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?

Activity

  1. lukesandberg commented on Nov 10, 2026

    @lukesandberg
    Author

    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

  2. daanx commented on Nov 10, 2026

    @daanx
    Collaborator

    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

  3. lukesandberg commented on Nov 10, 2026

    @lukesandberg
    Author

    Yeah exactly. Not perf but heap.

    A 12.5% heuristic would seem to solve this for us.

  4. daanx commented on Nov 10, 2026

    @daanx
    Collaborator

    Thank you for the report; I think the latest dev3 fixes the issue. (probably there will be a new versioned release later this week)

  5. lukesandberg commented on Nov 10, 2026

    @lukesandberg
    Author

    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.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions