Automatic Memory Management Without Run-time Overhead Brian Brooks
What's the point? I love my stop-the-world GC... ● The whole point of automatic memory management is that an object lives as long as it needs to, preferably no longer.
What's the point? I love my stop-the-world GC... ● The whole point of automatic memory management is that an object lives as long as it needs to, preferably no longer. ● When can we free an object?
What's the point? I love my stop-the-world GC... ● The whole point of automatic memory management is that an object lives as long as it needs to, preferably no longer. ● When can we free an object? – When we can guarantee it wont be used again.
We will look at... ● Linear type systems ● Region Based Memory Management ● Capability Calculus
History of linearity ● Linear logic (Girard) – Propositions must be used exactly once – No duplication or discarding ● Linear types (Wadler) – No duplication: only one pointer to a value – No discarding: use once, then deallocate
A Linear Type System
Are Linear Types Practical? ● Key point: values may only be used once. So deallocation is safe. ● Not really expressive. ● Could include non-linear types. Must GC.
Regions ● All values are stored in regions. ● Heap contains stack of regions. ● LIFO ordering of region lifetimes.
Regions ● All values are stored in regions. ● Heap contains stack of regions. ● LIFO ordering of region lifetimes. ● 2 annotations: “Evaluate e1 to some value and store it in region p” “Allocate a new region and bind it to p, evaluate e2, deallocate region.”
● Must ensure that: Region Safety
● Must ensure that: ● Need to track region accesses while type checking!! Region Safety
● Must ensure that: ● Need to track region accesses while type checking!! ● Annotate function type with an effect – Effect: set of regions the function may access Region Safety
● Must ensure that: ● Need to track region accesses while type checking!! ● Annotate function type with an effect – Effect: set of regions the function may access Region Safety
Regions: What else? ● Region inference – Translation from source to target annotated lang – Eliminate the need for most annotations ● Region polymorphism – Functions can be “parameterized” over regions. – Ex: Can pass a region to a function to store it's return value in.
Capabilities ● Key idea: values are still stored in regions, but region allocation / deallocation is explicit. ● Capabilities are similar to effects: – “Under … assumptions, it is legal to execute d, provided the capability C is held. – Capabilities indicate the set of regions that are valid to access (haven't been free'd).
Capabilities ● If we allocate a new region p, we update C to include p ● If we deallocate a region p, p must be in C, we then update C to remove p. ● If we try to read some value – The value v : T at p must be in the typing ctxt – AND, C must contain p.
Conclusion ● Linear types aren't very expressive. – No aliasing – Values may be used once ● Region Based Memory Management – Safe, simple discipline of automatic memory management. ● Capability Calculus – More expressive: explicit region lifetimes.
Questions?
References ● Linear types can change the world! Philip Wadler ● Region-Based Memory Management. Tofte & Talpin ● Typed Memory Management. David Walker ● Effect Types and Region-based Memory Management. Ch in AdvTAPL, Pierce.