Locality Mode OxCaml
Other keywords to spot: stack_, global_, @ global, exclave_ and [@local_opt]
Type: what it is -; Mode: how it's used
Mode | Lifetime | Allocation |
|---|---|---|
| MAY outlive its region | MUST be on the heap |
| MUST NOT outlive its region | MAY be on the stack |
- Region: compile-time representation of a stack frame
- Function bodies
- Loop bodies
- Lazy expressions
- Module level bindings
- Inference decides how to allocate, defaults to the stack
- Regions can nest and are wider than scopes
What does local mean?
- The value does not escape its region
- Neither function result nor exception payload
- Not captured in closure, not referred from mutable area
- Not reachable from a heap allocated value
- Freed at its region's end, without triggering the GC
- In function types -; This is the most important
- Contract between caller and callee
localmeans in the caller's region- Parameter: callee respects caller's locality
- Result: callee stores in caller's region
This really defines 4 arrows
val global_global : s -> t * t (* Legacy *) val local_global : local_ s -> t * t val global_local : s -> local_ t * t val local_local : local_ s -> local_ t * t
What is `local` for?
0. Low-latency code More importantly, stack allocations will never trigger a GC, and so they're safe to use in low-latency code that must currently be zero-alloc 1. Functions passed to higher-order iterators (such as `map`, `fold`, `bind` and others) are allocated on the stack 2. Safer callbacks