Tutorial
Stack Allocation with Locals in OCaml | OCaml Unboxed
- The Jane Street team plans to upstream local mode changes to the general OCaml compiler via an open source branch currently available for download.
- Standard OCaml initialization functions allocate significant heap memory via con cells, which are eventually reclaimed by the garbage collector after becoming unreachable.
- Mark and sweep garbage collection introduces latency and cache misses because it requires halting all threads sharing a heap to scan memory for references.
- Jane Street anticipates that while standard garbage collection suffices for most programs, it is undesirable in scenarios requiring immediate action where latency or slowdowns are unacceptable.
- Local mode allocates memory on a dedicated local stack distinct from both the call stack and the heap to manage variables that persist across function returns but not across regions.
- When a loop region concludes, all locals allocated within it can be deallocated instantly by updating a single memory value, allowing immediate reuse without marking, sweeping, or cache invalidation.
- A minor structural mismatch between functions and regions is managed by a component called exclave, and local allocation may introduce additional safety properties.