rust-lang / wg-allocators

Home of the Allocators working group: Paving a path for a standard set of allocator traits to be used in collections!
http://bit.ly/hello-wg-allocators
209 stars 9 forks source link

Small boxes #35

Closed burdges closed 4 years ago

burdges commented 4 years ago

I noticed in https://github.com/rust-lang/rust/issues/48331#issuecomment-570436716 that types smaller than usize could often be "boxed" without any allocation. There are some array crates like Bytes that provide such functionality, but doing this with Box mostly requires the vtable accommodate. I suspect doing this could dramatically improve our error handling story for crates that work both with and without std.

gnzlbg commented 4 years ago

Can you explain how this works? AFAICT, Box provides a stable address, so you are able to do Box::into_raw() to obtain a pointer to the address, that you can Copy into multiple pointers to the address, and then write through one pointer while reading through the others as long as you use a synchronization primitive like a Mutex to avoid data races. I don't immediately see how this could work without the Box actually performing a memory allocation using an stable allocator (like Heap).

burdges commented 4 years ago

I suppose some inherent methods like Box::into_raw might break this, not sure, thanks. Also this only works for Copy types, which I neglected before.

As to how it'd work, all call sites that know the exact type must know that because it's smaller than a usize then they should expect a usize. In particular any &dyn Trait for the type needs a vtable that expects the value to be embedded in the pointer.

In essence the rule becomes: optimize an immutable or owning refernece to a Copy type smaller than uszie as the value inside the usize.

I gave an example of doing this for just the std::error::Error trait in that link above, which should permit that trait working no_std, but I noticed it works much more generally.

Anyways maybe this goes against Rust's explicitness goals and it should be some other SmallBox type. It could maybe be constructed with proc macros even.