Share
Embed
Late Data Layout - Initial Presentation
This presentation shows how the miniboxing plugin transforms complex code structures without invalidating any of the Scala tree invariants.
Project website: http://scala-ldl.org
Vlad Ureche
February 11, 2014
Transcript
98 slides
-
scala-miniboxing.org Late Data Layout Late Data Layout or how to
boil a frog without it noticing LAMP, 11th of February 2014 Vlad Ureche Cristian Talau Martin Odersky -
scala-miniboxing.org Late Data Layout Late Data Layout or how to
boil a frog without it noticing LAMP, 11th of February 2014 Vlad Ureche Cristian Talau Martin Odersky Frog clipart credits: www.frog-life-cycle.com -
4 scala-miniboxing.org We all like generics We all like generics
a trivial example a trivial example -
5 scala-miniboxing.org We all like generics We all like generics
def identity[T](t: T): T = t a trivial example a trivial example -
6 scala-miniboxing.org We all like generics We all like generics
def identity[T](t: T): T = t • will take any type and a trivial example a trivial example -
7 scala-miniboxing.org We all like generics We all like generics
def identity[T](t: T): T = t • will take any type and • will return that same type a trivial example a trivial example -
8 scala-miniboxing.org We all like generics We all like generics
def identity[T](t: T): T = t a trivial example a trivial example but under erasure: def identity(t: Any): Any = t -
9 scala-miniboxing.org We all like generics We all like generics
def identity[T](t: T): T = t a trivial example a trivial example but under erasure: def identity(t: Any): Any = t Any indicates a by- reference parameter -
11 scala-miniboxing.org Specialization Specialization generates too much code generates too
much code def identity[T](t: T): T = t -
12 scala-miniboxing.org Specialization Specialization generates too much code generates too
much code def identity[T](t: T): T = t def identity_V(t: Unit): Unit = t def identity_Z(t: Boolean): Boolean = t def identity_B(t: Byte): Byte = t def identity_C(t: Char): Char = t def identity_S(t: Short): Short = t def identity_I(t: Int): Int = t def identity_J(t: Long): Long = t def identity_F(t: Float): Float = t def identity_D(t: Double): Double = t -
13 scala-miniboxing.org Specialization Specialization generates too much code generates too
much code def identity[T](t: T): T = t def identity_V(t: Unit): Unit = t def identity_Z(t: Boolean): Boolean = t def identity_B(t: Byte): Byte = t def identity_C(t: Char): Char = t def identity_S(t: Short): Short = t def identity_I(t: Int): Int = t def identity_J(t: Long): Long = t def identity_F(t: Float): Float = t def identity_D(t: Double): Double = t Generates 10 times the original code -
16 scala-miniboxing.org Miniboxing Miniboxing reduces the variants reduces the variants
by using something like a tagged union TAG DATA (VALUE) -
17 scala-miniboxing.org Miniboxing Miniboxing reduces the variants reduces the variants
by using something like a tagged union TAG DATA (VALUE) Stores the original type -
18 scala-miniboxing.org Miniboxing Miniboxing reduces the variants reduces the variants
by using something like a tagged union TAG DATA (VALUE) Stores the original type Stores the encoded value in a long integer -
19 scala-miniboxing.org Miniboxing Miniboxing reduces the variants reduces the variants
by using something like a tagged union TAG DATA (VALUE) -
20 scala-miniboxing.org Miniboxing Miniboxing reduces the variants reduces the variants
by using something like a tagged union TAG DATA (VALUE) BOOL 0x0 false = -
21 scala-miniboxing.org Miniboxing Miniboxing reduces the variants reduces the variants
by using something like a tagged union TAG DATA (VALUE) BOOL 0x0 false = BOOL 0x1 true = -
22 scala-miniboxing.org Miniboxing Miniboxing reduces the variants reduces the variants
by using something like a tagged union TAG DATA (VALUE) BOOL 0x0 false = BOOL 0x1 true = INT 0x2A 42 = -
23 scala-miniboxing.org Miniboxing Miniboxing reduces the variants reduces the variants
by using something like a tagged union TAG DATA (VALUE) -
24 scala-miniboxing.org Miniboxing Miniboxing reduces the variants reduces the variants
by using something like a tagged union TAG DATA (VALUE) and using the static type information – tags are attached to code, not to values -
25 scala-miniboxing.org Miniboxing Miniboxing let's revisit `def identity` let's revisit
`def identity` def identity[T](t: T): T = t -
26 scala-miniboxing.org Miniboxing Miniboxing let's revisit `def identity` let's revisit
`def identity` def identity[T](t: T): T = t def identity_J(T_tag: Byte, t: Long): Long -
27 scala-miniboxing.org Miniboxing Miniboxing let's revisit `def identity` let's revisit
`def identity` def identity[T](t: T): T = t def identity_J(T_tag: Byte, t: Long): Long TAG -
28 scala-miniboxing.org Miniboxing Miniboxing let's revisit `def identity` let's revisit
`def identity` def identity[T](t: T): T = t def identity_J(T_tag: Byte, t: Long): Long TAG DATA (VALUE) -
29 scala-miniboxing.org Miniboxing Miniboxing let's revisit `def identity` let's revisit
`def identity` def identity[T](t: T): T = t def identity_J(T_tag: Byte, t: Long): Long TAG DATA (VALUE) T_tag corresponds to the type parameter, instead of the values being passed around. -
30 scala-miniboxing.org Miniboxing Miniboxing let's revisit `def identity` let's revisit
`def identity` def identity[T](t: T): T = t def identity_J(T_tag: Byte, t: Long): Long TAG DATA (VALUE) T_tag corresponds to the type parameter, instead of the values being passed around. Tag hoisting -
31 scala-miniboxing.org WE ARE HERE Transform Boil the frog Advantages
Other uses Conclusion -
34 scala-miniboxing.org Transforming Transforming class C[@miniboxed T] { def foo(t:
T): T = if (...) t else ??? } could certainly be easier could certainly be easier -
35 scala-miniboxing.org Transforming Transforming class C[@miniboxed T] { def foo(t:
T): T = if (...) t else ??? } could certainly be easier could certainly be easier How to generate foo_J, the miniboxed version of foo? -
36 scala-miniboxing.org Transforming Transforming def foo_J(t: T): T = if
(...) t else ??? could certainly be easier could certainly be easier -
37 scala-miniboxing.org Transforming Transforming def foo_J(t: T): T = if
(...) t else ??? could certainly be easier could certainly be easier • change T to Long, one variable at a time -
38 scala-miniboxing.org Transforming Transforming def foo_J(t: T): T = if
(...) t else ??? could certainly be easier could certainly be easier • change T to Long, one variable at a time • patch up the tree -
39 scala-miniboxing.org Transforming Transforming def foo_J(t: Long): T = if
(...) t // error: found: Long req'd: T else ??? could certainly be easier could certainly be easier • change T to Long, one variable at a time • patch up the tree -
40 scala-miniboxing.org Transforming Transforming def foo_J(t: Long): T = if
(...) minibox2box(T_Tag, t) else ??? could certainly be easier could certainly be easier • change T to Long, one variable at a time • patch up the tree -
41 scala-miniboxing.org Transforming Transforming def foo_J(t: Long): T = if
(...) minibox2box(T_Tag, t) else ??? could certainly be easier could certainly be easier • change T to Long, one variable at a time • patch up the tree -
42 scala-miniboxing.org Transforming Transforming def foo_J(t: Long): Long = if
(...) minibox2box(T_Tag, t) else ??? // error: found: T req'd: Long could certainly be easier could certainly be easier • change T to Long, one variable at a time • patch up the tree -
43 scala-miniboxing.org Transforming Transforming def foo_J(t: Long): Long = box2minibox(T_Tag,
if (...) minibox2box(T_Tag, t) else ???) could certainly be easier could certainly be easier • change T to Long, one variable at a time • patch up the tree -
44 scala-miniboxing.org Transforming Transforming def foo_J(t: Long): Long = box2minibox(T_Tag,
if (...) minibox2box(T_Tag, t) else ???) could certainly be easier could certainly be easier -
45 scala-miniboxing.org Transforming Transforming def foo_J(t: Long): Long = box2minibox(T_Tag,
if (...) minibox2box(T_Tag, t) else ???) could certainly be easier could certainly be easier • performance? – we'd be better off boxing everywhere -
46 scala-miniboxing.org Peephole optimization Peephole optimization def foo_J(t: Long): Long
= box2minibox(T_Tag, if (...) minibox2box(T_Tag, t) else ???) to save the day to save the day -
47 scala-miniboxing.org Peephole optimization Peephole optimization def foo_J(t: Long): Long
= box2minibox(T_Tag, if (...) minibox2box(T_Tag, t) else ???) to save the day to save the day • special case for if – and blocks, and array operations and and and -
48 scala-miniboxing.org Peephole optimization Peephole optimization def foo_J(t: Long): Long
= if (...) box2minibox(minibox2box(t)) else box2minibox(???) to save the day to save the day -
49 scala-miniboxing.org Peephole optimization Peephole optimization def foo_J(t: Long): Long
= if (...) box2minibox(minibox2box(t)) else box2minibox(???) to save the day to save the day • special case for nested corecions -
50 scala-miniboxing.org Peephole optimization Peephole optimization def foo_J(t: Long): Long
= if (...) t else box2minibox(???) to save the day to save the day -
51 scala-miniboxing.org Peephole optimization Peephole optimization def foo_J(t: Long): Long
= if (...) t else box2minibox(???) to save the day to save the day • contains too many special cases -
52 scala-miniboxing.org Peephole optimization Peephole optimization def foo_J(t: Long): Long
= if (...) t else box2minibox(???) to save the day to save the day • contains too many special cases • tedious and error-prone to write -
53 scala-miniboxing.org WE ARE HERE Transform Boil the frog Advantages
Other uses Conclusion -
55 scala-miniboxing.org Transforming Transforming is like boiling a frog is
like boiling a frog -
56 scala-miniboxing.org Transforming Transforming is like boiling a frog is
like boiling a frog • if you put it in hot water – it jumps out right away -
57 scala-miniboxing.org Transforming Transforming is like boiling a frog is
like boiling a frog • if you put it in hot water – it jumps out right away error: found: T req'd: Long -
58 scala-miniboxing.org Transforming Transforming is like boiling a frog is
like boiling a frog • if you put it in hot water – it jumps out right away – so you need special precautions • patching up the tree + peephole optimization error: found: T req'd: Long -
59 scala-miniboxing.org Transforming Transforming is like boiling a frog is
like boiling a frog • if you put it in hot water – it jumps out right away – so you need special precautions • patching up the tree + peephole optimization • if you put it in cold water – it will like it there :) error: found: T req'd: Long -
60 scala-miniboxing.org Transforming Transforming is like boiling a frog is
like boiling a frog • if you put it in hot water – it jumps out right away – so you need special precautions • patching up the tree + peephole optimization • if you put it in cold water – it will like it there :) – and then you slowly heat up the water :D error: found: T req'd: Long -
61 scala-miniboxing.org Transforming Transforming is like boiling a frog is
like boiling a frog def foo_J(t: T): T = if (...) t else ??? -
62 scala-miniboxing.org Transforming Transforming is like boiling a frog is
like boiling a frog • you put it in cold water def foo_J(t: T): T = if (...) t else ??? -
63 scala-miniboxing.org Transforming Transforming is like boiling a frog is
like boiling a frog • you put it in cold water def foo_J(t: @storage T): @storage T = if (...) t else ??? -
64 scala-miniboxing.org Transforming Transforming is like boiling a frog is
like boiling a frog • you put it in cold water def foo_J(t: @storage T): @storage T = if (...) t else ??? T =:= @storage T -
65 scala-miniboxing.org Transforming Transforming is like boiling a frog is
like boiling a frog • you can easily do rewiring – foo(t) foo_J(t) → , no coercions needed def foo_J(t: @storage T): @storage T = if (...) t else ??? -
66 scala-miniboxing.org Transforming Transforming is like boiling a frog is
like boiling a frog def foo_J(t: @storage T): @storage T = if (...) t else ??? -
67 scala-miniboxing.org Transforming Transforming is like boiling a frog is
like boiling a frog • then you heat up the water def foo_J(t: @storage T): @storage T = if (...) t else ??? -
68 scala-miniboxing.org Transforming Transforming is like boiling a frog is
like boiling a frog • then you heat up the water def foo_J(t: @storage T): @storage T = if (...) t else ??? T =/= @storage T -
69 scala-miniboxing.org Transforming Transforming is like boiling a frog is
like boiling a frog • then you heat up the water def foo_J(t: @storage T): @storage T = if (...) t else ??? T =/= @storage T retypecheck expected type: @storage T -
70 scala-miniboxing.org Transforming Transforming is like boiling a frog is
like boiling a frog • then you heat up the water def foo_J(t: @storage T): @storage T = if (...) t else ??? T =/= @storage T retypecheck expected type: @storage T -
71 scala-miniboxing.org Transforming Transforming is like boiling a frog is
like boiling a frog • then you heat up the water def foo_J(t: @storage T): @storage T = if (...) t else ??? T =/= @storage T retypecheck expected type: @storage T OK -
72 scala-miniboxing.org Transforming Transforming is like boiling a frog is
like boiling a frog • then you heat up the water def foo_J(t: @storage T): @storage T = if (...) t else ??? T =/= @storage T retypecheck expected type: @storage T -
73 scala-miniboxing.org Transforming Transforming is like boiling a frog is
like boiling a frog • then you heat up the water def foo_J(t: @storage T): @storage T = if (...) t else ??? T =/= @storage T retypecheck expected type: @storage T NO -
74 scala-miniboxing.org Transforming Transforming is like boiling a frog is
like boiling a frog • then you heat up the water def foo_J(t: @storage T): @storage T = if (...) t else box2minibox(???) T =/= @storage T retypecheck expected type: @storage T -
75 scala-miniboxing.org Transforming Transforming is like boiling a frog is
like boiling a frog • then you heat up the water def foo_J(t: @storage T): @storage T = if (...) t else box2minibox(???) T =/= @storage T retypecheck expected type: @storage T OK -
76 scala-miniboxing.org Transforming Transforming is like boiling a frog is
like boiling a frog • then you boil the frog def foo_J(t: @storage T): @storage T = if (...) t else box2minibox(???) -
77 scala-miniboxing.org Transforming Transforming is like boiling a frog is
like boiling a frog • then you boil the frog def foo_J(t: @storage T): @storage T = if (...) t else box2minibox(???) @storage T Long → -
78 scala-miniboxing.org Transforming Transforming is like boiling a frog is
like boiling a frog • then you boil the frog def foo_J(t: Long): Long = if (...) t else box2minibox(T_Tag, ???) @storage T Long → -
79 scala-miniboxing.org Transforming Transforming is like boiling a frog is
like boiling a frog • the three steps are: -
80 scala-miniboxing.org Transforming Transforming is like boiling a frog is
like boiling a frog • the three steps are: T =:= @storage T -
81 scala-miniboxing.org Transforming Transforming is like boiling a frog is
like boiling a frog • the three steps are: T =/= @storage T T =:= @storage T -
82 scala-miniboxing.org Transforming Transforming is like boiling a frog is
like boiling a frog • the three steps are: @storage T Long → T =/= @storage T T =:= @storage T -
83 scala-miniboxing.org WE ARE HERE Transform Boil the frog Advantages
Other uses Conclusion -
86 scala-miniboxing.org Boiling the frog Boiling the frog is Late
Data Layout is Late Data Layout T T Long -
87 scala-miniboxing.org Boiling the frog Boiling the frog is Late
Data Layout is Late Data Layout -
88 scala-miniboxing.org Boiling the frog Boiling the frog is Late
Data Layout is Late Data Layout • choice made by injecting annotations • easy to rewrite the tree • coercions inserted late and on-demand -
89 scala-miniboxing.org Boiling the frog Boiling the frog is Late
Data Layout is Late Data Layout • choice made by injecting annotations • easy to rewrite the tree • coercions inserted late and on-demand def bar: Unit = { this.foo(...) ... } -
90 scala-miniboxing.org Boiling the frog Boiling the frog is Late
Data Layout is Late Data Layout • choice made by injecting annotations • easy to rewrite the tree • coercions inserted late and on-demand def bar: Unit = { this.foo(...) ... } this.foo_J(...) -
91 scala-miniboxing.org Boiling the frog Boiling the frog is Late
Data Layout is Late Data Layout • choice made by injecting annotations • easy to rewrite the tree • coercions inserted late and on-demand def bar: Unit = { this.foo(...) ... } this.foo_J(...) No coercion necessary @storage T <: WildcardType -
92 scala-miniboxing.org WE ARE HERE Transform Boil the frog Advantages
Other uses Conclusion -
93 scala-miniboxing.org Late Data Layout Late Data Layout is general
is general scala.Int int java.lang.Integer Autoboxing -
94 scala-miniboxing.org Late Data Layout Late Data Layout is general
is general Complex Complex (s.Float, s.Float) Autoboxing Value Classes -
95 scala-miniboxing.org Late Data Layout Late Data Layout is general
is general (s.Int, s.Int) s.Long (s.Int, s.Int) Autoboxing Value Classes Encoding -
96 scala-miniboxing.org Late Data Layout Late Data Layout is general
is general T T Long Autoboxing Value Classes Encoding Miniboxing -
98 scala-miniboxing.org Conclusion Conclusion Late Data Layout Late Data Layout
Autoboxing Value Classes Encoding Miniboxing • type-driven transformation – heavy-lifting in the type system • generalization – of existing transformations – not tied to erasure • formalization – thinking about it now