Stream: t-libs/wg-allocators

Topic: Crate wide allocators

Alexander Droste (Sep 23 2019 at 12:36, on Zulip):

Hey all!

First of all, I'd like to say that I'm really excited about the fact that there's a working group exploring how custom allocator support could be extended for Rust!

After giving the #[global_allocator] concept a try, I was wondering if there was also the idea of having crate wide allocators; so having something in-between fully global or per container.

As I'm working with real-time audio it'd be nice being able to scope custom allocators at a crate level since constraints around allocations are consistent within those crates.

On that note, if there's interest or even ongoing process in that area, I'm happy to help!

gnzlbg (Sep 23 2019 at 16:26, on Zulip):

I'm not sure how crate-wide allocators should or even could work.

gnzlbg (Sep 23 2019 at 16:26, on Zulip):

You can allocate something in a crate, and deallocate it on a different crate

gnzlbg (Sep 23 2019 at 16:26, on Zulip):

Yet which crate allocated an object is not stored anywhere

gnzlbg (Sep 23 2019 at 16:27, on Zulip):

e.g. pub fn foo() -> Vec<i32>; which can be called from other crates, the Vec<i32> type does not say which crate allocated the Vec, so... how would they know which allocator to use to free it ?

Alexander Droste (Sep 25 2019 at 08:39, on Zulip):

Thanks for giving this some thought. And yeah, makes sense. I guess, I was thinking about it in terms of combining distinct static libs (.a), assuming it would work having separate allocators in that scenario.

gnzlbg (Sep 25 2019 at 12:23, on Zulip):

With custom allocators, you can just use type MyBox<T> = Box<T, MyAlloc>; in your crate.

gnzlbg (Sep 25 2019 at 12:24, on Zulip):

That makes the allocator part of the type and your types know how to deallocate themselves

gnzlbg (Sep 25 2019 at 12:25, on Zulip):


Alexander Droste (Sep 26 2019 at 18:37, on Zulip):

That's great! But looking at the issues from the wg-allocators repo, there's no way yet to use custom allocators for Box or containers from std::collection, right?

Sydney Acksman (Sep 27 2019 at 16:23, on Zulip):

Not yet, I believe we're waiting on stability attributes for type parameters atm

Alexander Droste (Sep 27 2019 at 18:09, on Zulip):

I see, thanks for clarifying.

Tim Diekmann (Oct 05 2019 at 21:15, on Zulip):

Not yet, I believe we're waiting on stability attributes for type parameters atm

Can confirm, this is the first step we have to take before landing anything upstream.
This is the issue in the WG repo:
And the WIP PR:

Last update: Nov 15 2019 at 10:10UTC