Escape Analysis and Scalar Replacement
Determine whether an allocation’s identity can be observed outside a region and use that fact to eliminate storage work.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 2
- Reading time
- 1 min
In this article2 chapters
An object can escape by several routes
Returning an object, storing it in shared state or passing it to a call with unknown behavior can expose it beyond its creating activation. If it stays local and its identity has no relevant observation, the compiler can consider alternative storage. Follow aliases as well as the name that held the initial reference. An object can escape through another object that contains it.
Field values can replace an allocation
Suppose a function constructs a pair of integers and immediately adds its two fields. When identity and allocation effects are unobservable under the model, keep the field values in separate intermediate names and return their sum. This is scalar replacement. If the pair is retained by a closure, its fields may need longer-lived storage. Alias analysis supports both the escape decision and the proof that intervening operations cannot alter the extracted fields.[1][2]