Mostly-functional behavior in Java programs
Transcript of Mostly-functional behavior in Java programs
![Page 1: Mostly-functional behavior in Java programs](https://reader030.fdocuments.in/reader030/viewer/2022012121/61dda05b50c201441c3cbb56/html5/thumbnails/1.jpg)
William C. BentonRed Hat Emerging Technologies and University of Wisconsin
Charles N. Fischer University of Wisconsin
Mostly-functional behavior in Java programs
![Page 2: Mostly-functional behavior in Java programs](https://reader030.fdocuments.in/reader030/viewer/2022012121/61dda05b50c201441c3cbb56/html5/thumbnails/2.jpg)
2
Motivation
We’d like to do aggressive code transformations, specification checking and analysis of large object-oriented programs.
![Page 3: Mostly-functional behavior in Java programs](https://reader030.fdocuments.in/reader030/viewer/2022012121/61dda05b50c201441c3cbb56/html5/thumbnails/3.jpg)
3
The problem
Java programs are difficult to analyze, transform, and reason about (in part) due to mutable state.
![Page 4: Mostly-functional behavior in Java programs](https://reader030.fdocuments.in/reader030/viewer/2022012121/61dda05b50c201441c3cbb56/html5/thumbnails/4.jpg)
Our contribution
4
Type-and-effect system and type-based analysis
![Page 5: Mostly-functional behavior in Java programs](https://reader030.fdocuments.in/reader030/viewer/2022012121/61dda05b50c201441c3cbb56/html5/thumbnails/5.jpg)
Our contribution
4
Type-and-effect system and type-based analysis
Initialization effects
![Page 6: Mostly-functional behavior in Java programs](https://reader030.fdocuments.in/reader030/viewer/2022012121/61dda05b50c201441c3cbb56/html5/thumbnails/6.jpg)
Our contribution
4
Type-and-effect system and type-based analysis
Initialization effects
Quiescing field inference
![Page 7: Mostly-functional behavior in Java programs](https://reader030.fdocuments.in/reader030/viewer/2022012121/61dda05b50c201441c3cbb56/html5/thumbnails/7.jpg)
Our contribution
4
Type-and-effect system and type-based analysis
Initialization effects
Quiescing field inference
Degrees of method purity
![Page 8: Mostly-functional behavior in Java programs](https://reader030.fdocuments.in/reader030/viewer/2022012121/61dda05b50c201441c3cbb56/html5/thumbnails/8.jpg)
Our contribution
4
Type-and-effect system and type-based analysis
Initialization effects
Quiescing field inference
Degrees of method purity
![Page 9: Mostly-functional behavior in Java programs](https://reader030.fdocuments.in/reader030/viewer/2022012121/61dda05b50c201441c3cbb56/html5/thumbnails/9.jpg)
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!
![Page 10: Mostly-functional behavior in Java programs](https://reader030.fdocuments.in/reader030/viewer/2022012121/61dda05b50c201441c3cbb56/html5/thumbnails/10.jpg)
Forecast
• A simple object-oriented effects system
• Initializers and initialization effects
• Final fields and eventual immutability
• Inferring quiescing fields
• Evaluating quiescing field inference
5
![Page 11: Mostly-functional behavior in Java programs](https://reader030.fdocuments.in/reader030/viewer/2022012121/61dda05b50c201441c3cbb56/html5/thumbnails/11.jpg)
// method1: List<T> → intint method1(List<T> l) {
return l.size();}
Effect systems: motivation
6
// method2: List<T> → intint method2(List<T> l) {
int i = 0;while (! l.isEmpty() ) {
l.remove(0); i++;}return i;
}
![Page 12: Mostly-functional behavior in Java programs](https://reader030.fdocuments.in/reader030/viewer/2022012121/61dda05b50c201441c3cbb56/html5/thumbnails/12.jpg)
// method1: List<T> → intint method1(List<T> l) {
return l.size();}
Effect systems: motivation
6
// method2: List<T> → intint method2(List<T> l) {
int i = 0;while (! l.isEmpty() ) {
l.remove(0); i++;}return i;
}
READS state of l
![Page 13: Mostly-functional behavior in Java programs](https://reader030.fdocuments.in/reader030/viewer/2022012121/61dda05b50c201441c3cbb56/html5/thumbnails/13.jpg)
// method1: List<T> → intint method1(List<T> l) {
return l.size();}
Effect systems: motivation
6
// method2: List<T> → intint 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
![Page 14: Mostly-functional behavior in Java programs](https://reader030.fdocuments.in/reader030/viewer/2022012121/61dda05b50c201441c3cbb56/html5/thumbnails/14.jpg)
// method1: List<T> → intint method1(List<T> l) {
return l.size();}
Effect systems: motivation
6
// method2: List<T> → intint 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
![Page 15: Mostly-functional behavior in Java programs](https://reader030.fdocuments.in/reader030/viewer/2022012121/61dda05b50c201441c3cbb56/html5/thumbnails/15.jpg)
// method1: List<T> → intint method1(List<T> l) {
return l.size();}
Effect systems: motivation
6
// method2: List<T> → intint 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.
![Page 16: Mostly-functional behavior in Java programs](https://reader030.fdocuments.in/reader030/viewer/2022012121/61dda05b50c201441c3cbb56/html5/thumbnails/16.jpg)
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)
![Page 17: Mostly-functional behavior in Java programs](https://reader030.fdocuments.in/reader030/viewer/2022012121/61dda05b50c201441c3cbb56/html5/thumbnails/17.jpg)
Inferring effects for bytecodes
8
-formal
rpt
-
rpt
-formal pt
rpt
load rpt
store rpt
pmap
l = lh.ν lh.ν = l
method invocations
![Page 18: Mostly-functional behavior in Java programs](https://reader030.fdocuments.in/reader030/viewer/2022012121/61dda05b50c201441c3cbb56/html5/thumbnails/18.jpg)
Inferring effects for bytecodes
8
-formal
rpt
-
rpt
-formal pt
rpt
load rpt
store rpt
pmap
l = lh.ν lh.ν = l
method invocations
![Page 19: Mostly-functional behavior in Java programs](https://reader030.fdocuments.in/reader030/viewer/2022012121/61dda05b50c201441c3cbb56/html5/thumbnails/19.jpg)
Inferring effects for bytecodes
8
-formal
rpt
-
rpt
-formal pt
rpt
load rpt
store rpt
pmap
lh.ν = l
method invocations
![Page 20: Mostly-functional behavior in Java programs](https://reader030.fdocuments.in/reader030/viewer/2022012121/61dda05b50c201441c3cbb56/html5/thumbnails/20.jpg)
Inferring effects for bytecodes
8
-formal
rpt
-
rpt
-formal pt
rpt
load rpt
store rpt
pmap
lh.ν = l
method invocations
Regions we consider: ρthis, ρ0..n, and ⊤.
![Page 21: Mostly-functional behavior in Java programs](https://reader030.fdocuments.in/reader030/viewer/2022012121/61dda05b50c201441c3cbb56/html5/thumbnails/21.jpg)
Inferring effects for bytecodes
8
-formal
rpt
-
rpt
-formal pt
rpt
load rpt
store rpt
pmap
lh.ν = l
method invocations
Regions we consider: ρthis, ρ0..n, and ⊤.
![Page 22: Mostly-functional behavior in Java programs](https://reader030.fdocuments.in/reader030/viewer/2022012121/61dda05b50c201441c3cbb56/html5/thumbnails/22.jpg)
Inferring effects for bytecodes
8
-formal
rpt
-
rpt
-formal pt
rpt
load rpt
store rpt
pmapmethod invocations
Regions we consider: ρthis, ρ0..n, and ⊤.
![Page 23: Mostly-functional behavior in Java programs](https://reader030.fdocuments.in/reader030/viewer/2022012121/61dda05b50c201441c3cbb56/html5/thumbnails/23.jpg)
Inferring effects for bytecodes
8
-formal
rpt
-
rpt
-formal pt
rpt
load rpt
store rpt
pmapmethod invocations
Regions we consider: ρthis, ρ0..n, and ⊤.
![Page 24: Mostly-functional behavior in Java programs](https://reader030.fdocuments.in/reader030/viewer/2022012121/61dda05b50c201441c3cbb56/html5/thumbnails/24.jpg)
Inferring effects for bytecodes
8
-formal
rpt
-
rpt
-formal pt
rpt
load rpt
store rpt
pmap
Regions we consider: ρthis, ρ0..n, and ⊤.
![Page 25: Mostly-functional behavior in Java programs](https://reader030.fdocuments.in/reader030/viewer/2022012121/61dda05b50c201441c3cbb56/html5/thumbnails/25.jpg)
Extending the simple system
9
Type-and-effect system and type-based analysis
Initialization effects
Quiescing field inference
Degrees of method purity
![Page 26: Mostly-functional behavior in Java programs](https://reader030.fdocuments.in/reader030/viewer/2022012121/61dda05b50c201441c3cbb56/html5/thumbnails/26.jpg)
Extending the simple system
9
Type-and-effect system and type-based analysis
Initialization effects
Quiescing field inference
Degrees of method purity
![Page 27: Mostly-functional behavior in Java programs](https://reader030.fdocuments.in/reader030/viewer/2022012121/61dda05b50c201441c3cbb56/html5/thumbnails/27.jpg)
Extending the simple system
9
Type-and-effect system and type-based analysis
Initialization effects
Quiescing field inference
Degrees of method purity
![Page 28: Mostly-functional behavior in Java programs](https://reader030.fdocuments.in/reader030/viewer/2022012121/61dda05b50c201441c3cbb56/html5/thumbnails/28.jpg)
Forecast
• A simple object-oriented effects system
• Initializers and initialization effects
• Final fields and eventual immutability
• Inferring quiescing fields
• Evaluating quiescing field inference
10
![Page 29: Mostly-functional behavior in Java programs](https://reader030.fdocuments.in/reader030/viewer/2022012121/61dda05b50c201441c3cbb56/html5/thumbnails/29.jpg)
Motivating initialization effects
11
class IntBox {private int i;IntBox(int j) {
this.i = j;}
int get() {return i;
}}
![Page 30: Mostly-functional behavior in Java programs](https://reader030.fdocuments.in/reader030/viewer/2022012121/61dda05b50c201441c3cbb56/html5/thumbnails/30.jpg)
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
![Page 31: Mostly-functional behavior in Java programs](https://reader030.fdocuments.in/reader030/viewer/2022012121/61dda05b50c201441c3cbb56/html5/thumbnails/31.jpg)
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
![Page 32: Mostly-functional behavior in Java programs](https://reader030.fdocuments.in/reader030/viewer/2022012121/61dda05b50c201441c3cbb56/html5/thumbnails/32.jpg)
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
![Page 33: Mostly-functional behavior in Java programs](https://reader030.fdocuments.in/reader030/viewer/2022012121/61dda05b50c201441c3cbb56/html5/thumbnails/33.jpg)
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;
}
![Page 34: Mostly-functional behavior in Java programs](https://reader030.fdocuments.in/reader030/viewer/2022012121/61dda05b50c201441c3cbb56/html5/thumbnails/34.jpg)
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 objects (Leavens et al.; Rugina and Cherem)
![Page 35: Mostly-functional behavior in Java programs](https://reader030.fdocuments.in/reader030/viewer/2022012121/61dda05b50c201441c3cbb56/html5/thumbnails/35.jpg)
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
![Page 36: Mostly-functional behavior in Java programs](https://reader030.fdocuments.in/reader030/viewer/2022012121/61dda05b50c201441c3cbb56/html5/thumbnails/36.jpg)
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 object during its creation.
An initializer is a method that executes on an object during the dynamic lifetime of its constructor.
![Page 37: Mostly-functional behavior in Java programs](https://reader030.fdocuments.in/reader030/viewer/2022012121/61dda05b50c201441c3cbb56/html5/thumbnails/37.jpg)
Inferring initializer methods
14
![Page 38: Mostly-functional behavior in Java programs](https://reader030.fdocuments.in/reader030/viewer/2022012121/61dda05b50c201441c3cbb56/html5/thumbnails/38.jpg)
Inferring initializer methods
14
![Page 39: Mostly-functional behavior in Java programs](https://reader030.fdocuments.in/reader030/viewer/2022012121/61dda05b50c201441c3cbb56/html5/thumbnails/39.jpg)
Inferring initializer methods
14
this
this
this
this
![Page 40: Mostly-functional behavior in Java programs](https://reader030.fdocuments.in/reader030/viewer/2022012121/61dda05b50c201441c3cbb56/html5/thumbnails/40.jpg)
Inferring initializer methods
14
this
this
this
this
A constructor is an initializer on its receiver object.
![Page 41: Mostly-functional behavior in Java programs](https://reader030.fdocuments.in/reader030/viewer/2022012121/61dda05b50c201441c3cbb56/html5/thumbnails/41.jpg)
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.
![Page 42: Mostly-functional behavior in Java programs](https://reader030.fdocuments.in/reader030/viewer/2022012121/61dda05b50c201441c3cbb56/html5/thumbnails/42.jpg)
Inferring initialization effects
15
Initialization effects are writes to fields of this that occur within an initializer.
![Page 43: Mostly-functional behavior in Java programs](https://reader030.fdocuments.in/reader030/viewer/2022012121/61dda05b50c201441c3cbb56/html5/thumbnails/43.jpg)
Forecast
• A simple object-oriented effects system
• Initializers and initialization effects
• Final fields and eventual immutability
• Inferring quiescing fields
• Evaluating quiescing field inference
16
![Page 44: Mostly-functional behavior in Java programs](https://reader030.fdocuments.in/reader030/viewer/2022012121/61dda05b50c201441c3cbb56/html5/thumbnails/44.jpg)
class IntBox {private int i;IntBox(int j) {
this.i = j;}
int get() {return i;
}}
Final fields
17
![Page 45: Mostly-functional behavior in Java programs](https://reader030.fdocuments.in/reader030/viewer/2022012121/61dda05b50c201441c3cbb56/html5/thumbnails/45.jpg)
Final fields
17
class IntBox {private final int i;IntBox(int j) {
this.i = j;}
int get() {return i;
}}
![Page 46: Mostly-functional behavior in Java programs](https://reader030.fdocuments.in/reader030/viewer/2022012121/61dda05b50c201441c3cbb56/html5/thumbnails/46.jpg)
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
![Page 47: Mostly-functional behavior in Java programs](https://reader030.fdocuments.in/reader030/viewer/2022012121/61dda05b50c201441c3cbb56/html5/thumbnails/47.jpg)
Final fields
17
class IntBox {private final int i;IntBox(int j) {
this.i = j;}
int get() {return i;
}}
![Page 48: Mostly-functional behavior in Java programs](https://reader030.fdocuments.in/reader030/viewer/2022012121/61dda05b50c201441c3cbb56/html5/thumbnails/48.jpg)
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;
}}
![Page 49: Mostly-functional behavior in Java programs](https://reader030.fdocuments.in/reader030/viewer/2022012121/61dda05b50c201441c3cbb56/html5/thumbnails/49.jpg)
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.
![Page 50: Mostly-functional behavior in Java programs](https://reader030.fdocuments.in/reader030/viewer/2022012121/61dda05b50c201441c3cbb56/html5/thumbnails/50.jpg)
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
![Page 51: Mostly-functional behavior in Java programs](https://reader030.fdocuments.in/reader030/viewer/2022012121/61dda05b50c201441c3cbb56/html5/thumbnails/51.jpg)
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
![Page 52: Mostly-functional behavior in Java programs](https://reader030.fdocuments.in/reader030/viewer/2022012121/61dda05b50c201441c3cbb56/html5/thumbnails/52.jpg)
Forecast
• A simple object-oriented effects system
• Initializers and initialization effects
• Final fields and eventual immutability
• Inferring quiescing fields
• Evaluating quiescing field inference
20
![Page 53: Mostly-functional behavior in Java programs](https://reader030.fdocuments.in/reader030/viewer/2022012121/61dda05b50c201441c3cbb56/html5/thumbnails/53.jpg)
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
![Page 54: Mostly-functional behavior in Java programs](https://reader030.fdocuments.in/reader030/viewer/2022012121/61dda05b50c201441c3cbb56/html5/thumbnails/54.jpg)
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
![Page 55: Mostly-functional behavior in Java programs](https://reader030.fdocuments.in/reader030/viewer/2022012121/61dda05b50c201441c3cbb56/html5/thumbnails/55.jpg)
Forecast
• A simple object-oriented effects system
• Initializers and initialization effects
• Final fields and eventual immutability
• Inferring quiescing fields
• Evaluating quiescing field inference
23
![Page 56: Mostly-functional behavior in Java programs](https://reader030.fdocuments.in/reader030/viewer/2022012121/61dda05b50c201441c3cbb56/html5/thumbnails/56.jpg)
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
![Page 57: Mostly-functional behavior in Java programs](https://reader030.fdocuments.in/reader030/viewer/2022012121/61dda05b50c201441c3cbb56/html5/thumbnails/57.jpg)
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
![Page 58: Mostly-functional behavior in Java programs](https://reader030.fdocuments.in/reader030/viewer/2022012121/61dda05b50c201441c3cbb56/html5/thumbnails/58.jpg)
Static prevalence of final and quiescing fields
26
0
25
50
75
100
antlr bloat eclipse hsqldb jython luindex pmd
Final fieldsQuiescing fields
![Page 59: Mostly-functional behavior in Java programs](https://reader030.fdocuments.in/reader030/viewer/2022012121/61dda05b50c201441c3cbb56/html5/thumbnails/59.jpg)
0
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 fieldsReads from quiescing fields
![Page 60: Mostly-functional behavior in Java programs](https://reader030.fdocuments.in/reader030/viewer/2022012121/61dda05b50c201441c3cbb56/html5/thumbnails/60.jpg)
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
![Page 61: Mostly-functional behavior in Java programs](https://reader030.fdocuments.in/reader030/viewer/2022012121/61dda05b50c201441c3cbb56/html5/thumbnails/61.jpg)
http://www.cs.wisc.edu/~willb/
29