1 / 47

Region Inference for an Object-Oriented Language

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?

howe
Download Presentation

Region Inference for an Object-Oriented Language

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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

  3. 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

  4. 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)

  5. Main Contributions • Region Inference for Core-Java • Constraint-Based Type System. • Region Polymorphism & Region Subtyping • Handles Inheritance, Overriding, Downcast • Implementation

  6. region parameters for polymorphism class invariant method precondition region constraints constraint abstractions Region-Annotated Core-Java

  7. 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.

  8. 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.

  9. 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

  10. Outline • Background • Key Techniques • Inferring Class • Inferring Methods • Region Subtyping • Region Inference • Method Overriding + Downcast • Experimental Results

  11. (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)

  12. Example Without region subtyping, regions of a and bare forced to be equal. With region subtyping, these regions may be distinct.

  13. Some programs work better with recursive nodes in different regions … Problem – Recursive Fields Recursive nodes being forced into the same region

  14. (Field) Region Subtyping SOLUTION : Covariance for Read-Only Fields. Example: mutable read-only

  15. Outline • Background • Key Techniques • Region Inference • Method Overriding + Downcast • Experimental Results

  16. Region Inference • Main type inference rules • Apply inference according to global dependency graph (class,call,override dependencies).

  17. Inference Rule : Sequence Inference is flow-insensitive but can be improved by SSA form

  18. Inference Rule : Expression Block rs : the set of regions that do not escape the block Regions ofrscoalesced into a single localised region r

  19. Example – Localised Region

  20. localised region r Example – Localised Region

  21. Example : Recursive Function

  22. Outline • Background • Key Techniques • Region Inference • Overriding + Downcast • Experimental Results

  23. 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

  24. 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

  25. 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

  26. Experiments • Prototype was built using Haskell. • Inference is fast and takes <5s for the Olden benchmark.

  27. Experiments • Inference is competitive to hand-annotation (RegJava benchmark) • similar localised regions • same space recovery • Region subtyping gives better space recovery for some examples.

  28. 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.

  29. 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

  30. Correctness

  31. Core-Java

  32. 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.

  33. No Region Subtyping [Boyapati et al PLDI03] supports class subtyping but not region subtyping

  34. No Region Subtyping [Boyapati et al PLDI03] supports class subtyping but not region subtyping

  35. 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.

  36. Example - Reynolds3 With field region subtyping, the List(x,p) object can be allocated in a local region of the recursive call.

  37. 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

  38. 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

  39. 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

  40. Experiments • Inference is competitive hand-annotation. • Prototype is fast (< 5s for upto 1000 lines) • http://loris-4.ddns.comp.nus.edu.sg/~craciunm

  41. Experiments

  42. Inference of Methods class invariant method precondition

  43. Inferring for Class class invariant to prevent dangling references

  44. List Example Key Principles • First region is for only object of the class. • Keep regions of fields distinct for precision.

  45. Inference of Methods Key Principles • Region polymorphism from parameters/result • Method precondition is applicable to only those objects that may invoke the method.

  46. Example – Cyclic Structure p1 p2

  47. Inference of Methods method precondition

More Related