Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small boxes #35

Closed
burdges opened this issue Jan 3, 2020 · 2 comments
Closed

Small boxes #35

burdges opened this issue Jan 3, 2020 · 2 comments

Comments

@burdges
Copy link

burdges commented Jan 3, 2020

I noticed in rust-lang/rust#48331 (comment) 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
Copy link

gnzlbg commented Jan 3, 2020

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
Copy link
Author

burdges commented Jan 4, 2020

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.

@burdges burdges closed this as completed Jan 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants