-
Notifications
You must be signed in to change notification settings - Fork 297
Question: Is the Clone bound on allocator in A: Allocator + Clone really necessary? #309
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
I think it's probably possible to remove the bound, but this would require significant internal changes to the code. However I am curious about your allocator. Why can't it be |
This ended up being longer than I wanted, so tldr: an attempt to implement a safe memory arena as a self-referential struct. I am trying to do a pattern from C that is somewhat hard to represent in Rust. I have multiple bump allocators that either request chunks of memory from the platform as needed (pretty much the way Some of these allocators serve to allocate temporary structures for per-frame computation (yeah, this is a game dev project), but besides "clearly temporary data" that can be scoped to a The problem with keeping data around for multiple frames (or even passing it between multiple game systems) is lifetimes - any struct containing the data would have the lifetime of the borrow of the allocator, e.g An unrealistic, naive solution would be to have something like this: struct LevelDataArena {
allocator: Box<BumpAllocator>, // Boxed, because we don't want it moving, since things will be pointing to it
entities: Vec<Entity, &'self BumpAllocator>,
meshes: Vec<Mesh, &'self BumpAllocator>,
// ... more things ...
} This of course won't compile, and even if it did, we'd have to be careful about drop order, but the general idea is that resetting the allocator in the arena would require We can use My attempt was to only allow ... and this is how I discovered the |
Have you considered restructuring your code like this? As a bonus you don't need any unsafe code: // Outer loop, per level.
'level: loop {
// Per-level allocations
let level_bumpalo = Bump::new();
let mut level = Level {
alloc: &level_bumpalo,
entities: Vec::new_in(&level_bumpalo),
...
};
// Inner loop, per frame.
loop {
do_frame_stuff(&mut level);
// At the end of the frame, decide whether to flush all per-level
// allocations.
if next_level {
// Exit to the outer loop.
continue 'level;
}
}
} |
Oh, this is very elegant ❤️ Unfortunately, my situation is a bit more difficult, because of multiple platform support (desktops, consoles, web) and the way this affects architecting the main loop to be compatible with them all - the game code doesn't see the loop at all, it is just being called from the platform layer to do stuff1. The APIs on various platforms have various requirements on how you do the loop, e.g. in the browser the platform calls you once per-frame instead of you manually scheduling. If targeting Windows-only (and possibly other desktops, I am not very familiar there) and not worrying about portability, the approach you illustrated should indeed work, given some thought. Regarding the As for the unsafe API of the Arena as it exists now, I realized it is only unsafe to reset or drop the arena, not to leak the allocator. Because of this the default drop just leaks, and there's Footnotes
|
Hey :)
I am using the nightly
allocator_api
with a custom allocator. To guarantee safety in my setup, the allocator I have is notClone
.This worked with collections in
alloc
, but hashbrown's allocator needs to beClone
. I tracked this requirement down to a single function:RawTableInner::prepare_resize
, which has a couple of clients.Hashbrown's codebase is way above my paygrade, but the general question is: is this just a limitation of the current implementation, or will it never be possible to remove the bound? Naturally, the allocator needs to be
Clone
, if the collection wants to implementClone
, but does it need to beClone
otherwise?The text was updated successfully, but these errors were encountered: