Region Inference for an Object-Oriented Language Wei Ngan Chin1,2 Joint work with Florin Craciun1, Shengchao Qin1,2, Martin Rinard3 National University of Singapore 2 Singapore-MIT Alliance 3 MIT 1 11th June 2004 PLDI 2004 1 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 2 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 3 Two Major Assumptions top (younger) • Lexically-Scoped Regions: Stack-like lifetimes construct for local region : letreg r in e • No Dangling References: r4 r3 r2 Regions of components should have longer lifetimes than its object. r1 bottom (older) 4 Main Contributions • Region Inference for Core-Java • Constraint-Based Type System. • Region Polymorphism & Region Subtyping • Handles Inheritance, Overriding, Downcast • Implementation 5 Region-Annotated Core-Java region parameters for polymorphism class invariant method precondition region constraints constraint abstractions 6 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 property as class invariant. Step 4 : Inherit constraint of superclass. 7 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. 8 Inferring for Methods 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. 9 Outline • Background • Key Techniques • Inferring Class • Inferring Methods • Region Subtyping • Region Inference • Method Overriding + Downcast • Experimental Results 10 (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) 11 Example Without region subtyping, regions of a and b are forced to be equal. With region subtyping, these regions may be distinct. 12 Problem – Recursive Fields Recursive nodes being forced into the same region Some programs work better with recursive nodes in different regions … 13 (Field) Region Subtyping SOLUTION : Covariance for Read-Only Fields. Example: mutable read-only 14 Outline • Background • Key Techniques • Region Inference • Method Overriding + Downcast • Experimental Results 15 Region Inference • Main type inference rules • Apply inference according to global dependency graph (class,call,override dependencies). 16 Inference Rule : Sequence Inference is flow-insensitive but can be improved by SSA form 17 Inference Rule : Expression Block rs : the set of regions that do not escape the block Regions of rs coalesced into a single localised region r 18 Example – Localised Region 19 Example – Localised Region localised region r 20 Example : Recursive Function 21 Outline • Background • Key Techniques • Region Inference • Overriding + Downcast • Experimental Results 22 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 : Allows B.mn may be invoked where A.mn is expected Method Override Rule: CB Æ MA ) MB 23 Override Conflict Resolution Derived constraints CB MA MB may conflict with method override rule. Solution : strengthen CB , MA to C’B , M’A Until: C’B Æ M’A ) MB 24 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 25 Experiments • Prototype was built using Haskell. • Inference is fast and takes <5s for the Olden benchmark. 26 Experiments • Inference is competitive to hand-annotation (RegJava benchmark) • similar localised regions • same space recovery • Region subtyping gives better space recovery for some examples. 27 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. 28 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 29 Correctness 30 Core-Java 31 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. 32 No Region Subtyping [Boyapati et al PLDI03] supports class subtyping but not region subtyping 33 No Region Subtyping [Boyapati et al PLDI03] supports class subtyping but not region subtyping 34 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. 35 Example - Reynolds3 With field region subtyping, the List(x,p) object can be allocated in a local region of the recursive call. 36 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: selection argument result Method Override Rule: CB Æ MA ) MB 37 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 38 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 39 Experiments • Inference is competitive hand-annotation. • Prototype is fast (< 5s for upto 1000 lines) • http://loris-4.ddns.comp.nus.edu.sg/~craciunm 40 Experiments 41 Inference of Methods class invariant method precondition 42 Inferring for Class class invariant to prevent dangling references 43 List Example Key Principles • First region is for only object of the class. • Keep regions of fields distinct for precision. 44 Inference of Methods Key Principles • Region polymorphism from parameters/result • Method precondition is applicable to only those objects that may invoke the method. 45 Example – Cyclic Structure p1 p2 46 Inference of Methods method precondition 47