-
Notifications
You must be signed in to change notification settings - Fork 10
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
Proof Of Concept Crate: Simplistic Bump Allocator #71
Comments
Most existing bump allocators use lifetimes to ensure everything is deallocated when they reset. |
That might be fine, but clogs up the outer types of everything if you use lifetimes, so it might also be very unwanted. There is space for both styles probably. |
For lifetime-style bumpalo looks nice |
Sure. lifetimes are going to infect lots of functions, but you're going to add allocator reference in arguments anyway wherever allocation will happen. And whatever function borrows allocated data will have lifetime too. |
Yes, I was expecting almost exclusively that you'd be getting owned allocation out of the allocator and then dropping them all at the end of a loop. Also that you could potentially put it on a global static and then never need to pass it around. Or a thread-local, depending on if lots of threads will do lots of stuff. |
We've implemented this for usage in amethyst and rendy related projects. https://github.com/jaynus/purple The entire concept revolves around some user behavior constraints to protect against UB, which we dont handle. Let me know if this is of any interest to get up to snuff on the safety side. We currently use it for some limited cases in amethyst and rendy, with some known limitations and factors around that. Collections type are a bit lacking at the moment as well, but that is, again, because it is geared towards being a very strict game-oriented bump allocator, not a generic one (such as bumpalo) |
@Ericson2314 @TimDiekmann I am just taking our discussion from issue #70 here. You mentioned that this code segment might solve my problem: impl<ChunkAlloc> AllocRef for Bump<ChunkAlloc> where ChunkAlloc: AllocRef {}
impl<ChunkAlloc> AllocRef for &Bump<&ChunkAlloc> where &ChunkAlloc: AllocRef {} but as far as I can tell this doesn't affect my use case at all. I am adding the following function to my pub fn alloc_t<T>(&self, val: T) -> Result<&mut T, <&Self as AllocRef>::Error> {
assert!(mem::size_of::<T>() > 0);
unsafe {
let layout = NonZeroLayout::new_unchecked::<T>();
let ptr = self.alloc(layout)?;
let ptr = ptr.cast::<T>().as_ptr();
ptr::write(ptr, val);
Ok(&mut *ptr)
}
} The |
@TimDiekmann, I extended @95th's implementation of bumpalo by generalizing the bump allocator over a Some general feedback:
Feel free to give feedback on the design/implementation. |
Thanks for the feedback @Wodann! I'll look into your commit as soon as I have a bit more free time 😄
The
Nice to hear! With this it should also be easier to interface FFI.
Can't say, if I like this or not. On the one hand, internal mutability is pretty flexible and a solution to this. On the other hand it always introduces an overhead, especially with multithreading. |
@Wodann Please let me know if the use case for |
Having read that, it makes a lot more sense @scottjmaddox for the use cases you mention. @TimDiekmann Did you have time to take a look at my use case? The Rust All-Hands 2020 is coming up in March and if possible I'd like your alloc-wg crate integration into nightly to be one of the topics brought up there as beneficial to gamedev. |
@Wodann with your use case, I guess you mean your bump allocator and TimDiekmann/alloc-wg#11? Yes, I have tested around with it. I think, the problem isn't the mutable What do you think? |
@TimDiekmann I tried you new API design. (see commit) I like that this API allows the implementer to designate whether they are passing by value, shared reference, or mutable reference. Is there are reason why the unsafe fn build_alloc_ref(
&mut self,
_ptr: NonNull<u8>,
_layout: Option<NonZeroLayout>,
) -> Self::Ref {
self
} fn get_build_alloc(&mut self) -> Self::BuildAlloc {
self
} |
Nice to hear! I have posted a proposal to wg-allocators.
|
Many groups want a simple "bump allocator" which can be used for per-frame data.
This should be sufficiently simple to setup and also maintain safety.
reset
on the allocator to reset the allocator to the start of the data pile.reset
is non-zero, it panics.What we need is a proof of concept crate so that we can test it out, find the problems, iterate design, etc.
The text was updated successfully, but these errors were encountered: