4 ms·
For those who are interested, I think that arena allocation is an underrated approach to managing lifetimes of interconnected objects that works well with borro
by taylorallred 1y ago
For those who are interested, I think that arena allocation is an underrated approach to managing lifetimes of interconnected objects that works well with borrow checking.
- worik 1y ago> works well with borrow checking. Yes, because it defeats borrow checking. Unsafe Rust, used directly, works too
- celeritascelery 1y agoIt does not defeat borrow checking. The borrow checker will ensure that objects do not outlive the arena. It works with borrow checking.
- Archit3ch 1y agoThis. Arenas don't work when you don't know when it's okay to free. The borrow checker can help with that (or you can track it manually in C/Zig).
- worik 1y agoThe borrow checker knows nothing about your arena allocations. That is if we are talking about the same thing! All the borrow checker knows is there is a chunk of memory (the arena) in scope. It works, no memory safety in the sense that you must manage your own garbage and you can reference uninitialized parts of the arena I have found myself using arenas in Rust for managing circular references (networks with cycles) and if I were to do it again I think I would write that bit in C or unsafe Rust.
- ben-schaaf 1y agoThe popular Bumpalo only returns references with the lifetime of the allocator. Not sure what you mean by manage your own garbage, an arena allocator deallocates everything when it goes out of scope. You definitely can't reference uninitialized parts of an arena.
- haberman 1y agoI agree, but in my experience arena allocation in Rust leaves something to be desired. I wrote something about this here: https://blog.reverberate.org/2021/12/19/arenas-and-rust.html https://blog.reverberate.org/2021/12/19/arenas-and-rust.html I was previously excited about this project which proposed to support arena allocation in the language in a more fundamental way: https://www.sophiajt.com/search-for-easier-safe-systems-programming/ https://www.sophiajt.com/search-for-easier-safe-systems-prog... That effort was focused primarily on learnability and teachability, but it seems like more fundamental arena support could help even for experienced devs if it made patterns like linked lists fundamentally easier to work with.
- taylorallred 1y agoThanks for those links. Have you tried using arenas that give out handles (sometimes indexes) instead of mutable references? It's less convenient and you're not leveraging borrow checking but I would imagine it supports Send well.