$30 off During Our Annual Pro Sale. View Details »

What do auto(un)boxing, specialization and value classes have in common?

What do auto(un)boxing, specialization and value classes have in common?

Late Data Layout presentation at the Scala Bay Area Meetup (thanks Vlad Patryshev!): What do auto(un)boxing, specialization and value classes have in common?

The recording is available on youtube:
https://www.youtube.com/watch?v=HPqNmaAUAJc

Project website: http://scala-miniboxing.org

Vlad Ureche

August 01, 2014
Tweet

More Decks by Vlad Ureche

Other Decks in Programming

Transcript

  1. scala-miniboxing.org 1st of August 2014
    Scala Bay Area Meetup
    Linkedin HQ, Mountain View

    View Slide

  2. scala-miniboxing.org
    Vlad URECHE
    PhD student in the Scala Team @ EPFL
    Miniboxing guy. Also worked on
    specialization, the backend and scaladoc.
    @
    @VladUreche
    @VladUreche
    [email protected]

    View Slide

  3. scala-miniboxing.org
    What do auto(un)boxing,
    specialization and value
    classes have in common?

    View Slide

  4. scala-miniboxing.org
    What do auto(un)boxing,
    specialization and value
    classes have in common?
    What are they?

    View Slide

  5. 5
    scala-miniboxing.org
    Auto(un)boxing
    Auto(un)boxing
    def identity[T](t: T): T = t

    View Slide

  6. 6
    scala-miniboxing.org
    Auto(un)boxing
    Auto(un)boxing
    def identity[T](t: T): T = t
    scalac / javac

    View Slide

  7. 7
    scala-miniboxing.org
    Auto(un)boxing
    Auto(un)boxing
    def identity[T](t: T): T = t
    def identity(t: Object): Object = t
    scalac / javac

    View Slide

  8. 8
    scala-miniboxing.org
    Auto(un)boxing
    Auto(un)boxing
    def identity[T](t: T): T = t
    def identity(t: Object): Object = t
    The process is called erasure,
    and is the simplest translation
    of generics to bytecode.
    scalac / javac

    View Slide

  9. 9
    scala-miniboxing.org
    Auto(un)boxing
    Auto(un)boxing
    identity(5)

    View Slide

  10. 10
    scala-miniboxing.org
    Auto(un)boxing
    Auto(un)boxing
    identity(5)
    scalac / javac

    View Slide

  11. 11
    scala-miniboxing.org
    Auto(un)boxing
    Auto(un)boxing
    identity(5)
    identity(j.l.Integer.valueOf(5)).intValue
    scalac / javac

    View Slide

  12. 12
    scala-miniboxing.org
    Auto(un)boxing
    Auto(un)boxing
    identity(5)
    identity(j.l.Integer.valueOf(5)).intValue
    scalac / javac
    Object representation

    View Slide

  13. 13
    scala-miniboxing.org
    Auto(un)boxing
    Auto(un)boxing
    identity(5)
    identity(j.l.Integer.valueOf(5)).intValue
    scalac / javac inflates heap
    requirements
    Object representation

    View Slide

  14. 14
    scala-miniboxing.org
    Auto(un)boxing
    Auto(un)boxing
    identity(5)
    identity(j.l.Integer.valueOf(5)).intValue
    scalac / javac produces
    garbage
    inflates heap
    requirements
    Object representation

    View Slide

  15. 15
    scala-miniboxing.org
    Auto(un)boxing
    Auto(un)boxing
    identity(5)
    identity(j.l.Integer.valueOf(5)).intValue
    scalac / javac produces
    garbage
    inflates heap
    requirements
    indirect
    (slow) access
    to the value
    Object representation

    View Slide

  16. 16
    scala-miniboxing.org
    Auto(un)boxing
    Auto(un)boxing
    identity(5)
    identity(j.l.Integer.valueOf(5)).intValue
    scalac / javac produces
    garbage
    breaks locality
    guarantees
    inflates heap
    requirements
    indirect
    (slow) access
    to the value
    Object representation

    View Slide

  17. 17
    scala-miniboxing.org
    Auto(un)boxing
    Auto(un)boxing
    val five: Int = 5

    View Slide

  18. 18
    scala-miniboxing.org
    Auto(un)boxing
    Auto(un)boxing
    val five: Int = 5
    scala.Int behaves like an object
    (has methods, can be used with generics)

    View Slide

  19. 19
    scala-miniboxing.org
    Auto(un)boxing
    Auto(un)boxing
    val five: Int = 5
    scalac
    scala.Int behaves like an object
    (has methods, can be used with generics)

    View Slide

  20. 20
    scala-miniboxing.org
    Auto(un)boxing
    Auto(un)boxing
    val five: Int = 5
    val five: int = 5
    scalac
    scala.Int behaves like an object
    (has methods, can be used with generics)

    View Slide

  21. 21
    scala-miniboxing.org
    Auto(un)boxing
    Auto(un)boxing
    val five: Int = 5
    val five: int = 5
    scalac
    Unboxed integer
    scala.Int behaves like an object
    (has methods, can be used with generics)

    View Slide

  22. 22
    scala-miniboxing.org
    Auto(un)boxing
    Auto(un)boxing
    val five: Int = 5
    val five: int = 5
    scalac
    Unboxed integer
    five + 3
    scala.Int behaves like an object
    (has methods, can be used with generics)

    View Slide

  23. 23
    scala-miniboxing.org
    Auto(un)boxing
    Auto(un)boxing
    val five: Int = 5
    val five: int = 5
    scalac
    Unboxed integer
    five + 3
    scalac
    scala.Int behaves like an object
    (has methods, can be used with generics)

    View Slide

  24. 24
    scala-miniboxing.org
    Auto(un)boxing
    Auto(un)boxing
    val five: Int = 5
    val five: int = 5
    scalac
    Unboxed integer
    five + 3
    five + 3
    scalac
    scala.Int behaves like an object
    (has methods, can be used with generics)

    View Slide

  25. 25
    scala-miniboxing.org
    Auto(un)boxing
    Auto(un)boxing
    val five: Int = 5
    val five: int = 5
    scalac
    Unboxed integer
    five + 3
    five + 3
    scalac
    Unboxed addition
    scala.Int behaves like an object
    (has methods, can be used with generics)

    View Slide

  26. 26
    scala-miniboxing.org
    Auto(un)boxing
    Auto(un)boxing
    val five: Int = identity(5)

    View Slide

  27. 27
    scala-miniboxing.org
    Auto(un)boxing
    Auto(un)boxing
    val five: Int = identity(5)
    scalac

    View Slide

  28. 28
    scala-miniboxing.org
    Auto(un)boxing
    Auto(un)boxing
    val five: Int = identity(5)
    val five: int =
    scalac

    View Slide

  29. 29
    scala-miniboxing.org
    Auto(un)boxing
    Auto(un)boxing
    val five: Int = identity(5)
    val five: int =
    identity(I.valueOf(5)).intValue
    scalac

    View Slide

  30. 30
    scala-miniboxing.org
    Auto(un)boxing
    Auto(un)boxing
    val five: Int = identity(5)
    val five: int =
    identity(I.valueOf(5)).intValue
    scalac
    Boxing coercion

    View Slide

  31. 31
    scala-miniboxing.org
    Auto(un)boxing
    Auto(un)boxing
    val five: Int = identity(5)
    val five: int =
    identity(I.valueOf(5)).intValue
    scalac
    Boxing coercion Unboxing coercion

    View Slide

  32. 32
    scala-miniboxing.org
    Auto(un)boxing
    Auto(un)boxing
    scala.Int

    View Slide

  33. 33
    scala-miniboxing.org
    Auto(un)boxing
    Auto(un)boxing
    scala.Int

    View Slide

  34. 34
    scala-miniboxing.org
    Auto(un)boxing
    Auto(un)boxing
    scala.Int
    int

    fast access

    no garbage collection

    locality

    View Slide

  35. 35
    scala-miniboxing.org
    Auto(un)boxing
    Auto(un)boxing
    scala.Int
    int

    fast access

    no garbage collection

    locality

    indirect access

    object allocation

    and thus garbage collection

    no locality guarantees

    compatible with erased generics
    java.lang.Integer

    View Slide

  36. 36
    scala-miniboxing.org
    Auto(un)boxing
    Auto(un)boxing
    scala.Int
    int

    fast access

    no garbage collection

    locality

    indirect access

    object allocation

    and thus garbage collection

    no locality guarantees

    compatible with erased generics
    java.lang.Integer
    incompatible
    → coercions

    View Slide

  37. scala-miniboxing.org
    What do auto(un)boxing,
    specialization and value
    classes have in common?

    View Slide

  38. 38
    scala-miniboxing.org
    Specialization
    Specialization
    def identity[T](t: T): T = t

    View Slide

  39. 39
    scala-miniboxing.org
    Specialization
    Specialization
    def identity[T](t: T): T = t
    specialization

    View Slide

  40. 40
    scala-miniboxing.org
    Specialization
    Specialization
    def identity[T](t: T): T = t
    def identity(t: Object): Object = t
    specialization

    View Slide

  41. 41
    scala-miniboxing.org
    Specialization
    Specialization
    def identity[T](t: T): T = t
    def identity(t: Object): Object = t
    specialization
    def identity_Z(t: bool): bool = t

    View Slide

  42. 42
    scala-miniboxing.org
    Specialization
    Specialization
    def identity[T](t: T): T = t
    def identity(t: Object): Object = t
    specialization
    def identity_Z(t: bool): bool = t
    def identity_C(t: char): char = t

    View Slide

  43. 43
    scala-miniboxing.org
    Specialization
    Specialization
    def identity[T](t: T): T = t
    def identity(t: Object): Object = t
    specialization
    def identity_Z(t: bool): bool = t
    def identity_C(t: char): char = t
    … (7 other variants)

    View Slide

  44. 44
    scala-miniboxing.org
    Specialization
    Specialization
    identity(5)

    View Slide

  45. 45
    scala-miniboxing.org
    Specialization
    Specialization
    identity(5)
    specialization

    View Slide

  46. 46
    scala-miniboxing.org
    Specialization
    Specialization
    identity(5)
    identity_I(5)
    specialization

    View Slide

  47. 47
    scala-miniboxing.org
    Specialization
    Specialization
    identity(5)
    identity_I(5)
    specialization
    The variant of identity
    specialized for int

    View Slide

  48. 48
    scala-miniboxing.org
    Specialization
    Specialization
    identity(5)
    identity_I(5)
    specialization
    The variant of identity
    specialized for int
    // no boxing!

    View Slide

  49. 49
    scala-miniboxing.org
    Specialization
    Specialization
    def tupled[T1, T2](t1: T1, t2: T2) ...

    View Slide

  50. 50
    scala-miniboxing.org
    Specialization
    Specialization
    def tupled[T1, T2](t1: T1, t2: T2) ...
    specialization

    View Slide

  51. 51
    scala-miniboxing.org
    Specialization
    Specialization
    def tupled[T1, T2](t1: T1, t2: T2) ...
    // 100 methods (102)
    specialization

    View Slide

  52. 52
    scala-miniboxing.org
    Specialization
    Specialization
    def tupled[T1, T2](t1: T1, t2: T2) ...
    // 100 methods (102)
    specialization

    View Slide

  53. 53
    scala-miniboxing.org
    Specialization
    Specialization
    def tupled[T1, T2](t1: T1, t2: T2) ...
    // 100 methods (102)
    specialization
    Can we do something
    about this?

    View Slide

  54. 54
    scala-miniboxing.org
    Specialization
    Specialization
    def tupled[T1, T2](t1: T1, t2: T2) ...
    // 100 methods (102)
    specialization
    Can we do something
    about this?

    View Slide

  55. 55
    scala-miniboxing.org
    Miniboxing
    Miniboxing
    def identity[T](t: T): T = t

    View Slide

  56. 56
    scala-miniboxing.org
    Miniboxing
    Miniboxing
    def identity[T](t: T): T = t
    miniboxing

    View Slide

  57. 57
    scala-miniboxing.org
    Miniboxing
    Miniboxing
    def identity[T](t: T): T = t
    def identity(t: Object): Object = t
    miniboxing

    View Slide

  58. 58
    scala-miniboxing.org
    Miniboxing
    Miniboxing
    def identity[T](t: T): T = t
    def identity(t: Object): Object = t
    miniboxing
    def identity_M(..., t: long): long = t

    View Slide

  59. 59
    scala-miniboxing.org
    Miniboxing
    Miniboxing
    def identity[T](t: T): T = t
    def identity(t: Object): Object = t
    miniboxing
    def identity_M(..., t: long): long = t
    long encodes all
    primitive types

    View Slide

  60. 60
    scala-miniboxing.org
    Miniboxing
    Miniboxing
    def identity[T](t: T): T = t
    def identity(t: Object): Object = t
    miniboxing
    def identity_M(..., t: long): long = t
    Only 2n variants
    long encodes all
    primitive types

    View Slide

  61. 61
    scala-miniboxing.org
    Miniboxing
    Miniboxing
    identity(3)

    View Slide

  62. 62
    scala-miniboxing.org
    Miniboxing
    Miniboxing
    identity(3)
    miniboxing

    View Slide

  63. 63
    scala-miniboxing.org
    Miniboxing
    Miniboxing
    identity(3)
    identity_M(..., int2minibox(3))
    miniboxing

    View Slide

  64. 64
    scala-miniboxing.org
    Miniboxing
    Miniboxing
    identity(3)
    identity_M(..., int2minibox(3))
    miniboxing
    Coercion

    View Slide

  65. 65
    scala-miniboxing.org
    Miniboxing
    Miniboxing
    identity(3)
    identity_M(..., int2minibox(3))
    miniboxing
    Coercion
    The miniboxed
    variant of identity

    View Slide

  66. 66
    scala-miniboxing.org
    Miniboxing
    Miniboxing
    T

    View Slide

  67. 67
    scala-miniboxing.org
    Miniboxing
    Miniboxing
    T

    View Slide

  68. 68
    scala-miniboxing.org
    Miniboxing
    Miniboxing
    T
    long

    preferred encoding

    View Slide

  69. 69
    scala-miniboxing.org
    Miniboxing
    Miniboxing
    T
    long

    preferred encoding ●
    fallback encoding

    compatible with

    method calls

    supertypes

    erased generics
    T (erased to Object)

    View Slide

  70. 70
    scala-miniboxing.org
    Miniboxing
    Miniboxing
    T
    long

    preferred encoding ●
    fallback encoding

    compatible with

    method calls

    supertypes

    erased generics
    T (erased to Object)
    incompatible
    → coercions

    View Slide

  71. scala-miniboxing.org
    What do auto(un)boxing,
    specialization and value
    classes have in common?

    View Slide

  72. 72
    scala-miniboxing.org
    Value Classes
    Value Classes
    def abs(c: Complex): Double = ...

    View Slide

  73. 73
    scala-miniboxing.org
    Value Classes
    Value Classes
    def abs(c: Complex): Double = ...
    value class transformation

    View Slide

  74. 74
    scala-miniboxing.org
    Value Classes
    Value Classes
    def abs(c: Complex): Double = ...
    def abs(c_re: Double,
    c_im: Double): Double = ...
    value class transformation

    View Slide

  75. 75
    scala-miniboxing.org
    Value Classes
    Value Classes
    def abs(c: Complex): Double = ...
    def abs(c_re: Double,
    c_im: Double): Double = ...
    value class transformation
    No object created!

    View Slide

  76. 76
    scala-miniboxing.org
    Value Classes
    Value Classes
    val c: Complex = Complex(2,1)

    View Slide

  77. 77
    scala-miniboxing.org
    Value Classes
    Value Classes
    val c: Complex = Complex(2,1)
    value class transformation

    View Slide

  78. 78
    scala-miniboxing.org
    Value Classes
    Value Classes
    val c: Complex = Complex(2,1)
    val c_re: Double = 2
    val c_im: Double = 1
    value class transformation

    View Slide

  79. 79
    scala-miniboxing.org
    Value Classes
    Value Classes
    val c: Complex = Complex(2,1)
    val c_re: Double = 2
    val c_im: Double = 1
    value class transformation
    No object created!

    View Slide

  80. 80
    scala-miniboxing.org
    Value Classes
    Value Classes
    val a: Any = c

    View Slide

  81. 81
    scala-miniboxing.org
    Value Classes
    Value Classes
    val a: Any = c
    value class transformation

    View Slide

  82. 82
    scala-miniboxing.org
    Value Classes
    Value Classes
    val a: Any = c
    val a: Object =
    new Complex(c_re, c_im)
    value class transformation

    View Slide

  83. 83
    scala-miniboxing.org
    Value Classes
    Value Classes
    val a: Any = c
    val a: Object =
    new Complex(c_re, c_im)
    value class transformation
    Coercion!

    View Slide

  84. 84
    scala-miniboxing.org
    Value Classes
    Value Classes
    value class

    View Slide

  85. 85
    scala-miniboxing.org
    Value Classes
    Value Classes
    value class

    View Slide

  86. 86
    scala-miniboxing.org
    Value Classes
    Value Classes
    value class
    structure (by-val)

    preferred encoding

    View Slide

  87. 87
    scala-miniboxing.org
    Value Classes
    Value Classes
    value class
    structure (by-val)

    preferred encoding ●
    fallback encoding

    compatible with

    supertypes

    erased generics
    class (by-ref)

    View Slide

  88. 88
    scala-miniboxing.org
    Value Classes
    Value Classes
    value class
    structure (by-val)

    preferred encoding ●
    fallback encoding

    compatible with

    supertypes

    erased generics
    class (by-ref)
    incompatible
    → coercions

    View Slide

  89. scala-miniboxing.org
    What do auto(un)boxing,
    specialization and value
    classes have in common?

    View Slide

  90. scala-miniboxing.org
    What do auto(un)boxing,
    specialization and value
    classes have in common?
    Multi-stage programs too,
    but we won't go there!

    View Slide

  91. scala-miniboxing.org
    What do auto(un)boxing,
    specialization and value
    classes have in common?

    View Slide

  92. 92
    scala-miniboxing.org
    Auto(un)boxing
    Auto(un)boxing
    scala.Int
    int

    fast access

    no garbage collection

    locality

    indirect access

    garbage collection

    and object allocation

    no locality guarantees

    compatible with erased generics
    java.lang.Integer
    incompatible
    → coercions

    View Slide

  93. 93
    scala-miniboxing.org
    Miniboxing
    Miniboxing
    T
    long

    preferred encoding ●
    fallback encoding

    compatible with

    method calls

    supertypes

    erased generics
    T (erased to Object)
    incompatible
    → coercions

    View Slide

  94. 94
    scala-miniboxing.org
    Value Classes
    Value Classes
    value class
    structure (by-val)

    preferred encoding ●
    fallback encoding

    compatible with

    supertypes

    erased generics
    class (by-ref)
    incompatible
    → coercions

    View Slide

  95. scala-miniboxing.org
    What do auto(un)boxing,
    specialization and value
    classes have in common?

    View Slide

  96. scala-miniboxing.org
    What do auto(un)boxing,
    specialization and value
    classes have in common?
    Starting from a high-level concept:

    View Slide

  97. scala-miniboxing.org
    What do auto(un)boxing,
    specialization and value
    classes have in common?
    Starting from a high-level concept:
    (1) split it into multiple representations
    based on external constaints

    View Slide

  98. scala-miniboxing.org
    What do auto(un)boxing,
    specialization and value
    classes have in common?
    Starting from a high-level concept:
    (1) split it into multiple representations
    based on external constaints
    (2) introduce the necessary coercions when
    the representation has to be converted

    View Slide

  99. scala-miniboxing.org
    What do auto(un)boxing,
    specialization and value
    classes have in common?
    Why is this important?
    The transformation
    Benchmark
    Conclusion

    View Slide

  100. scala-miniboxing.org
    What do auto(un)boxing,
    specialization and value
    classes have in common?
    Why is this important?
    The transformation
    Benchmark
    Conclusion

    View Slide

  101. scala-miniboxing.org
    Why is this important?

    View Slide

  102. 102
    scala-miniboxing.org
    Late Data Layout (LDL)
    Late Data Layout (LDL)

    View Slide

  103. 103
    scala-miniboxing.org
    Late Data Layout (LDL)
    Late Data Layout (LDL)
    concept

    View Slide

  104. 104
    scala-miniboxing.org
    Late Data Layout (LDL)
    Late Data Layout (LDL)
    concept

    View Slide

  105. 105
    scala-miniboxing.org
    Late Data Layout (LDL)
    Late Data Layout (LDL)
    concept
    repr. 1

    View Slide

  106. 106
    scala-miniboxing.org
    Late Data Layout (LDL)
    Late Data Layout (LDL)
    concept
    repr. 1 repr. 2

    View Slide

  107. 107
    scala-miniboxing.org
    Late Data Layout (LDL)
    Late Data Layout (LDL)
    concept
    repr. 1 … repr. n
    repr. 2

    View Slide

  108. 108
    scala-miniboxing.org
    Late Data Layout (LDL)
    Late Data Layout (LDL)
    concept
    repr. 1 … repr. n
    repr. 2
    Constraints from the interaction
    with other language features:

    generics

    subtyping

    virtual dispatch

    DSL semantics (staging)

    View Slide

  109. 109
    scala-miniboxing.org
    Late Data Layout (LDL)
    Late Data Layout (LDL)
    concept
    repr. 1 … repr. n
    repr. 2
    Constraints from the interaction
    with other language features:

    generics

    subtyping

    virtual dispatch

    DSL semantics (staging)
    We've seen this pattern over
    and over again:

    autounboxing

    specialization

    value classes

    multi-stage programming

    function representation

    collection representation

    View Slide

  110. 110
    scala-miniboxing.org
    Collection Representation
    Collection Representation
    List[T]

    View Slide

  111. 111
    scala-miniboxing.org
    Collection Representation
    Collection Representation
    List[T]
    Stream[T]

    preferred encoding

    for pure comprehensions

    View Slide

  112. 112
    scala-miniboxing.org
    Collection Representation
    Collection Representation
    List[T]
    Stream[T]

    preferred encoding

    for pure comprehensions

    for impure comprehensions

    more expensive (needs to
    be materialized at each
    step)
    List[T]

    View Slide

  113. 113
    scala-miniboxing.org
    Collection Representation
    Collection Representation
    List[T]
    Stream[T]

    preferred encoding

    for pure comprehensions

    for impure comprehensions

    more expensive (needs to
    be materialized at each
    step)
    List[T]
    incompatible
    → coercions

    View Slide

  114. 114
    scala-miniboxing.org
    Collection Representation
    Collection Representation
    val c = List(1,2,3)
    val d = c.map(_+1).filter(_%2==0)
    val c = List(1,2,3)
    val d = c.map(_+1).filter(_%2==0)

    View Slide

  115. 115
    scala-miniboxing.org
    Collection Representation
    Collection Representation
    val c = List(1,2,3)
    val d = c.map(_+1).filter(_%2==0)
    Materialized list
    val c = List(1,2,3)
    val d = c.map(_+1).filter(_%2==0)

    View Slide

  116. 116
    scala-miniboxing.org
    Collection Representation
    Collection Representation
    val c = List(1,2,3)
    val d = c.map(_+1).filter(_%2==0)
    Materialized list Materialized list
    val c = List(1,2,3)
    val d = c.map(_+1).filter(_%2==0)

    View Slide

  117. 117
    scala-miniboxing.org
    Collection Representation
    Collection Representation
    val c = List(1,2,3)
    val d = c.map(_+1).filter(_%2==0)
    Materialized list Materialized list
    val c = List(1,2,3)
    val d = c.map(_+1).filter(_%2==0)

    View Slide

  118. 118
    scala-miniboxing.org
    Collection Representation
    Collection Representation
    val c = List(1,2,3)
    val d = c.map(_+1).filter(_%2==0)
    Materialized list Materialized list
    val c: Stream[Int] =
    List(1,2,3).toStream
    val c = List(1,2,3)
    val d = c.map(_+1).filter(_%2==0)

    View Slide

  119. 119
    scala-miniboxing.org
    Collection Representation
    Collection Representation
    val c = List(1,2,3)
    val d = c.map(_+1).filter(_%2==0)
    Materialized list Materialized list
    val c: Stream[Int] =
    List(1,2,3).toStream
    val d: Stream[Int] =
    c.map(_+1).filter(_%2==0)
    val c = List(1,2,3)
    val d = c.map(_+1).filter(_%2==0)

    View Slide

  120. 120
    scala-miniboxing.org
    Collection Representation
    Collection Representation
    val c = List(1,2,3)
    val d = c.map(_+1).filter(_%2==0)
    Materialized list Materialized list
    val c: Stream[Int] =
    List(1,2,3).toStream
    val d: Stream[Int] =
    c.map(_+1).filter(_%2==0)
    val c = List(1,2,3)
    val d = c.map(_+1).filter(_%2==0)
    No materialization!

    View Slide

  121. 121
    scala-miniboxing.org


    View Slide

  122. 122
    scala-miniboxing.org



    View Slide

  123. 123
    scala-miniboxing.org



    repr. 1 … repr. n
    repr. 2

    View Slide

  124. 124
    scala-miniboxing.org



    repr. 1 … repr. n
    repr. 2
    Can LDL help you?

    View Slide

  125. 125
    scala-miniboxing.org



    repr. 1 … repr. n
    repr. 2
    Can LDL help you? It may not be a perfect fit,
    but let's give it a shot!

    View Slide

  126. scala-miniboxing.org
    What do auto(un)boxing,
    specialization and value
    classes have in common?
    Why is this important?
    The transformation
    Benchmark
    Conclusion

    View Slide

  127. scala-miniboxing.org
    What do auto(un)boxing,
    specialization and value
    classes have in common?
    Why is this important?
    The transformation
    Benchmark
    Conclusion

    View Slide

  128. scala-miniboxing.org
    How do transform a program?

    View Slide

  129. scala-miniboxing.org
    How do transform a program?
    If you understand the
    high-level picture, you
    will immediately see if
    your usecase matches.

    View Slide

  130. scala-miniboxing.org
    How do transform a program?
    If you understand the
    high-level picture, you
    will immediately see if
    your usecase matches. We'll use autounboxing
    as the running example,
    to keep things simple

    View Slide

  131. 131
    scala-miniboxing.org
    Auto(un)boxing
    Auto(un)boxing
    scala.Int
    int

    fast access

    no garbage collection

    locality

    indirect access

    object allocation

    and thus garbage collection

    no locality guarantees

    compatible with erased generics
    java.lang.Integer

    View Slide

  132. 132
    scala-miniboxing.org
    Auto(un)boxing
    Auto(un)boxing
    scala.Int
    int

    fast access

    no garbage collection

    locality

    indirect access

    object allocation

    and thus garbage collection

    no locality guarantees

    compatible with erased generics
    java.lang.Integer
    incompatible
    → coercions

    View Slide

  133. scala-miniboxing.org
    Naive transformation
    Syntax-based transformation
    Type-based LDL transformation

    View Slide

  134. 134
    scala-miniboxing.org
    Naive transformation
    Naive transformation
    val x: Int = List[Int](1, 2, 3).head
    val y: List[Int] = List[Int](x)
    naive unboxing

    View Slide

  135. 135
    scala-miniboxing.org
    Naive transformation
    Naive transformation
    val x: Int = List[Int](1, 2, 3).head
    val y: List[Int] = List[Int](x)
    val x: int = List[Int](1, 2, 3).head
    val y: List[Int] = List[Int](x)
    naive unboxing

    View Slide

  136. 136
    scala-miniboxing.org
    Naive transformation
    Naive transformation
    val x: Int = List[Int](1, 2, 3).head
    val y: List[Int] = List[Int](x)
    val x: int = List[Int](1, 2, 3).head
    val y: List[Int] = List[Int](x)
    naive unboxing
    representation mismatch:
    expected: int
    found: Int

    View Slide

  137. 137
    scala-miniboxing.org
    Naive transformation
    Naive transformation
    val x: Int = List[Int](1, 2, 3).head
    val y: List[Int] = List[Int](x)
    val x: int = List[Int](1, 2, 3).head
    val y: List[Int] = List[Int](x)
    naive unboxing
    representation mismatch:
    expected: int
    found: Int
    representation mismatch:
    expected: Int
    found: int

    View Slide

  138. 138
    scala-miniboxing.org
    Naive transformation
    Naive transformation

    naively replacing representations
    – leads to mismatches
    – which are hard to recover
    (impossible for value classes and miniboxing)

    we need coercions between representations

    View Slide

  139. scala-miniboxing.org
    Naive transformation
    Syntax-based transformation
    Type-based LDL transformation

    View Slide

  140. 140
    scala-miniboxing.org
    Syntax-based
    Syntax-based

    when transforming a value
    – coerce the definition right-hand side
    – coerce all references to it

    View Slide

  141. 141
    scala-miniboxing.org
    Syntax-based
    Syntax-based
    val x: Int = List[Int](1, 2, 3).head

    View Slide

  142. 142
    scala-miniboxing.org
    Syntax-based
    Syntax-based
    val x: Int = List[Int](1, 2, 3).head
    syntax-based unboxing

    View Slide

  143. 143
    scala-miniboxing.org
    Syntax-based
    Syntax-based
    val x: Int = List[Int](1, 2, 3).head
    val x: int =
    syntax-based unboxing

    View Slide

  144. 144
    scala-miniboxing.org
    Syntax-based
    Syntax-based
    val x: Int = List[Int](1, 2, 3).head
    val x: int =
    unbox(List[Int](1, 2, 3).head)
    syntax-based unboxing

    View Slide

  145. 145
    scala-miniboxing.org
    Syntax-based
    Syntax-based
    val x: Int = List[Int](1, 2, 3).head
    val x: int =
    unbox(List[Int](1, 2, 3).head)
    syntax-based unboxing
    There are no references to x,
    so there's nothing else to do.

    View Slide

  146. 146
    scala-miniboxing.org
    Syntax-based
    Syntax-based
    val x: Int = List[Int](1, 2, 3).head
    val x: int =
    unbox(List[Int](1, 2, 3).head)
    syntax-based unboxing
    There are no references to x,
    so there's nothing else to do.

    View Slide

  147. 147
    scala-miniboxing.org
    Syntax-based
    Syntax-based
    val x: Int = List[Int](1, 2, 3).head
    val y: Int = x

    View Slide

  148. 148
    scala-miniboxing.org
    Syntax-based
    Syntax-based
    val x: Int = List[Int](1, 2, 3).head
    val y: Int = x
    Transform one by one

    View Slide

  149. 149
    scala-miniboxing.org
    Syntax-based
    Syntax-based
    val x: Int = List[Int](1, 2, 3).head
    val y: Int = x
    syntax-based unboxing
    Transform one by one

    View Slide

  150. 150
    scala-miniboxing.org
    Syntax-based
    Syntax-based
    val x: Int = List[Int](1, 2, 3).head
    val y: Int = x
    val x: int =
    unbox(List[Int](1, 2, 3).head)
    syntax-based unboxing
    Transform one by one

    View Slide

  151. 151
    scala-miniboxing.org
    Syntax-based
    Syntax-based
    val x: Int = List[Int](1, 2, 3).head
    val y: Int = x
    val x: int =
    unbox(List[Int](1, 2, 3).head)
    val y: Int = box(x)
    syntax-based unboxing
    Transform one by one

    View Slide

  152. 152
    scala-miniboxing.org
    Syntax-based
    Syntax-based
    val x: int =
    unbox(List[Int](1, 2, 3).head)
    val y: Int = box(x)

    View Slide

  153. 153
    scala-miniboxing.org
    Syntax-based
    Syntax-based
    val x: int =
    unbox(List[Int](1, 2, 3).head)
    val y: Int = box(x)
    syntax-based unboxing

    View Slide

  154. 154
    scala-miniboxing.org
    Syntax-based
    Syntax-based
    val x: int =
    unbox(List[Int](1, 2, 3).head)
    val y: Int = box(x)
    val x: int =
    unbox(List[Int](1, 2, 3).head)
    val y: int =
    syntax-based unboxing

    View Slide

  155. 155
    scala-miniboxing.org
    Syntax-based
    Syntax-based
    val x: int =
    unbox(List[Int](1, 2, 3).head)
    val y: Int = box(x)
    val x: int =
    unbox(List[Int](1, 2, 3).head)
    val y: int = unbox(box(x))
    syntax-based unboxing

    View Slide

  156. 156
    scala-miniboxing.org
    Syntax-based
    Syntax-based
    val x: int =
    unbox(List[Int](1, 2, 3).head)
    val y: Int = box(x)
    val x: int =
    unbox(List[Int](1, 2, 3).head)
    val y: int = unbox(box(x))
    syntax-based unboxing
    suboptimal

    View Slide

  157. 157
    scala-miniboxing.org
    Peephole Optimization
    Peephole Optimization
    val y: int = unbox(box(x))

    View Slide

  158. 158
    scala-miniboxing.org
    Peephole Optimization
    Peephole Optimization
    val y: int = unbox(box(x))
    peephole

    View Slide

  159. 159
    scala-miniboxing.org
    Peephole Optimization
    Peephole Optimization
    val y: int = unbox(box(x))
    val y: int = x
    peephole

    View Slide

  160. 160
    scala-miniboxing.org
    Peephole Optimization
    Peephole Optimization
    val y: int = unbox(box(x))
    val y: int = x
    peephole

    View Slide

  161. 161
    scala-miniboxing.org
    Peephole Optimization
    Peephole Optimization
    val y: int = unbox(box(x))
    val y: int = x
    peephole
    Okay, let's add the peephole
    transformation in the pipeline.

    View Slide

  162. 162
    scala-miniboxing.org
    Syntax-based
    Syntax-based
    def choice(t1: Int, t2: Int): Int =
    if (Random.nextBoolean())
    t1
    else
    t2

    View Slide

  163. 163
    scala-miniboxing.org
    Syntax-based
    Syntax-based
    def choice(t1: Int, t2: Int): Int =
    if (Random.nextBoolean())
    t1
    else
    t2
    Transform one by one

    View Slide

  164. 164
    scala-miniboxing.org
    Syntax-based
    Syntax-based
    def choice(t1: int, t2: Int): Int =
    if (Random.nextBoolean())
    box(t1)
    else
    t2

    View Slide

  165. 165
    scala-miniboxing.org
    Syntax-based
    Syntax-based
    def choice(t1: int, t2: int): Int =
    if (Random.nextBoolean())
    box(t1)
    else
    box(t2)

    View Slide

  166. 166
    scala-miniboxing.org
    Syntax-based
    Syntax-based
    def choice(t1: int, t2: int): Int =
    if (Random.nextBoolean())
    box(t1)
    else
    box(t2)
    Anything missing?

    View Slide

  167. 167
    scala-miniboxing.org
    Syntax-based
    Syntax-based
    def choice(t1: int, t2: int): int =
    unbox(if (Random.nextBoolean())
    box(t1)
    else
    box(t2))

    View Slide

  168. 168
    scala-miniboxing.org
    Syntax-based
    Syntax-based
    def choice(t1: int, t2: int): int =
    unbox(if (Random.nextBoolean())
    box(t1)
    else
    box(t2))

    View Slide

  169. 169
    scala-miniboxing.org
    Syntax-based
    Syntax-based
    def choice(t1: int, t2: int): int =
    unbox(if (Random.nextBoolean())
    box(t1)
    else
    box(t2))
    new peephole rule

    View Slide

  170. 170
    scala-miniboxing.org
    Syntax-based
    Syntax-based
    def choice(t1: int, t2: int): int =
    unbox(if (Random.nextBoolean())
    box(t1)
    else
    box(t2))
    new peephole rule
    sink outside coercions
    into the if branches

    View Slide

  171. 171
    scala-miniboxing.org
    Syntax-based
    Syntax-based
    def choice(t1: int, t2: int): int =
    if (Random.nextBoolean())
    unbox(box(t1))
    else
    unbox(box(t2))

    View Slide

  172. 172
    scala-miniboxing.org
    Syntax-based
    Syntax-based
    def choice(t1: int, t2: int): int =
    if (Random.nextBoolean())
    unbox(box(t1))
    else
    unbox(box(t2))

    View Slide

  173. 173
    scala-miniboxing.org
    Syntax-based
    Syntax-based
    def choice(t1: int, t2: int): int =
    if (Random.nextBoolean())
    t1
    else
    t2

    View Slide

  174. 174
    scala-miniboxing.org
    Syntax-based
    Syntax-based
    def choice(t1: int, t2: int): int =
    if (Random.nextBoolean())
    t1
    else
    t2
    complicated

    View Slide

  175. 175
    scala-miniboxing.org
    Syntax-based
    Syntax-based

    peephole transformation does not scale
    – needs multiple rules for each node
    – needs successive rewriting => slow
    – impossible to guarantee optimality

    View Slide

  176. scala-miniboxing.org
    Naive transformation
    Syntax-based transformation
    Type-based LDL transformation

    View Slide

  177. 177
    scala-miniboxing.org
    Type-based transformation
    Type-based transformation

    propagate representation information
    – into the type system (based on annotated types)
    – let the type system propagate this information

    View Slide

  178. 178
    scala-miniboxing.org
    Type-based transformation
    Type-based transformation

    re-typechecking the tree
    – exposes inconsistencies in the representation
    – so we introduce coercions

    only when representations don't match

    View Slide

  179. 179
    scala-miniboxing.org
    Type-based transformation
    Type-based transformation

    three-stage mechanism
    – inject annotate the values to be unboxed

    – coerce introduce coercion markers

    – commit commit to the alternative representations

    View Slide

  180. 180
    scala-miniboxing.org
    Type-based transformation
    Type-based transformation
    Warning!
    Throughout the presentation we'll be writing
    annotations written before types (e.g. “@unboxed
    Int”), although in the Scala syntax they are written
    after the type (e.g.“Int @unboxed”). This makes it
    easier to read the types aloud.

    View Slide

  181. 181
    scala-miniboxing.org
    Type-based
    Type-based inject
    coerce
    commit

    View Slide

  182. 182
    scala-miniboxing.org
    Type-based
    Type-based
    def choice(t1: Int,
    t2: Int): Int =
    if (Random.nextBoolean())
    t1
    else
    t2
    inject
    coerce
    commit

    View Slide

  183. 183
    scala-miniboxing.org
    Type-based
    Type-based
    def choice(t1: Int,
    t2: Int): Int =
    if (Random.nextBoolean())
    t1
    else
    t2
    inject
    coerce
    commit

    View Slide

  184. 184
    scala-miniboxing.org
    Type-based
    Type-based
    def choice(t1: @unboxed Int,
    t2: @unboxed Int): @unboxed Int =
    if (Random.nextBoolean())
    t1
    else
    t2
    inject
    coerce
    commit

    View Slide

  185. 185
    scala-miniboxing.org
    Type-based
    Type-based
    def choice(t1: @unboxed Int,
    t2: @unboxed Int): @unboxed Int =
    if (Random.nextBoolean())
    t1
    else
    t2
    inject
    coerce
    commit
    configurable introduction
    of annotations
    based on external constraints

    View Slide

  186. 186
    scala-miniboxing.org
    Type-based
    Type-based
    def choice(t1: @unboxed Int,
    t2: @unboxed Int): @unboxed Int =
    if (Random.nextBoolean())
    t1
    else
    t2
    inject
    coerce
    commit

    View Slide

  187. 187
    scala-miniboxing.org
    Type-based
    Type-based
    def choice(t1: @unboxed Int,
    t2: @unboxed Int): @unboxed Int =
    if (Random.nextBoolean())
    t1
    else
    t2
    inject
    coerce
    commit

    View Slide

  188. 188
    scala-miniboxing.org
    Type-based
    Type-based
    def choice(t1: @unboxed Int,
    t2: @unboxed Int): @unboxed Int =
    if (Random.nextBoolean())
    t1
    else
    t2
    inject
    coerce
    commit

    View Slide

  189. 189
    scala-miniboxing.org
    Type-based
    Type-based
    def choice(t1: @unboxed Int,
    t2: @unboxed Int): @unboxed Int =
    if (Random.nextBoolean())
    t1
    else
    t2
    inject
    coerce
    commit
    the rhs expression must
    be of type @unboxed Int

    View Slide

  190. 190
    scala-miniboxing.org
    Type-based
    Type-based
    def choice(t1: @unboxed Int,
    t2: @unboxed Int): @unboxed Int =
    if (Random.nextBoolean())
    t1
    else
    t2
    inject
    coerce
    commit
    : @unboxed Int
    the rhs expression must
    be of type @unboxed Int

    View Slide

  191. 191
    scala-miniboxing.org
    Type-based
    Type-based
    def choice(t1: @unboxed Int,
    t2: @unboxed Int): @unboxed Int =
    if (Random.nextBoolean())
    t1
    else
    t2
    inject
    coerce
    commit
    : @unboxed Int
    expected type
    (part of local type inference)
    the rhs expression must
    be of type @unboxed Int

    View Slide

  192. 192
    scala-miniboxing.org
    Type-based
    Type-based
    def choice(t1: @unboxed Int,
    t2: @unboxed Int): @unboxed Int =
    if (Random.nextBoolean())
    t1
    else
    t2
    inject
    coerce
    commit
    : Boolean

    View Slide

  193. 193
    scala-miniboxing.org
    Type-based
    Type-based
    def choice(t1: @unboxed Int,
    t2: @unboxed Int): @unboxed Int =
    if (Random.nextBoolean())
    t1
    else
    t2
    inject
    coerce
    commit
    : Boolean
    matches:
    expected: Boolean
    found: Boolean

    View Slide

  194. 194
    scala-miniboxing.org
    Type-based
    Type-based
    def choice(t1: @unboxed Int,
    t2: @unboxed Int): @unboxed Int =
    if (Random.nextBoolean())
    t1
    else
    t2
    inject
    coerce
    commit
    : @unboxed Int

    View Slide

  195. 195
    scala-miniboxing.org
    Type-based
    Type-based
    def choice(t1: @unboxed Int,
    t2: @unboxed Int): @unboxed Int =
    if (Random.nextBoolean())
    t1
    else
    t2
    inject
    coerce
    commit
    : @unboxed Int
    matches:
    expected: @unboxed Int
    found: @unboxed Int

    View Slide

  196. 196
    scala-miniboxing.org
    Type-based
    Type-based
    def choice(t1: @unboxed Int,
    t2: @unboxed Int): @unboxed Int =
    if (Random.nextBoolean())
    t1
    else
    t2
    inject
    coerce
    commit
    : @unboxed Int

    View Slide

  197. 197
    scala-miniboxing.org
    Type-based
    Type-based
    def choice(t1: @unboxed Int,
    t2: @unboxed Int): @unboxed Int =
    if (Random.nextBoolean())
    t1
    else
    t2
    inject
    coerce
    commit
    matches:
    ...
    : @unboxed Int

    View Slide

  198. 198
    scala-miniboxing.org
    Type-based
    Type-based
    def choice(t1: @unboxed Int,
    t2: @unboxed Int): @unboxed Int =
    if (Random.nextBoolean())
    t1
    else
    t2
    inject
    coerce
    commit

    View Slide

  199. 199
    scala-miniboxing.org
    Type-based
    Type-based
    def choice(t1: @unboxed Int,
    t2: @unboxed Int): @unboxed Int =
    if (Random.nextBoolean())
    t1
    else
    t2
    inject
    coerce
    commit
    all okay, next phase

    View Slide

  200. 200
    scala-miniboxing.org
    Type-based
    Type-based
    def choice(t1: @unboxed Int,
    t2: @unboxed Int): @unboxed Int =
    if (Random.nextBoolean())
    t1
    else
    t2
    inject
    coerce
    commit

    View Slide

  201. 201
    scala-miniboxing.org
    Type-based
    Type-based
    def choice(t1: @unboxed Int,
    t2: @unboxed Int): @unboxed Int =
    if (Random.nextBoolean())
    t1
    else
    t2
    inject
    coerce
    commit
    Replace:
    @unboxed Int → int
    (not showing Int → j.l.Integer)

    View Slide

  202. 202
    scala-miniboxing.org
    Type-based
    Type-based
    def choice(t1: int,
    t2: int): int =
    if (Random.nextBoolean())
    t1
    else
    t2
    inject
    coerce
    commit

    View Slide

  203. 203
    scala-miniboxing.org
    Type-based
    Type-based
    def choice(t1: int,
    t2: int): int =
    if (Random.nextBoolean())
    t1
    else
    t2
    inject
    coerce
    commit
    that's it!

    View Slide

  204. 204
    scala-miniboxing.org
    Type-based transformation
    Type-based transformation

    three-stage mechanism
    – inject: add annotations
    – coerce: add coercions (based on the annotations)
    – commit: final representation semantics

    View Slide

  205. 205
    scala-miniboxing.org
    Type-based transformation
    Type-based transformation

    Scalac's erasure
    – similar transformation
    – less flexible (no annotations)
    – entangled with other transformations

    we took what's good
    – and allowed the transformation to work on other
    usecases as well

    View Slide

  206. 206
    scala-miniboxing.org
    Type-based transformation
    Type-based transformation

    why do this?
    – at the core of:

    miniboxing (http://scala-miniboxing.org)

    value classes plugin (https://github.com/miniboxing/value-plugin)

    multi-stage plugin (https://github.com/miniboxing/staging-plugin)


    View Slide

  207. 207
    scala-miniboxing.org
    Type-based transformation
    Type-based transformation

    why do this?
    – at the core of:

    miniboxing (http://scala-miniboxing.org)

    value classes plugin (https://github.com/miniboxing/value-plugin)

    multi-stage plugin (https://github.com/miniboxing/staging-plugin)


    most important one

    View Slide

  208. scala-miniboxing.org
    Consistency
    Selectivity
    Optimality*
    Properties
    Type-based LDL transformation
    * not formally proven yet

    View Slide

  209. 209
    scala-miniboxing.org
    Consistency
    Consistency inject
    coerce
    commit

    representations become explicit in types
    – representation mismatches

    become type mismatches

    are exposed by the type system
    – mismatches lead to coercions

    explicit bridges between representations

    are introduced automatically
    – regardless of the representations

    at a meta-level

    View Slide

  210. scala-miniboxing.org
    Consistency
    Selectivity
    Optimality*
    Properties
    Type-based LDL transformation
    * not formally proven yet

    View Slide

  211. 211
    scala-miniboxing.org
    Selectivity
    Selectivity inject
    coerce
    commit

    annotations allow selectively picking the values
    to be transformed
    – value classes

    cannot unbox multi-param values in return position (not
    supported by the JVM platform)

    bridge methods
    – staging

    annotations signal domain-specific knowledge
    – can occur inside generics (List[@staged Int])

    View Slide

  212. 212
    scala-miniboxing.org
    Selectivity
    Selectivity
    def choice(t1: Int,
    t2: Int): Int =
    if (Random.nextBoolean())
    t1
    else
    t2
    inject
    coerce
    commit

    View Slide

  213. 213
    scala-miniboxing.org
    Selectivity
    Selectivity
    def choice(t1: Int,
    t2: Int): Int =
    if (Random.nextBoolean())
    t1
    else
    t2
    inject
    coerce
    commit
    what if we did not annotate t1?

    View Slide

  214. 214
    scala-miniboxing.org
    Selectivity
    Selectivity
    def choice(t1: Int,
    t2: @unboxed Int): @unboxed Int =
    if (Random.nextBoolean())
    t1
    else
    t2
    inject
    coerce
    commit
    what if we did not annotate t1?

    View Slide

  215. 215
    scala-miniboxing.org
    Selectivity
    Selectivity
    def choice(t1: Int,
    t2: @unboxed Int): @unboxed Int =
    if (Random.nextBoolean())
    t1
    else
    t2
    inject
    coerce
    commit
    : @unboxed Int

    View Slide

  216. 216
    scala-miniboxing.org
    Selectivity
    Selectivity
    def choice(t1: Int,
    t2: @unboxed Int): @unboxed Int =
    if (Random.nextBoolean())
    t1
    else
    t2
    inject
    coerce
    commit
    : @unboxed Int

    View Slide

  217. 217
    scala-miniboxing.org
    Selectivity
    Selectivity
    def choice(t1: Int,
    t2: @unboxed Int): @unboxed Int =
    if (Random.nextBoolean())
    t1
    else
    t2
    inject
    coerce
    commit
    : @unboxed Int
    mismatch:
    expected: @unboxed Int
    found: Int

    View Slide

  218. 218
    scala-miniboxing.org
    Selectivity
    Selectivity
    def choice(t1: Int,
    t2: @unboxed Int): @unboxed Int =
    if (Random.nextBoolean())
    t1
    else
    t2
    inject
    coerce
    commit
    : @unboxed Int
    mismatch:
    expected: @unboxed Int
    found: Int
    coercion

    View Slide

  219. 219
    scala-miniboxing.org
    Selectivity
    Selectivity
    def choice(t1: Int,
    t2: @unboxed Int): @unboxed Int =
    if (Random.nextBoolean())
    unbox(t1)
    else
    t2
    inject
    coerce
    commit

    View Slide

  220. 220
    scala-miniboxing.org
    Selectivity
    Selectivity
    def choice(t1: Int,
    t2: @unboxed Int): @unboxed Int =
    if (Random.nextBoolean())
    unbox(t1)
    else
    t2
    inject
    coerce
    commit

    View Slide

  221. 221
    scala-miniboxing.org
    Selectivity
    Selectivity
    def choice(t1: Int,
    t2: int): int =
    if (Random.nextBoolean())
    t1.intValue
    else
    t2
    inject
    coerce
    commit

    View Slide

  222. scala-miniboxing.org
    Consistency
    Selectivity
    Optimality*
    Properties
    Type-based LDL transformation
    * not formally proven yet

    View Slide

  223. 223
    scala-miniboxing.org
    Optimality
    Optimality
    def choice(t1: Int,
    t2: @unboxed Int): @unboxed Int =
    if (Random.nextBoolean())
    unbox(t1)
    else
    t2
    inject
    coerce
    commit

    View Slide

  224. 224
    scala-miniboxing.org
    Optimality
    Optimality
    def choice(t1: Int,
    t2: @unboxed Int): @unboxed Int =
    if (Random.nextBoolean())
    unbox(t1)
    else
    t2
    inject
    coerce
    commit

    View Slide

  225. 225
    scala-miniboxing.org
    Optimality
    Optimality
    def choice(t1: Int,
    t2: @unboxed Int): @unboxed Int =
    if (Random.nextBoolean())
    unbox(t1)
    else
    t2
    inject
    coerce
    commit
    Coercions are sunk in the tree →
    excute only if necessary

    View Slide

  226. scala-miniboxing.org
    Miniboxing
    Value Classes*
    Multi-stage Programming*
    Use cases
    Type-based LDL transformation
    * early prototypes

    View Slide

  227. 227
    scala-miniboxing.org
    LDL for miniboxing
    LDL for miniboxing
    def tupled[T1, T2](t1: T1, t2: T2) = ...

    View Slide

  228. 228
    scala-miniboxing.org
    LDL for miniboxing
    LDL for miniboxing
    def tupled[T1, T2](t1: T1, t2: T2) = ...
    miniboxing

    View Slide

  229. 229
    scala-miniboxing.org
    LDL for miniboxing
    LDL for miniboxing
    def tupled[T1, T2](t1: T1, t2: T2) = ...
    miniboxing
    first: duplicate body

    View Slide

  230. 230
    scala-miniboxing.org
    LDL for miniboxing
    LDL for miniboxing
    def tupled[T1, T2](t1: T1, t2: T2) = ...
    miniboxing
    def tupled[T1, T2](t1: T1, t2: T2) = …
    first: duplicate body

    View Slide

  231. 231
    scala-miniboxing.org
    LDL for miniboxing
    LDL for miniboxing
    def tupled[T1, T2](t1: T1, t2: T2) = ...
    miniboxing
    def tupled[T1, T2](t1: T1, t2: T2) = …
    def tupled_ML[T1, T2](..., t1: T1, t2: T2) = …
    first: duplicate body

    View Slide

  232. 232
    scala-miniboxing.org
    LDL for miniboxing
    LDL for miniboxing
    def tupled[T1, T2](t1: T1, t2: T2) = ...
    miniboxing
    def tupled[T1, T2](t1: T1, t2: T2) = …
    def tupled_ML[T1, T2](..., t1: T1, t2: T2) = …
    def tupled_LM[T1, T2](..., t1: T1, t2: T2) = …
    first: duplicate body

    View Slide

  233. 233
    scala-miniboxing.org
    LDL for miniboxing
    LDL for miniboxing
    def tupled[T1, T2](t1: T1, t2: T2) = ...
    miniboxing
    def tupled[T1, T2](t1: T1, t2: T2) = …
    def tupled_ML[T1, T2](..., t1: T1, t2: T2) = …
    def tupled_LM[T1, T2](..., t1: T1, t2: T2) = …
    def tupled_MM[T1, T2](..., t1: T1, t2: T2) = …
    first: duplicate body

    View Slide

  234. 234
    scala-miniboxing.org
    LDL for miniboxing
    LDL for miniboxing
    def tupled[T1, T2](t1: T1, t2: T2) = ...
    miniboxing
    def tupled[T1, T2](t1: T1, t2: T2) = …
    def tupled_ML[T1, T2](..., t1: T1, t2: T2) = …
    def tupled_LM[T1, T2](..., t1: T1, t2: T2) = …
    def tupled_MM[T1, T2](..., t1: T1, t2: T2) = …
    first: duplicate body
    second: adapt it

    View Slide

  235. 235
    scala-miniboxing.org
    LDL for miniboxing
    LDL for miniboxing
    def tupled[T1, T2](t1: T1, t2: T2) = ...
    miniboxing
    def tupled[T1, T2](t1: T1, t2: T2) = …
    def tupled_ML[T1, T2](..., t1: @long T1, t2: T2) = …
    def tupled_LM[T1, T2](..., t1: T1, t2: @long T2) = …
    def tupled_MM[T1, T2](..., t1: @long T1, t2: @long T2) =
    * @long is used instead of @storage[Long]
    second: adapt it
    first: duplicate body

    View Slide

  236. 236
    scala-miniboxing.org
    LDL for miniboxing
    LDL for miniboxing
    def tupled[T1, T2](t1: T1, t2: T2) = ...
    miniboxing
    def tupled[T1, T2](t1: T1, t2: T2) = …
    def tupled_ML[T1, T2](..., t1: @long T1, t2: T2) = …
    def tupled_LM[T1, T2](..., t1: T1, t2: @long T2) = …
    def tupled_MM[T1, T2](..., t1: @long T1, t2: @long T2) =
    * @long is used instead of @storage[Long]
    second: adapt it
    using the transformation
    first: duplicate body

    View Slide

  237. 237
    scala-miniboxing.org
    LDL for miniboxing
    LDL for miniboxing
    def tupled[T1, T2](t1: T1, t2: T2) = ...
    miniboxing
    def tupled[T1, T2](t1: T1, t2: T2) = …
    def tupled_ML[T1, T2](..., t1: @long T1, t2: T2) = …
    def tupled_LM[T1, T2](..., t1: T1, t2: @long T2) = …
    def tupled_MM[T1, T2](..., t1: @long T1, t2: @long T2) =
    * @long is used instead of @storage[Long]
    second: adapt it
    using the transformation
    first: duplicate body
    body gets adapted**

    View Slide

  238. scala-miniboxing.org
    Miniboxing
    Value Classes*
    Multi-stage Programming*
    Use cases
    Type-based LDL transformation
    * early prototypes

    View Slide

  239. 239
    scala-miniboxing.org
    LDL for value classes
    LDL for value classes
    def abs(c: @unboxed Complex): Double = ...

    View Slide

  240. 240
    scala-miniboxing.org
    LDL for value classes
    LDL for value classes
    def abs(c: @unboxed Complex): Double = ...
    value class plugin
    (commit phase)

    View Slide

  241. 241
    scala-miniboxing.org
    LDL for value classes
    LDL for value classes
    def abs(c: @unboxed Complex): Double = ...
    value class plugin
    (commit phase)
    def abs(c_re: Double, c_im: Double): Double = ...

    View Slide

  242. 242
    scala-miniboxing.org
    LDL for value classes
    LDL for value classes
    def abs(c: @unboxed Complex): Double = ...
    value class plugin
    (commit phase)
    def abs(c_re: Double, c_im: Double): Double = ...

    View Slide

  243. 243
    scala-miniboxing.org
    LDL for value classes
    LDL for value classes
    def abs(c: @unboxed Complex): Double = ...
    value class plugin
    (commit phase)
    def abs(c_re: Double, c_im: Double): Double = ...
    I'm hiding a lot of details here.
    But one could talk about this for an entire day

    View Slide

  244. 244
    scala-miniboxing.org
    LDL for value classes
    LDL for value classes
    def abs(c: @unboxed Complex): Double = ...
    value class plugin
    (commit phase)
    def abs(c_re: Double, c_im: Double): Double = ...
    I'm hiding a lot of details here.
    But one could talk about this for an entire day
    The one is @xeno-by!
    He implemented this :)

    View Slide

  245. scala-miniboxing.org
    Miniboxing
    Value Classes*
    Multi-stage Programming*
    Use cases
    Type-based LDL transformation
    * early prototypes

    View Slide

  246. scala-miniboxing.org
    Miniboxing
    Value Classes*
    Multi-stage Programming*
    Use cases
    Type-based LDL transformation
    We won't go into it today, but see
    github.com/miniboxing/staging-plugin
    * early prototypes

    View Slide

  247. scala-miniboxing.org
    What do auto(un)boxing,
    specialization and value
    classes have in common?
    Why is this important?
    The transformation
    Benchmark
    Conclusion

    View Slide

  248. scala-miniboxing.org
    What do auto(un)boxing,
    specialization and value
    classes have in common?
    Why is this important?
    The transformation
    Benchmark
    Conclusion

    View Slide

  249. 249
    scala-miniboxing.org
    Miniboxing Benchmarks
    Miniboxing Benchmarks
    on the Scala library
    on the Scala library

    benchmark: Least Squares Method
    – using a mockup of scala.collection.immutable.List

    View Slide

  250. 250
    scala-miniboxing.org
    Miniboxing Benchmarks
    Miniboxing Benchmarks
    on the Scala library
    on the Scala library

    work with Aymeric Genet (github: @MelodyLucid)

    mock-up of Scala linked List
    – Tuples
    – Traversable/TraversableLike
    – Iterator/Iterable/IterableLike
    – LinearSeqOptimized
    – Builder/CanBuildFrom

    FunctionX has a nice story

    View Slide

  251. 251
    scala-miniboxing.org
    Miniboxing Benchmarks
    Miniboxing Benchmarks
    on the Scala library
    on the Scala library

    FunctionX from the Scala library
    – specialized
    – no way to call from miniboxed code without boxing slow


    MiniboxedFunctionX benchmarked

    – miniboxed, call without coercing
    – added automatically with another LDL cycle

    MyFunctionX benchmarked

    – miniboxed, call without coercing
    – added by hand to the library

    View Slide

  252. 252
    scala-miniboxing.org
    Miniboxing Benchmarks
    Miniboxing Benchmarks
    on the Scala library
    on the Scala library

    View Slide

  253. 253
    scala-miniboxing.org
    Miniboxing Benchmarks
    Miniboxing Benchmarks
    on the Scala library
    on the Scala library
    30%-45% faster

    View Slide

  254. 254
    scala-miniboxing.org
    Miniboxing Benchmarks
    Miniboxing Benchmarks
    on the Scala library
    on the Scala library
    30%-45% faster
    On a non-contiguous
    data structure

    View Slide

  255. 255
    scala-miniboxing.org
    Miniboxing Benchmarks
    Miniboxing Benchmarks
    on the Scala library (with GC)
    on the Scala library (with GC)

    View Slide

  256. 256
    scala-miniboxing.org
    Miniboxing Benchmarks
    Miniboxing Benchmarks
    on the Scala library (with GC)
    on the Scala library (with GC)
    3x faster
    On a non-contiguous
    data structure

    View Slide

  257. scala-miniboxing.org
    What do auto(un)boxing,
    specialization and value
    classes have in common?
    Why is this important?
    The transformation
    Benchmark
    Conclusion

    View Slide

  258. scala-miniboxing.org
    What do auto(un)boxing,
    specialization and value
    classes have in common?
    Why is this important?
    The transformation
    Benchmark
    Conclusion
    `

    View Slide

  259. 259
    scala-miniboxing.org
    Conclusion
    Conclusion
    LDL is a
    LDL is a transformation
    transformation

    that allows splitting a high-level concept
    – into multiple representations
    – in a consistent way (through coercions)
    – in a selective way (through annotations)
    – in an optimal way (coerce only if necessary)

    use cases covered
    – miniboxing
    – value classes

    what is your use case?

    repr. 1 … repr. n
    repr. 2

    View Slide

  260. scala-miniboxing.org
    Credits and Thank you-s

    Cristian Talau - developed the initial prototype, as a semester project

    Eugene Burmako - the value class plugin based on the LDL transformation

    Aymeric Genet - developing collection-like benchmarks for the miniboxing plugin

    Martin Odersky, for his patient guidance

    Eugene Burmako, for trusting the idea enough to develop the value-plugin based on the LDL transformation

    Iulian Dragos, for his work on specialization and many explanations

    Miguel Garcia, for his original insights that spawned the miniboxing idea

    Michel Schinz, for his wonderful comments and enlightening ACC course

    Andrew Myers and Roland Ducournau for the discussions we had and the feedback provided

    Heather Miller for the eye-opening discussions we had

    Vojin Jovanovic, Sandro Stucki, Manohar Jonalagedda and the whole LAMP laboratory in EPFL for the extraordinary atmosphere

    Adriaan Moors, for the miniboxing name which stuck :))

    Thierry Coppey, Vera Salvisberg and George Nithin, who patiently listened to many presentations and provided valuable feedback

    Grzegorz Kossakowski, for the many brainstorming sessions on specialization

    Erik Osheim, Tom Switzer and Rex Kerr for their guidance on the Scala community side

    OOPSLA paper and artifact reviewers, who reshaped the paper with their feedback

    Sandro, Vojin, Nada, Heather, Manohar - reviews and discussions on the LDL paper

    Hubert Plociniczak for the type notation in the LDL paper

    Denys Shabalin, Dmitry Petrashko for their patient reviews of the LDL paper
    Special thanks to the Scala Community for their support!
    (@StuHood, @vpatryshev and everyone else!)

    View Slide

  261. 261
    scala-miniboxing.org
    Conclusion
    Conclusion
    LDL is a
    LDL is a transformation
    transformation

    that allows splitting a high-level concept
    – into multiple representations
    – in a consistent way (through coercions)
    – in a selective way (through annotations)
    – in an optimal way (coerce only if necessary)

    use cases covered
    – miniboxing
    – value classes

    what is your use case?

    repr. 1 … repr. n
    repr. 2

    View Slide

  262. scala-miniboxing.org
    ScalaTeam @ EPFL

    View Slide

  263. scala-miniboxing.org
    ScalaTeam @ EPFL

    Dependent Object Types calculus
    – core type system of the dotty compiler
    – Nada Amin/Tiark Rompf
    https://github.com/TiarkRompf/minidot
    https://github.com/lampepfl/dotty

    View Slide

  264. scala-miniboxing.org
    ScalaTeam @ EPFL

    YinYang multi-stage macro-based frontend
    – replacement for scala-virtualized
    – Vojin Jovanovic/Sandro Stucki + others
    https://github.com/vjovanov/yin-yang

    View Slide

  265. scala-miniboxing.org
    ScalaTeam @ EPFL

    Scala.js backend
    – compiles Scala to JavaScript
    – Sébastien Doeraene/Tobias Schlatter + others
    http://www.scala-js.org/
    www

    View Slide

  266. scala-miniboxing.org
    ScalaTeam @ EPFL

    Staged Parser-combinators
    – fast parser combinators through staging
    – Manohar Jonnalagedda + others
    https://github.com/manojo/experiments

    View Slide

  267. scala-miniboxing.org
    ScalaTeam @ EPFL

    Pickling framework and Spores
    – support for distributed programming
    – Heather Miller/Philipp Haller + others
    https://github.com/scala/pickling
    https://github.com/heathermiller/spores

    View Slide

  268. scala-miniboxing.org
    ScalaTeam @ EPFL

    dotty
    – experimental compiler based on DOT
    – Martin Odersky/Dmitry Petrashko/Samuel
    Grütter/Tobias Schlatter + others
    https://github.com/lampepfl/dotty

    View Slide

  269. scala-miniboxing.org
    ScalaTeam @ EPFL

    scala.meta metaprogramming support
    – Improved reflection, macros, and many more
    – Eugene Burmako/Denys Shabalin + others
    http://scalameta.org/
    www

    View Slide

  270. scala-miniboxing.org
    ScalaTeam @ EPFL

    miniboxing specialization
    – LDL transformation
    – Vlad Ureche/Aymeric Genêt + others
    http://scala-miniboxing.org/
    www

    View Slide

  271. scala-miniboxing.org
    ScalaTeam @ EPFL

    scaladyno plugin
    – giving Scala a dynamic language look and feel
    – Cédric Bastin/Vlad Ureche
    https://github.com/scaladyno/scaladyno-plugin

    View Slide

  272. scala-miniboxing.org
    ScalaTeam @ EPFL

    ScalaBlitz optimization framework
    – macro-based collection optimization
    – Dmitry Petrashko/Aleksandar Prokopec
    http://scala-blitz.github.io/
    www

    View Slide

  273. scala-miniboxing.org
    ScalaTeam @ EPFL

    Type debugger for Scala
    – debugging aid for Scala type errors
    – Hubert Plociniczak
    http://lampwww.epfl.ch/~plocinic/
    type-debugger-tutorial/
    www

    View Slide

  274. scala-miniboxing.org
    ScalaTeam @ EPFL

    ScalaMeter benchmarking framework
    – google caliper for scala
    – Aleksandar Prokopec
    http://scalameter.github.io/
    www

    View Slide

  275. scala-miniboxing.org
    ScalaTeam @ EPFL

    the new scalac backend
    – good performance gains
    – Miguel Garcia

    on the job market right now
    http://magarciaepfl.github.io/scala/
    www
    @ [email protected]

    View Slide

  276. scala-miniboxing.org 1st of August 2014
    Scala Bay Area Meetup
    Linkedin HQ, Mountain View
    Thank you!

    View Slide