480 likes | 565 Views
Region Inference for an Object-Oriented Language. Wei Ngan Chin 1,2 Joint work with Florin Craciun 1 , Shengchao Qin 1,2 , Martin Rinard 3 1 National University of Singapore 2 Singapore-MIT Alliance 3 MIT. Background. Basic problem : how to recover memory of dead objects?
E N D
Region Inference for an Object-Oriented Language Wei Ngan Chin1,2 Joint work with Florin Craciun1, Shengchao Qin1,2, Martin Rinard3 1 National University of Singapore 2 Singapore-MIT Alliance 3 MIT PLDI 2004
Background • Basic problem: • how to recover memory of dead objects? • Two Approaches • garbage collection • explicit memory management • Our Focus: • Explicit memory regions with different lifetimes
Current Status Region Inference for a functional language • -Calculus [Tofte-Talpin POPL94] Previous Works on Region Type Checkers: • Java [DIKU98], • Real-Time Java : [MIT-PLDI03], • Cyclone [Cornell-PLDI02]. Our Goal: automatic region inference for object-oriented programs
Two Major Assumptions top (younger) • Lexically-Scoped Regions: Stack-like lifetimes construct for local region : letreg r in e r4 r3 • No Dangling References: Regions of components should have longer lifetimes than its object. r2 r1 bottom (older)
Main Contributions • Region Inference for Core-Java • Constraint-Based Type System. • Region Polymorphism & Region Subtyping • Handles Inheritance, Overriding, Downcast • Implementation
region parameters for polymorphism class invariant method precondition region constraints constraint abstractions Region-Annotated Core-Java
Step 1 : Reserve first region for the receiver object (this). Step 2 : Keep regions of fields distinct for precision. Step 3 : No dangling property as class invariant. Inferring for Class Step 4 : Inherit constraint of superclass.
Inferring for Class Step 1 : Reserve first region for the receiver object (this). Step 2 : Keep regions of fields distinct for precision. Step 3 : No dangling requirement as class invariant.
method precondition Step 1 : Region parameters from method signature. Step 2 : Gather constraints from method body. Step 3 : Localise regions that do not escape. Step 4 : Fix-point analysis for recursion. Inferring for Methods
Outline • Background • Key Techniques • Inferring Class • Inferring Methods • Region Subtyping • Region Inference • Method Overriding + Downcast • Experimental Results
(Object) Region Subtyping concept pioneered in Cyclone [PLDI02] an object w of type c<r,..>can be passed to a location v of type c<s,..> provided r º s (e.g. in assignment and parameter/result passing)
Example Without region subtyping, regions of a and bare forced to be equal. With region subtyping, these regions may be distinct.
Some programs work better with recursive nodes in different regions … Problem – Recursive Fields Recursive nodes being forced into the same region
(Field) Region Subtyping SOLUTION : Covariance for Read-Only Fields. Example: mutable read-only
Outline • Background • Key Techniques • Region Inference • Method Overriding + Downcast • Experimental Results
Region Inference • Main type inference rules • Apply inference according to global dependency graph (class,call,override dependencies).
Inference Rule : Sequence Inference is flow-insensitive but can be improved by SSA form
Inference Rule : Expression Block rs : the set of regions that do not escape the block Regions ofrscoalesced into a single localised region r
localised region r Example – Localised Region
Outline • Background • Key Techniques • Region Inference • Overriding + Downcast • Experimental Results
Method Overriding class Bhr1 .. rni extends Ahr1 .. rmi where CB In class A: Y mnhr’1,..,r’pi (X) where MA { } In class B: Y mnhr’1,..,r’pi (X) where MB { } Dynamic Dispatch : AllowsB.mn may be invoked where A.mn is expected Method Override Rule: CBÆMA)MB
Override Conflict Resolution Derived constraints CB MA MB may conflict with method override rule. Solution : strengthenCB , MAto C’B,M’A Until: C’BÆM’A)MB
Downcast Safety • Compile-time guarantee to downcast safety. • Problem : regions are lost during upcast that have to be recovered for downcast. Our Solution: • Selectively maintain extra regions that are lost during upcast and may be needed by downcast. • Use flow analysis
Experiments • Prototype was built using Haskell. • Inference is fast and takes <5s for the Olden benchmark.
Experiments • Inference is competitive to hand-annotation (RegJava benchmark) • similar localised regions • same space recovery • Region subtyping gives better space recovery for some examples.
Concluding Remarks • Constraint-based region inference system: • Region subtyping. • Region polymorphism. • Polymorphic recursion • Handles OO features: • class subtyping • method overriding • downcast safety • Implementation is fast and precise.
Future Work • Better Lifetime Precision • variable liveness analysis • flow sensitivity via SSA • treat null as a primitive value • Memory Efficiency : Sized Regions + Reuse • Extensions : Genericity + RTSJ
Key Principles : Classes • Region type : chr1,…, rni • r1 : (region for current object of this type) • r2,…,rn (regions for components/fields) • rkº r1, 8 k 2 2..n (no dangling references) • First region is special. • Keep regions of components distinct.
No Region Subtyping [Boyapati et al PLDI03] supports class subtyping but not region subtyping
No Region Subtyping [Boyapati et al PLDI03] supports class subtyping but not region subtyping
Key Principles : Methods • Region polymorphism from the region types of parameters and output • t mn hr*i((t v)*) where rc e • r* captures region polymorphism • Keep region constraints of classes and methods separate.
Example - Reynolds3 With field region subtyping, the List(x,p) object can be allocated in a local region of the recursive call.
Method Overriding class Bhr1 .. rni extends Ahr1 .. rmi where CB In class A: Y mnhr’1,..,r’pi (X) where MA { } In class B: Y mnhr’1,..,r’pi (X) where MB { } Function Subtyping Rule: Method Override Rule: CBÆMA)MB selection argument result
Downcast Safety • In our framework : • regions may be lost during upcast e.g. Object<r1> o = new Pair<s1,s2,s3> (…) • lost regions must be recovered during downcast. e.g. Pair<a1,a2,a3> p = (Pair) o • Key technique : maintain extra regions that were lost during upcasting and may be needed! e.g. Object<r1>[r2,r3] p = new Pair<s1,s2,s3> (…) • Solution : Flow analysis
Region Subtyping concept from [Cyclone PLDI02] allows v=w where location v::c<s,..> can be passed an object w of type c<r,..> provided r s
Experiments • Inference is competitive hand-annotation. • Prototype is fast (< 5s for upto 1000 lines) • http://loris-4.ddns.comp.nus.edu.sg/~craciunm
Inference of Methods class invariant method precondition
Inferring for Class class invariant to prevent dangling references
List Example Key Principles • First region is for only object of the class. • Keep regions of fields distinct for precision.
Inference of Methods Key Principles • Region polymorphism from parameters/result • Method precondition is applicable to only those objects that may invoke the method.
Inference of Methods method precondition