docs/heap/zone-allocator.md
The Zone allocator is a region-based memory manager used in V8 to support fast allocation of small chunks of memory. Chunks allocated in a Zone cannot be deallocated individually. Instead, the Zone supports deallocating all chunks in one fast operation.
This makes Zones ideal for holding temporary data structures that are needed only for a specific phase of execution, such as the Abstract Syntax Tree (AST) or compiler intermediate representations (IR), which can be discarded all at once after compilation is complete.
Notably, object pointers within a Zone can be relied on to stay live and available after allocation for as long as the Zone is live. This allows easier definition of complex data structures without having to worry about reachability, reference counting, or clean teardown of individual elements.
The Zone allocator works by requesting large segments of memory from the OS or a memory pool and then satisfying allocation requests by simply moving a pointer within the current segment.
A Zone maintains a linked list of Segments. When a Zone is created, it doesn't allocate any memory immediately. The first allocation triggers the allocation of a segment.
Segments are requested via the AccountingAllocator.
Within a segment, allocation is extremely fast. It is a "bump pointer" allocation:
When the current segment is full, the Zone calls Expand to get a new segment.
kMinimumSegmentSize, typically 8KB) and a maximum (kMaximumSegmentSize, typically 32KB).Zone: The main class representing the allocator. It provides Allocate, New, and AllocateArray methods.ZoneSnapshot: Stores the current allocation state of a Zone (position in the current segment and the list of segments).ZoneScope: A RAII-style class that takes a snapshot of a Zone on creation and restores the Zone to that state on destruction, effectively freeing all memory allocated during the scope.ZoneObject: A base class for objects that should be allocated in a Zone. It deletes the delete operator to prevent accidental individual deallocation.
std::vector that manage their own memory).To facilitate the use of Zones, V8 provides a set of containers that are specialized to use Zone allocation:
ZoneVector<T>: A custom vector implementation giving precise control over its implementation and performance.ZoneMap, ZoneSet, ZoneDeque, etc.: Wrappers around standard C++ containers specialized with a ZoneAllocator.These containers ensure that their internal nodes and backing stores are allocated within the Zone.
The Zone allocator is used pervasively in the V8 compiler (src/compiler/).
BytecodeAnalysis are allocated in a Zone and contain Zone-based containers like ZoneVector and ZoneMap to store analysis data.Note: The Zone implementation is inherently not thread-safe.