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

Late Data Layout - Initial Presentation

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

Vlad Ureche

February 11, 2014
Tweet

More Decks by Vlad Ureche

Other Decks in Technology

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. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 16 scala-miniboxing.org Miniboxing Miniboxing reduces the variants reduces the variants

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

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

    by using something like a tagged union TAG DATA (VALUE)
  18. 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
  19. 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
  20. 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
  21. 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)
  22. 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.
  23. 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
  24. 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
  25. 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?
  26. 36 scala-miniboxing.org Transforming Transforming def foo_J(t: T): T = if

    (...) t else ??? could certainly be easier could certainly be easier
  27. 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
  28. 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
  29. 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
  30. 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
  31. 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
  32. 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
  33. 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
  34. 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
  35. 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
  36. 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
  37. 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
  38. 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
  39. 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
  40. 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
  41. 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
  42. 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
  43. 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
  44. 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
  45. 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
  46. 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
  47. 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
  48. 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 ???
  49. 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 ???
  50. 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 ???
  51. 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
  52. 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 ???
  53. 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 ???
  54. 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 ???
  55. 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
  56. 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
  57. 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
  58. 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
  59. 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
  60. 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
  61. 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
  62. 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
  63. 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(???)
  64. 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 →
  65. 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 →
  66. 80 scala-miniboxing.org Transforming Transforming is like boiling a frog is

    like boiling a frog • the three steps are: T =:= @storage T
  67. 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
  68. 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
  69. 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
  70. 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(...) ... }
  71. 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(...)
  72. 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
  73. 93 scala-miniboxing.org Late Data Layout Late Data Layout is general

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

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

    is general T T Long Autoboxing Value Classes Encoding Miniboxing
  77. 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