-
Notifications
You must be signed in to change notification settings - Fork 20
Safe aligning of MaybeUninit
mutable slices
#564
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
Comments
Oh, the motivating reason for having this is that it's safe? So you'd use it by |
@scottmcm I apologize for the possibly insufficiently detailed description, but if you look into my gist you can see that My main motivation is possibility of implementing parallel algorithms like on GPU that use some memory and then flush the entire buffer before reusing it again. |
Yes, to show people who is implementing arenas that this operations is actually safe. So, the most arenas can be implemented in safe Rust.
Yes, just holding initialized after |
I do think it would be pretty neat to not need any |
@hanna-kruppe I'm trying to implement as optimal as possible scoped bump allocator with the following interface: pub struct BumpAllocator<'scope> { .. }
impl<'scope> BumpAllocator<'scope> {
pub fn new(memory: &'scope mut [MaybeUninit<u8>]) -> Self { .. }
pub fn try_alloc_uninit<T>(&mut self) -> Option<&'scope mut MaybeUninit<T>> { .. }
} Here is godbolt comparison of the best I found with new suggested method vs It seems we need something like impl<T> [MaybeUninit<T>] {
pub fn split_last_aligned<U>(&mut self) -> (&mut MaybeUninit<U>, &mut Self) { .. }
} to reach such codegen. |
I made it a bit better by using let end = self.memory.as_ptr_range().end;
end.wrapping_sub((end as usize) & (align_of::<T>() - 1)) is correctly aligned pointer. Maybe we need some operation on pointers like |
I am not overly worried about bumping up vs down (it’s not like one is always clearly better than the other). Being able to express it at all in safe code, with decent performance, would already be great. Squeezing out the absolute maximum of performance is a legitimate use case for carefully written |
Btw, with new pub struct BumpAllocator<'scope> {
memory: &'scope mut [MaybeUninit<u8>],
}
impl<'scope> BumpAllocator<'scope> {
pub fn new(memory: &'scope mut [MaybeUninit<u8>]) -> Self {
Self { memory }
}
pub fn try_alloc_uninit<T>(&mut self) -> Option<&'scope mut MaybeUninit<T>> {
let first_end = self.memory.as_ptr().align_offset(align_of::<T>()) + size_of::<T>();
let prefix = self.memory.split_off_mut(..first_end)?;
Some(&mut prefix.align_to_uninit_mut::<T>().1[0])
}
} |
We discussed this proposal in the most recent standard library API team meeting and are in favor of it, exactly as proposed. Please open a library tracking issue in rust-lang/rust and send the implementation! Thank you. |
…k-Simulacrum Add `slice::align_to_uninit_mut` Add new `slice::align_to_uninit_mut` method. Tracking issue: rust-lang#139062 ACP: rust-lang/libs-team#564
Rollup merge of rust-lang#139072 - nickkuk:align_to_uninit_mut, r=Mark-Simulacrum Add `slice::align_to_uninit_mut` Add new `slice::align_to_uninit_mut` method. Tracking issue: rust-lang#139062 ACP: rust-lang/libs-team#564
…k-Simulacrum Add `slice::align_to_uninit_mut` Add new `slice::align_to_uninit_mut` method. Tracking issue: rust-lang#139062 ACP: rust-lang/libs-team#564
Proposal
Problem statement
Add new method for slice:
Motivating examples or use cases
This method originated from a discussion in users forum: https://users.rust-lang.org/t/typed-scoped-self-referential-arena-in-safe-rust/127155.
It allows to implement scoped multithread typed or untyped arena that allows self-references in safe Rust: gist.
Solution sketch
Alternatives
I don't know of any so far.
Links and related work
Look at motivating examples.
What happens now?
This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.
Possible responses
The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):
Second, if there's a concrete solution:
The text was updated successfully, but these errors were encountered: