Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Late Data Layout - Initial Presentation

456d1d6154efe50e950b65f966f63a50?s=47 Vlad Ureche
February 11, 2014

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

456d1d6154efe50e950b65f966f63a50?s=128

Vlad Ureche

February 11, 2014
Tweet

Transcript

  1. 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
  2. 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
  3. 3 scala-miniboxing.org We all like generics We all like generics

  4. 4 scala-miniboxing.org We all like generics We all like generics

    a trivial example a trivial example
  5. 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. 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. 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. 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. 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
  10. 10 scala-miniboxing.org Specialization Specialization generates too much code generates too

    much code
  11. 11 scala-miniboxing.org Specialization Specialization generates too much code generates too

    much code def identity[T](t: T): T = t
  12. 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. 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
  14. 14 scala-miniboxing.org Miniboxing Miniboxing

  15. 15 scala-miniboxing.org Miniboxing Miniboxing reduces the variants reduces the variants

  16. 16 scala-miniboxing.org Miniboxing Miniboxing reduces the variants reduces the variants

    by using something like a tagged union TAG DATA (VALUE)
  17. 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. 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. 19 scala-miniboxing.org Miniboxing Miniboxing reduces the variants reduces the variants

    by using something like a tagged union TAG DATA (VALUE)
  20. 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. 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. 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. 23 scala-miniboxing.org Miniboxing Miniboxing reduces the variants reduces the variants

    by using something like a tagged union TAG DATA (VALUE)
  24. 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. 25 scala-miniboxing.org Miniboxing Miniboxing let's revisit `def identity` let's revisit

    `def identity` def identity[T](t: T): T = t
  26. 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. 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. 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. 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. 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. 31 scala-miniboxing.org WE ARE HERE Transform Boil the frog Advantages

    Other uses Conclusion
  32. 32 scala-miniboxing.org Transforming Transforming

  33. 33 scala-miniboxing.org Transforming Transforming could certainly be easier could certainly

    be easier
  34. 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. 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. 36 scala-miniboxing.org Transforming Transforming def foo_J(t: T): T = if

    (...) t else ??? could certainly be easier could certainly be easier
  37. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 53 scala-miniboxing.org WE ARE HERE Transform Boil the frog Advantages

    Other uses Conclusion
  54. 54 scala-miniboxing.org Transforming Transforming

  55. 55 scala-miniboxing.org Transforming Transforming is like boiling a frog is

    like boiling a frog
  56. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 79 scala-miniboxing.org Transforming Transforming is like boiling a frog is

    like boiling a frog • the three steps are:
  80. 80 scala-miniboxing.org Transforming Transforming is like boiling a frog is

    like boiling a frog • the three steps are: T =:= @storage T
  81. 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. 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. 83 scala-miniboxing.org WE ARE HERE Transform Boil the frog Advantages

    Other uses Conclusion
  84. 84 scala-miniboxing.org Boiling the frog Boiling the frog

  85. 85 scala-miniboxing.org Boiling the frog Boiling the frog T T

    Long
  86. 86 scala-miniboxing.org Boiling the frog Boiling the frog is Late

    Data Layout is Late Data Layout T T Long
  87. 87 scala-miniboxing.org Boiling the frog Boiling the frog is Late

    Data Layout is Late Data Layout
  88. 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. 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. 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. 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. 92 scala-miniboxing.org WE ARE HERE Transform Boil the frog Advantages

    Other uses Conclusion
  93. 93 scala-miniboxing.org Late Data Layout Late Data Layout is general

    is general scala.Int int java.lang.Integer Autoboxing
  94. 94 scala-miniboxing.org Late Data Layout Late Data Layout is general

    is general Complex Complex (s.Float, s.Float) Autoboxing Value Classes
  95. 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. 96 scala-miniboxing.org Late Data Layout Late Data Layout is general

    is general T T Long Autoboxing Value Classes Encoding Miniboxing
  97. 97 scala-miniboxing.org Transform Boil the frog Advantages Other uses Conclusion

    WE ARE HERE
  98. 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