# Arenas and Rust

DevFeed: [Arenas and Rust](<https://devfeed.tech/articles/arenas-and-rust-21137.md>)

Original publisher: [Read original article](<https://blog.reverberate.org/2021/12/19/arenas-and-rust.html>)

Author: Haberman

Published: 2021-12-19T00:00:00Z

Content type: article

Language: en

Sources: [Josh Haberman](<https://devfeed.tech/sources/josh-haberman.md>)

Topics: [Rust](<https://devfeed.tech/topics/rust.md>), [C](<https://devfeed.tech/topics/c.md>), [C++](<https://devfeed.tech/topics/c-plus-plus.md>), [Library](<https://devfeed.tech/topics/library.md>)

Tags: [arena](<https://devfeed.tech/tags/arena.md>), [c](<https://devfeed.tech/tags/c.md>), [c-plus-plus](<https://devfeed.tech/tags/c-plus-plus.md>), [concurrent](<https://devfeed.tech/tags/concurrent.md>), [efficiency](<https://devfeed.tech/tags/efficiency.md>), [memory](<https://devfeed.tech/tags/memory.md>), [rust](<https://devfeed.tech/tags/rust.md>), [safety](<https://devfeed.tech/tags/safety.md>)

## AI overview

This article examines arena allocation in Rust and compares it with arena APIs in C and C++. It explains how Rust lifetimes can statically ensure that arena-allocated references do not outlive their arena, and discusses differences in thread-safety, efficiency, and complexity.

## Source excerpt

For a while I've been wondering what it would be like to use arenas in Rust. In C and C++ I have been turning to arenas more and more as a fast alternative to heap allocation. If you have a bunch of objects that share a common lifetime, arenas offer cheaper allocation and much cheaper deallocation than the heap. The more I use this pattern, the more it feels downright wasteful to use heap allocation when an arena would do. I've been wanting to know how arenas would play with Rust's lifetime semantics. An arena must always outlive all the objects allocated from that arena. Rust's lifetime system seems ideal for expressing a condition like this. I was curious to see how this plays out in practice. Arena APIs C and C++ First I will present the arena APIs I am familiar with in C and C++. Here is a simplified version of the C++ Arena API for protobuf: // The C++ Arena is thread-safe (Functions taking Arena* may be called // concurrently, except the destructor). class Arena { public: Arena(); ~Arena(); // Frees all objects in the arena. // Creates an object on the arena. The object is freed when the arena is // destroyed. The destructor will be run unless it is trivial. template <class T, class... Args> static T* Create(Arena* arena, Args&&... args); } void Test() { Arena arena; int* i1 = Arena::Create<int>(&arena); int* i2 = Arena::Create<int>(&arena); int* i3 = Arena::Create<int>(&arena); // Use i1, i2, i3... // When the arena is destroyed, the individual objects are freed. } Here is a similar but somewhat different example in C, from the upb protobuf library: // The C arena is thread-compatible, but not thread-safe (functions that take // upb_arena* may not be called concurrently). upb_arena *upb_arena_new(void); // Frees all memory in the arena. void upb_arena_free(upb_arena *a); // Allocates so memory from the arena. void *upb_arena_malloc(upb_arena *a, size_t size); void test() { upb_arena *arena = upb_arena_new(); int* i1 = upb_arena_malloc(arena, sizeof(*i1)); int