logoalt Hacker News

logicchainstoday at 5:20 PM1 replyview on HN

If you're writing ultra low latency code you basically want everything allocated from an arena, with different arenas for different kinds of objects. Zig comes with this built in, while Rust makes it extremely unergonomic to do safely (due to the lifetime system).


Replies

kibwentoday at 6:15 PM

Arenas in Rust work very well with lifetimes. In fact arenas benefit greatly from lifetimes, because lifetimes allow them to uphold the usual Rust safety guarantees about preventing use-after-free. For example, here's the bumpalo crate in action:

    let mut arena = Bump::new(); // create arena
    
    let foo = arena.alloc(Foo { x: 42 }); // allocate item in arena
    
    bump.reset(); // clear arena
    
    foo.x += 1; // compiler error preventing use-after-free