Mostly-functional behavior in Java programs William C. Benton Red - - PowerPoint PPT Presentation

mostly functional behavior in java programs
SMART_READER_LITE
LIVE PREVIEW

Mostly-functional behavior in Java programs William C. Benton Red - - PowerPoint PPT Presentation

Mostly-functional behavior in Java programs William C. Benton Red Hat Emerging Technologies and University of Wisconsin Charles N. Fischer University of Wisconsin Motivation Wed like to do aggressive code transformations, specification


slide-1
SLIDE 1

William C. Benton Red Hat Emerging Technologies and University of Wisconsin Charles N. Fischer University of Wisconsin

Mostly-functional behavior in Java programs

slide-2
SLIDE 2

2

Motivation

We’d like to do aggressive code transformations, specification checking and analysis of large

  • bject-oriented programs.
slide-3
SLIDE 3

3

The problem

Java programs are difficult to analyze, transform, and reason about (in part) due to mutable state.

slide-4
SLIDE 4

Our contribution

4

Type-and-effect system and type-based analysis

slide-5
SLIDE 5

Our contribution

4

Type-and-effect system and type-based analysis

Initialization effects

slide-6
SLIDE 6

Our contribution

4

Type-and-effect system and type-based analysis

Initialization effects Quiescing field inference

slide-7
SLIDE 7

Our contribution

4

Type-and-effect system and type-based analysis

Initialization effects Quiescing field inference Degrees of method purity

slide-8
SLIDE 8

Our contribution

4

Type-and-effect system and type-based analysis

Initialization effects Quiescing field inference Degrees of method purity

slide-9
SLIDE 9

Our contribution

4

Type-and-effect system and type-based analysis

Initialization effects Quiescing field inference Degrees of method purity

Surprising result: substantial mostly- functional behavior in Java!

slide-10
SLIDE 10

Forecast

  • A simple object-oriented effects system
  • Initializers and initialization effects
  • Final fields and eventual immutability
  • Inferring quiescing fields
  • Evaluating quiescing field inference

5

slide-11
SLIDE 11

// method1: List<T> → int int method1(List<T> l) { return l.size(); }

Effect systems: motivation

6

// method2: List<T> → int int method2(List<T> l) { int i = 0; while (! l.isEmpty() ) { l.remove(0); i++; } return i; }

slide-12
SLIDE 12

// method1: List<T> → int int method1(List<T> l) { return l.size(); }

Effect systems: motivation

6

// method2: List<T> → int int method2(List<T> l) { int i = 0; while (! l.isEmpty() ) { l.remove(0); i++; } return i; }

READS state of l

slide-13
SLIDE 13

// method1: List<T> → int int method1(List<T> l) { return l.size(); }

Effect systems: motivation

6

// method2: List<T> → int int method2(List<T> l) { int i = 0; while (! l.isEmpty() ) { l.remove(0); i++; } return i; }

READS state of l READS, WRITES state of l

slide-14
SLIDE 14

// method1: List<T> → int int method1(List<T> l) { return l.size(); }

Effect systems: motivation

6

// method2: List<T> → int int method2(List<T> l) { int i = 0; while (! l.isEmpty() ) { l.remove(0); i++; } return i; }

Type systems: “what?” Effect systems: “how?”

READS state of l READS, WRITES state of l

slide-15
SLIDE 15

// method1: List<T> → int int method1(List<T> l) { return l.size(); }

Effect systems: motivation

6

// method2: List<T> → int int method2(List<T> l) { int i = 0; while (! l.isEmpty() ) { l.remove(0); i++; } return i; }

Type systems: “what?” Effect systems: “how?”

READS state of l READS, WRITES state of l

“How” consists of an effect (READ or WRITE) in some region.

slide-16
SLIDE 16

Object-oriented effect systems

7

  • Classic effect systems typically feature

lexically-scoped regions

  • Object-oriented effect systems better

support classes, fields, &c.

  • See Greenhouse & Boyland (ECOOP 99) or

Bierman & Parkinson (WOOD 03)

slide-17
SLIDE 17

Inferring effects for bytecodes

8



load rpt 



store rpt 

 

pmap

l = lh.ν lh.ν = l method invocations

slide-18
SLIDE 18

Inferring effects for bytecodes

8



load rpt 



store rpt 

 

pmap

l = lh.ν lh.ν = l method invocations

slide-19
SLIDE 19

Inferring effects for bytecodes

8



load rpt 



store rpt 

 

pmap

lh.ν = l method invocations

slide-20
SLIDE 20

Inferring effects for bytecodes

8



load rpt 



store rpt 

 

pmap

lh.ν = l method invocations

Regions we consider: ρthis, ρ0..n, and ⊤.

slide-21
SLIDE 21

Inferring effects for bytecodes

8



load rpt 



store rpt 

 

pmap

lh.ν = l method invocations

Regions we consider: ρthis, ρ0..n, and ⊤.

slide-22
SLIDE 22

Inferring effects for bytecodes

8



load rpt 



store rpt 

 

pmap

method invocations

Regions we consider: ρthis, ρ0..n, and ⊤.

slide-23
SLIDE 23

Inferring effects for bytecodes

8



load rpt 



store rpt 

 

pmap

method invocations

Regions we consider: ρthis, ρ0..n, and ⊤.

slide-24
SLIDE 24

Inferring effects for bytecodes

8



load rpt 



store rpt 

 

pmap

Regions we consider: ρthis, ρ0..n, and ⊤.

slide-25
SLIDE 25

Extending the simple system

9

Type-and-effect system and type-based analysis Initialization effects Quiescing field inference Degrees of method purity

slide-26
SLIDE 26

Extending the simple system

9

Type-and-effect system and type-based analysis

Initialization effects Quiescing field inference Degrees of method purity

slide-27
SLIDE 27

Extending the simple system

9

Type-and-effect system and type-based analysis

Initialization effects Quiescing field inference Degrees of method purity

slide-28
SLIDE 28

Forecast

  • A simple object-oriented effects system
  • Initializers and initialization effects
  • Final fields and eventual immutability
  • Inferring quiescing fields
  • Evaluating quiescing field inference

10

slide-29
SLIDE 29

Motivating initialization effects

11

class IntBox { private int i; IntBox(int j) { this.i = j; } int get() { return i; } }

slide-30
SLIDE 30

Motivating initialization effects

11

class IntBox { private int i; IntBox(int j) { this.i = j; } int get() { return i; } }

reads IntBox.i from this writes IntBox.i to this

slide-31
SLIDE 31

Motivating initialization effects

11

class IntBox { private int i; IntBox(int j) { this.i = j; } int get() { return i; } } These two effects cannot interfere!

reads IntBox.i from this writes IntBox.i to this

slide-32
SLIDE 32

Motivating initialization effects

11

class IntBox { private int i; IntBox(int j) { this.i = j; } int get() { return i; } } These two effects cannot interfere!

reads IntBox.i from this initializes IntBox.i of this

slide-33
SLIDE 33

Motivating initialization effects

12

class IntBox { private int i; IntBox(int j) { this.i = j; } int get() { return i; } } IntBox factory(int j) { IntBox r = new IntBox(j); return r; }

slide-34
SLIDE 34

Motivating initialization effects

12

class IntBox { private int i; IntBox(int j) { this.i = j; } int get() { return i; } } IntBox factory(int j) { IntBox r = new IntBox(j); return r; }

“Pure” methods can modify newly-allocated

  • bjects (Leavens et al.;

Rugina and Cherem)

slide-35
SLIDE 35

Defining initialization effects

13

class IntBox { private int i; IntBox(int j) { this.i = j; } int get() { return i; } }

initializes IntBox.i field of this

slide-36
SLIDE 36

Defining initialization effects

13

class IntBox { private int i; IntBox(int j) { this.i = j; } int get() { return i; } }

An initialization effect is a

WRITE to the state of an

  • bject during its creation.

An initializer is a method that executes on an object during the dynamic lifetime

  • f its constructor.
slide-37
SLIDE 37

Inferring initializer methods

14

slide-38
SLIDE 38

Inferring initializer methods

14

slide-39
SLIDE 39

Inferring initializer methods

14

this this this this

slide-40
SLIDE 40

Inferring initializer methods

14

this this this this

A constructor is an initializer on its receiver object.

slide-41
SLIDE 41

Inferring initializer methods

14

this this this this

A constructor is an initializer on its receiver object. TWO A constructor is an initializer on its receiver object. A method that is only invoked via this-edges from an initializer is also an initializer on its receiver object.

slide-42
SLIDE 42

Inferring initialization effects

15

Initialization effects are writes to fields of this that occur within an initializer.

slide-43
SLIDE 43

Forecast

  • A simple object-oriented effects system
  • Initializers and initialization effects
  • Final fields and eventual immutability
  • Inferring quiescing fields
  • Evaluating quiescing field inference

16

slide-44
SLIDE 44

class IntBox { private int i; IntBox(int j) { this.i = j; } int get() { return i; } }

Final fields

17

slide-45
SLIDE 45

Final fields

17

class IntBox { private final int i; IntBox(int j) { this.i = j; } int get() { return i; } }

slide-46
SLIDE 46

Final fields

17

class IntBox { private final int i; IntBox(int j) { this.i = j; } int get() { return i; } }

i is a run-time constant

slide-47
SLIDE 47

Final fields

17

class IntBox { private final int i; IntBox(int j) { this.i = j; } int get() { return i; } }

slide-48
SLIDE 48

Final fields

17

class IntBox { private final int i; IntBox(int j) { init(i); } int get() { return i; } private void init(int j) { this.i = j; } }

slide-49
SLIDE 49

Final fields

17

class IntBox { private final int i; IntBox(int j) { init(i); } int get() { return i; } private void init(int j) { this.i = j; } }

Final fields must be assigned exactly once on every path through each constructor and may only be assigned in the constructor.

slide-50
SLIDE 50

Final fields and immutability

18

  • Java definition of final is restrictive,

designed for simple verification

  • Many fields that represent run-time

constants are not declared final

  • Several groups have developed analyses

to find such fields

slide-51
SLIDE 51

One example: stationary fields

  • Unkel and Lam (2008): stationary fields

are never written after they are read

  • About 50% of fields in open-source Java

programs can be inferred stationary; a much smaller percentage are final

  • Their analysis is based on flow- and

context-sensitive points-to analysis

19

slide-52
SLIDE 52

Forecast

  • A simple object-oriented effects system
  • Initializers and initialization effects
  • Final fields and eventual immutability
  • Inferring quiescing fields
  • Evaluating quiescing field inference

20

slide-53
SLIDE 53

Quiescing fields

  • A field is quiescing if it is initialized but

never written; all final fields are quiescing

  • Inference algorithm for these is

straightforward: consider only fields that aren’t implicated in a WRITE effect

21

slide-54
SLIDE 54

Comparing kinds of fields

  • All final fields are quiescing fields
  • Some quiescing fields are not stationary
  • Some stationary fields are not quiescing
  • The inference algorithm for quiescing

fields runs in seconds on substantial Java programs

22

slide-55
SLIDE 55

Forecast

  • A simple object-oriented effects system
  • Initializers and initialization effects
  • Final fields and eventual immutability
  • Inferring quiescing fields
  • Evaluating quiescing field inference

23

slide-56
SLIDE 56

Evaluation

  • Medium-sized Java programs from the

DaCapo benchmark suite

  • Soot and DIMPLE for bytecode analysis,

performed on workstation hardware

  • Executed benchmarks under

instrumented Jikes RVM

24

slide-57
SLIDE 57

Benchmarks

25

statements classes fields methods antlr 1.39 M 3729 14082 37209 bloat 1.41 M 3827 14524 33609 eclipse 1.38 M 3895 15161 33408 hsqldb 1.59 M 4190 17566 38504 jython 1.45 M 4058 14737 35604 luindex 1.35 M 3903 14511 32759 pmd 1.51 M 4265 15489 36393

slide-58
SLIDE 58

Static prevalence of final and quiescing fields

26 25 50 75 100 antlr bloat eclipse hsqldb jython luindex pmd

Final fields Quiescing fields

slide-59
SLIDE 59

20 40 60 80 100 antlr bloat eclipse hsqldb jython luindex pmd

Final and quiescing fields as a percentage of all dynamic reads

27

Reads from final fields Reads from quiescing fields

slide-60
SLIDE 60

Conclusion

28

  • Three novel features improve the

precision of type-and-effect systems

  • A significant portion of Java field reads

are from fields with unchanging values

  • It is possible to efficiently infer quiescing

fields with type-based analyses

slide-61
SLIDE 61

Thanks!

willb@acm.org http://www.cs.wisc.edu/~willb/

29