4 ms·
Exactly - people seem to assume that RAII means having to heap allocate each object individually and then having to deallocate each object individually when don
by csb6 2y ago
Exactly - people seem to assume that RAII means having to heap allocate each object individually and then having to deallocate each object individually when done. Clearly that is not true and the allocation patterns are orthogonal to whether RAII is used.
RAII is also useful for non-memory related tasks like unlocking mutexes once the lock is no longer needed, which is just plainly useful in any language that allows functions to have multiple exit points.
- jitl 2y agoI would prefer explicit linear types for locks, I find following Rust code using locks is very confusing - like an compiler-enforced call to `defer drop(lock)` to make it clear how it works. But I'm a noob at Rust.
- ozgrakkurt 2y agoA lot of concepts don’t directly mean you have to write bad code but they push you in that direction. Fact is, arena allocation or any other kind of custom allocation is pretty much non-existent in rust. Hopefully it will be implemented soon but I’m not sure if it is feasible because of lifetimes
- whytevuhuni 2y agoHow so? I've been using arena allocation with the bumpalo crate [1] with great success. I also wrote my own arena allocator for a WASM project, and also wrapped Nginx's memory pool via safe APIs. I'm not going to claim that writing one correctly is easy... much like writing any low-level allocators, you have to deal with layout, drop order, unsafe cells, etc. But using them? The lifetimes work great with them. And the allocator trait [2], once released, will make it so std collection types can be used as well. [1] https://docs.rs/bumpalo https://docs.rs/bumpalo [2] https://doc.rust-lang.org/std/alloc/trait.Allocator.html https://doc.rust-lang.org/std/alloc/trait.Allocator.html