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

Late Data Layout - OOPLSA Talk

Late Data Layout - OOPLSA Talk

Late Data Layout talk at the OOPLSA research conference (Object-Oriented Programming, Systems, Languages & Applications). Website: http://2014.splashcon.org/event/oopsla2014-unifying-data-representation-transformations

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

Vlad Ureche

October 23, 2014
Tweet

More Decks by Vlad Ureche

Other Decks in Programming

Transcript

  1. scala-ldl.org
    Late Data Layout:
    Unifying Data Representation Transformations
    Vlad Ureche Eugene Burmako Martin Odersky
    École polytechnique fédérale de Lausanne, Switzerland
    {first.last}@epfl.ch
    23rd of October 2014
    OOPSLA '14
    Portland, OR

    View full-size slide

  2. scala-ldl.org
    Late Data Layout:
    Unifying Data Representation Transformations

    View full-size slide

  3. scala-ldl.org
    Late Data Layout:
    Unifying Data Representation Transformations

    compiler transformations

    separate compilation

    global scope

    View full-size slide

  4. scala-ldl.org
    Late Data Layout:
    Unifying Data Representation Transformations

    unboxing, value classes

    how data is represented

    View full-size slide

  5. scala-ldl.org
    Late Data Layout:
    Unifying Data Representation Transformations

    what is there to unify?

    why bother?

    View full-size slide

  6. scala-ldl.org
    Motivation
    Transformation
    Conclusion
    Properties
    Benchmarks

    View full-size slide

  7. scala-ldl.org
    Representation Transformations

    View full-size slide

  8. scala-ldl.org
    Unboxing Primitive Types
    Representation Transformations

    View full-size slide

  9. scala-ldl.org
    Unboxing Primitive Types
    Unboxing Primitive Types

    View full-size slide

  10. scala-ldl.org
    Unboxing Primitive Types
    Unboxing Primitive Types
    int

    value

    no garbage collection

    locality

    View full-size slide

  11. scala-ldl.org
    Unboxing Primitive Types
    Unboxing Primitive Types

    indirect access

    object allocation

    and thus garbage collection

    no locality guarantees

    compatible with erased generics
    java.lang.Integer
    int

    value

    no garbage collection

    locality

    View full-size slide

  12. scala-ldl.org
    Unboxing Primitive Types
    Unboxing Primitive Types

    indirect access

    object allocation

    and thus garbage collection

    no locality guarantees

    compatible with erased generics
    java.lang.Integer
    in Java, programmers are
    responsible for the choice
    of representation
    int

    value

    no garbage collection

    locality

    View full-size slide

  13. scala-ldl.org
    Unboxing Primitive Types
    Unboxing Primitive Types

    indirect access

    object allocation

    and thus garbage collection

    no locality guarantees

    compatible with erased generics
    java.lang.Integer
    in Java, programmers are
    responsible for the choice
    of representation What about Scala?
    int

    value

    no garbage collection

    locality

    View full-size slide

  14. scala-ldl.org
    Unboxing Primitive Types
    Unboxing Primitive Types

    indirect access

    object allocation

    and thus garbage collection

    no locality guarantees

    compatible with erased generics
    java.lang.Integer
    int

    value

    no garbage collection

    locality

    View full-size slide

  15. scala-ldl.org
    Unboxing Primitive Types
    Unboxing Primitive Types

    indirect access

    object allocation

    and thus garbage collection

    no locality guarantees

    compatible with erased generics
    java.lang.Integer
    int

    value

    no garbage collection

    locality

    View full-size slide

  16. scala-ldl.org
    Unboxing Primitive Types
    Unboxing Primitive Types

    indirect access

    object allocation

    and thus garbage collection

    no locality guarantees

    compatible with erased generics
    java.lang.Integer
    int

    value

    no garbage collection

    locality
    scala.Int

    View full-size slide

  17. scala-ldl.org
    Unboxing Primitive Types
    Unboxing Primitive Types

    indirect access

    object allocation

    and thus garbage collection

    no locality guarantees

    compatible with erased generics
    java.lang.Integer
    int

    value

    no garbage collection

    locality
    scalac
    scala.Int

    View full-size slide

  18. scala-ldl.org
    Unboxing Primitive Types
    Unboxing Primitive Types

    indirect access

    object allocation

    and thus garbage collection

    no locality guarantees

    compatible with erased generics
    java.lang.Integer
    int

    value

    no garbage collection

    locality
    scalac
    Choice of representation
    1
    scala.Int

    View full-size slide

  19. scala-ldl.org
    Unboxing Primitive Types
    Unboxing Primitive Types

    indirect access

    object allocation

    and thus garbage collection

    no locality guarantees

    compatible with erased generics
    java.lang.Integer
    int

    value

    no garbage collection

    locality
    scalac
    Choice of representation
    1
    2 Coercions between representations
    scala.Int

    View full-size slide

  20. scala-ldl.org
    Unboxing Primitive Types
    Value Classes
    Representation Transformations

    View full-size slide

  21. scala-ldl.org
    Value Classes
    Value Classes

    View full-size slide

  22. scala-ldl.org
    Value Classes
    Value Classes
    value class

    View full-size slide

  23. scala-ldl.org
    Value Classes
    Value Classes
    value class
    struct (by-val)

    preferred encoding

    fields are inlined

    no heap allocations

    View full-size slide

  24. scala-ldl.org
    Value Classes
    Value Classes
    value class
    struct (by-val)

    preferred encoding

    fields are inlined

    no heap allocations

    fallback encoding

    compatible with

    subtyping

    erased generics
    object (by-ref)

    View full-size slide

  25. scala-ldl.org
    Value Classes
    Value Classes
    value class
    struct (by-val)

    preferred encoding

    fields are inlined

    no heap allocations

    fallback encoding

    compatible with

    subtyping

    erased generics
    object (by-ref)
    scalac
    Choice of representation
    1
    2 Coercions between representations

    View full-size slide

  26. scala-ldl.org
    Unboxing Primitive Types
    Value Classes
    Miniboxing
    Representation Transformations

    View full-size slide

  27. scala-ldl.org
    Miniboxing
    Miniboxing
    OOPSLA '13

    View full-size slide

  28. scala-ldl.org
    Miniboxing
    Miniboxing
    T (primitive)

    View full-size slide

  29. scala-ldl.org
    Miniboxing
    Miniboxing
    long integer

    preferred encoding

    for all primitive types
    T (primitive)

    View full-size slide

  30. scala-ldl.org
    Miniboxing
    Miniboxing
    long integer

    preferred encoding

    for all primitive types

    fallback encoding

    compatible with

    virtual dispatch

    subtyping

    erased generics
    T (erased to Object)
    T (primitive)

    View full-size slide

  31. scala-ldl.org
    Miniboxing
    Miniboxing
    long integer

    preferred encoding

    for all primitive types

    fallback encoding

    compatible with

    virtual dispatch

    subtyping

    erased generics
    T (erased to Object)
    T (primitive)
    scalac
    Choice of representation
    1
    2 Coercions between representations

    View full-size slide

  32. scala-ldl.org
    Unboxing Primitive Types
    Value Classes
    Representation Transformations
    Miniboxing

    View full-size slide

  33. scala-ldl.org
    Unboxing Primitive Types
    Value Classes
    Representation Transformations
    Miniboxing
    motivated by
    erased generics

    View full-size slide

  34. scala-ldl.org
    Unboxing Primitive Types
    Value Classes
    Representation Transformations
    Miniboxing
    motivated by
    erased generics
    Staging (Multi-Stage Programming)

    View full-size slide

  35. scala-ldl.org
    Staging
    Staging
    Program
    Result
    1+1+3
    5
    1-stage execution

    View full-size slide

  36. scala-ldl.org
    Staging
    Staging
    Program
    Result
    1+1+3
    5
    1-stage execution
    Program
    Program
    Result
    2+3
    1+1+3
    5
    2-stage execution

    View full-size slide

  37. scala-ldl.org
    Staging
    Staging
    value

    View full-size slide

  38. scala-ldl.org
    Staging
    Staging
    value
    direct value (5)

    is a computed value

    from an expression
    evaluated in the
    current stage

    View full-size slide

  39. scala-ldl.org
    Staging
    Staging
    value
    direct value (5)

    is a computed value

    from an expression
    evaluated in the
    current stage

    executed in the next stage

    stores the expression that
    produces the value
    lifted expression (2+3)

    View full-size slide

  40. scala-ldl.org
    scalac
    Staging
    Staging
    value
    direct value (5)

    is a computed value

    from an expression
    evaluated in the
    current stage

    executed in the next stage

    stores the expression that
    produces the value
    lifted expression (2+3)
    Choice of representation – domain-specific
    1
    2 Coercions between representations

    View full-size slide

  41. scala-ldl.org
    Motivation
    Transformation
    Conclusion
    Properties
    Benchmarks

    View full-size slide

  42. scala-ldl.org
    How to transform a program?

    View full-size slide

  43. scala-ldl.org
    How to transform a program?
    We'll use primitive unboxing
    as the running example,
    to keep things simple

    View full-size slide

  44. scala-ldl.org
    Syntax-based transformation
    Late Data Layout transformation

    View full-size slide

  45. scala-ldl.org
    Syntax-based transformation
    Late Data Layout transformation

    View full-size slide

  46. scala-ldl.org
    Syntax-based
    Syntax-based

    we need coercions between representations

    simple set of syntax-based rules
    – example

    View full-size slide

  47. scala-ldl.org
    Syntax-based
    Syntax-based
    val x: Int = ...
    val y: Int = x

    View full-size slide

  48. scala-ldl.org
    Syntax-based
    Syntax-based
    val x: Int = ...
    val y: Int = x

    View full-size slide

  49. scala-ldl.org
    Syntax-based
    Syntax-based
    val x: Int = ...
    val y: Int = x
    val x: int = unbox(...)
    val y: Int = box(x)

    View full-size slide

  50. scala-ldl.org
    Syntax-based
    Syntax-based
    val x: Int = ...
    val y: Int = x
    val x: int = unbox(...)
    val y: Int = box(x)
    Coerce the definition
    right-hand side

    View full-size slide

  51. scala-ldl.org
    Syntax-based
    Syntax-based
    val x: Int = ...
    val y: Int = x
    val x: int = unbox(...)
    val y: Int = box(x)
    Coerce all occurences of
    the transformed value

    View full-size slide

  52. scala-ldl.org
    Syntax-based
    Syntax-based
    val x: Int = ...
    val y: Int = x
    val x: int = unbox(...)
    val y: Int = box(x)

    View full-size slide

  53. scala-ldl.org
    Syntax-based
    Syntax-based
    val x: Int = ...
    val y: Int = x
    val x: int = unbox(...)
    val y: Int = box(x)
    val x: int = unbox(...)
    val y: int = unbox(box(x))

    View full-size slide

  54. scala-ldl.org
    Syntax-based
    Syntax-based
    val x: Int = ...
    val y: Int = x
    val x: int = unbox(...)
    val y: Int = box(x)
    val x: int = unbox(...)
    val y: int = unbox(box(x))
    suboptimal

    View full-size slide

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

    View full-size slide

  56. scala-ldl.org
    Syntax-based
    Syntax-based
    another example

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  62. scala-ldl.org
    Syntax-based
    Syntax-based
    def choice(t1: int, t2: int): Int =
    if (Random.nextBoolean())
    box(t1)
    else
    box(t2)
    Anything missing?
    Yes, unboxing the returned value

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  69. scala-ldl.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 full-size slide

  70. scala-ldl.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 full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  75. scala-ldl.org
    Syntax-based
    Syntax-based

    peephole transformation does not scale
    – needs multiple rewrite rules for each node
    – needs stateful rewrite rules
    – leads to an explosion of rules x states
    Details in
    the paper

    View full-size slide

  76. scala-ldl.org
    Coercions are fixed in the tree

    View full-size slide

  77. scala-ldl.org
    Coercions are fixed in the tree
    and moving them around is difficult.

    View full-size slide

  78. scala-ldl.org
    Coercions are fixed in the tree
    and moving them around is difficult.
    We need a more fluid abstraction.

    View full-size slide

  79. scala-ldl.org
    Coercions are fixed in the tree
    and moving them around is difficult.
    We need a more fluid abstraction.
    Types

    View full-size slide

  80. scala-ldl.org
    Syntax-based transformation
    Late Data Layout transformation

    View full-size slide

  81. scala-ldl.org
    Inject
    Coerce
    Commit
    Phases
    Late Data Layout transformation

    View full-size slide

  82. scala-ldl.org
    LDL Transformation
    LDL Transformation

    propagates representation information
    – into the type system

    based on annotated types

    e.g. an @unboxed annotation added to integers
    The Inject Phase
    The Inject Phase

    View full-size slide

  83. scala-ldl.org
    LDL Transformation
    LDL Transformation
    def choice(t1: Int,
    t2: Int): Int =
    if (Random.nextBoolean())
    t1
    else
    t2
    inject
    coerce
    commit

    View full-size slide

  84. scala-ldl.org
    LDL Transformation
    LDL Transformation
    def choice(t1: @unboxed Int,
    t2: @unboxed Int): @unboxed Int =
    if (Random.nextBoolean())
    t1
    else
    t2
    inject
    coerce
    commit

    View full-size slide

  85. scala-ldl.org
    LDL Transformation
    LDL Transformation
    def choice(t1: @unboxed Int,
    t2: @unboxed Int): @unboxed Int =
    if (Random.nextBoolean())
    t1
    else
    t2
    inject
    coerce
    commit
    depending on the transformation, other
    operations can be performed as well
    (e.g. miniboxing duplicates methods)

    View full-size slide

  86. scala-ldl.org
    Inject
    Coerce
    Commit
    Phases
    Late Data Layout transformation

    View full-size slide

  87. scala-ldl.org
    LDL Transformation
    LDL Transformation

    introduces coercions
    – re-type-checks the tree
    – exposes representation mismatches

    as annotation mismatches (Int vs @unboxed Int)

    leading to coercions
    The Coerce Phase
    The Coerce Phase

    View full-size slide

  88. scala-ldl.org
    LDL Transformation
    LDL Transformation
    def choice(t1: @unboxed Int,
    t2: @unboxed Int): @unboxed Int =
    if (Random.nextBoolean())
    t1
    else
    t2
    inject
    coerce
    commit

    View full-size slide

  89. scala-ldl.org
    LDL Transformation
    LDL Transformation
    def choice(t1: @unboxed Int,
    t2: @unboxed Int): @unboxed Int =
    if (Random.nextBoolean())
    t1
    else
    t2
    inject
    coerce
    commit

    View full-size slide

  90. scala-ldl.org
    LDL Transformation
    LDL Transformation
    def choice(t1: @unboxed Int,
    t2: @unboxed Int): @unboxed Int =
    if (Random.nextBoolean())
    t1
    else
    t2
    inject
    coerce
    commit
    the return type of choice
    is @unboxed Int

    View full-size slide

  91. scala-ldl.org
    LDL Transformation
    LDL Transformation
    def choice(t1: @unboxed Int,
    t2: @unboxed Int): @unboxed Int =
    if (Random.nextBoolean())
    t1
    else
    t2
    inject
    coerce
    commit
    the return type of choice
    is @unboxed Int

    View full-size slide

  92. scala-ldl.org
    LDL Transformation
    LDL Transformation
    def choice(t1: @unboxed Int,
    t2: @unboxed Int): @unboxed Int =
    if (Random.nextBoolean())
    t1
    else
    t2
    inject
    coerce
    commit
    : @unboxed Int
    the return type of choice
    is @unboxed Int

    View full-size slide

  93. scala-ldl.org
    LDL Transformation
    LDL Transformation
    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 return type of choice
    is @unboxed Int

    View full-size slide

  94. scala-ldl.org
    LDL Transformation
    LDL Transformation
    def choice(t1: @unboxed Int,
    t2: @unboxed Int): @unboxed Int =
    if (Random.nextBoolean())
    t1
    else
    t2
    inject
    coerce
    commit
    : @unboxed Int

    View full-size slide

  95. scala-ldl.org
    LDL Transformation
    LDL Transformation
    def choice(t1: @unboxed Int,
    t2: @unboxed Int): @unboxed Int =
    if (Random.nextBoolean())
    t1
    else
    t2
    inject
    coerce
    commit
    : Boolean

    View full-size slide

  96. scala-ldl.org
    LDL Transformation
    LDL Transformation
    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 full-size slide

  97. scala-ldl.org
    LDL Transformation
    LDL Transformation
    def choice(t1: @unboxed Int,
    t2: @unboxed Int): @unboxed Int =
    if (Random.nextBoolean())
    t1
    else
    t2
    inject
    coerce
    commit
    : @unboxed Int

    View full-size slide

  98. scala-ldl.org
    LDL Transformation
    LDL Transformation
    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 full-size slide

  99. scala-ldl.org
    LDL Transformation
    LDL Transformation
    def choice(t1: @unboxed Int,
    t2: @unboxed Int): @unboxed Int =
    if (Random.nextBoolean())
    t1
    else
    t2
    inject
    coerce
    commit
    : @unboxed Int

    View full-size slide

  100. scala-ldl.org
    LDL Transformation
    LDL Transformation
    def choice(t1: @unboxed Int,
    t2: @unboxed Int): @unboxed Int =
    if (Random.nextBoolean())
    t1
    else
    t2
    inject
    coerce
    commit
    matches:
    ...
    : @unboxed Int

    View full-size slide

  101. scala-ldl.org
    LDL Transformation
    LDL Transformation
    def choice(t1: @unboxed Int,
    t2: @unboxed Int): @unboxed Int =
    if (Random.nextBoolean())
    t1
    else
    t2
    inject
    coerce
    commit

    View full-size slide

  102. scala-ldl.org
    LDL Transformation
    LDL Transformation
    def choice(t1: @unboxed Int,
    t2: @unboxed Int): @unboxed Int =
    if (Random.nextBoolean())
    t1
    else
    t2
    inject
    coerce
    commit
    LDL optimally transforms
    the tree the first time

    View full-size slide

  103. scala-ldl.org
    LDL Transformation
    LDL Transformation
    def choice(t1: @unboxed Int,
    t2: @unboxed Int): @unboxed Int =
    if (Random.nextBoolean())
    t1
    else
    t2
    inject
    coerce
    commit
    LDL optimally transforms
    the tree the first time
    No peephole transformation

    View full-size slide

  104. scala-ldl.org
    Inject
    Coerce
    Commit
    Phases
    Late Data Layout transformation

    View full-size slide

  105. scala-ldl.org
    LDL Transformation
    LDL Transformation

    converts annotations to representations
    – @unboxed Int → int
    – Int java.lang.Integer


    coercion markers are also transformed
    – box(...) → new Integer(...)
    – unbox(...) → ....intValue
    The Commit Phase
    The Commit Phase

    View full-size slide

  106. scala-ldl.org
    LDL Transformation
    LDL Transformation
    def choice(t1: @unboxed Int,
    t2: @unboxed Int): @unboxed Int =
    if (Random.nextBoolean())
    t1
    else
    t2
    inject
    coerce
    commit

    View full-size slide

  107. scala-ldl.org
    LDL Transformation
    LDL Transformation
    def choice(t1: int,
    t2: int): int =
    if (Random.nextBoolean())
    t1
    else
    t2
    inject
    coerce
    commit

    View full-size slide

  108. scala-ldl.org
    LDL Transformation
    LDL Transformation
    def choice(t1: int,
    t2: int): int =
    if (Random.nextBoolean())
    t1
    else
    t2
    inject
    coerce
    commit
    that's it!

    View full-size slide

  109. scala-ldl.org
    Inject
    Coerce
    Commit
    Phases
    Late Data Layout transformation

    View full-size slide

  110. scala-ldl.org
    Motivation
    Transformation
    Conclusion
    Properties
    Benchmarks

    View full-size slide

  111. scala-ldl.org
    Selectivity
    Consistency
    Optimality (not formally proven yet)
    Properties
    Late Data Layout transformation

    View full-size slide

  112. scala-ldl.org
    Selectivity
    Selectivity inject
    coerce
    commit

    annotated types
    – selectively pick the representation for each value

    View full-size slide

  113. scala-ldl.org
    Selectivity
    Selectivity inject
    coerce
    commit

    annotated types
    – selectively pick the representation for each value

    selectivity is used for
    – bridge methods (some args boxed, others unboxed)
    – value classes (JVM: no multi-value returns)
    – staging (representation: domain-specific knowledge)

    List[Int] vs List[@staged Int] vs @staged List[Int]

    View full-size slide

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

    View full-size slide

  115. scala-ldl.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 full-size slide

  116. scala-ldl.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 full-size slide

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

    View full-size slide

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

    View full-size slide

  119. scala-ldl.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 full-size slide

  120. scala-ldl.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 full-size slide

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

    View full-size slide

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

    View full-size slide

  123. scala-ldl.org
    Selectivity
    Selectivity
    def choice(t1: java.lang.Integer,
    t2: int): int =
    if (Random.nextBoolean())
    t1.intValue
    else
    t2
    inject
    coerce
    commit

    View full-size slide

  124. scala-ldl.org
    Selectivity
    Consistency
    Optimality (not formally proven yet)
    Properties
    Late Data Layout transformation

    View full-size slide

  125. scala-ldl.org
    Consistency
    Consistency inject
    coerce
    commit

    representations become part of types

    re-type-checking the program
    – proves type correctness
    – proves representation consistency

    View full-size slide

  126. scala-ldl.org
    Selectivity
    Consistency
    Optimality (not formally proven yet)
    Properties
    Late Data Layout transformation

    View full-size slide

  127. scala-ldl.org
    Optimality
    Optimality
    def choice(t1: java.lang.Integer,
    t2: int): int =
    if (Random.nextBoolean())
    t1.intValue
    else
    t2
    inject
    coerce
    commit

    View full-size slide

  128. scala-ldl.org
    Optimality
    Optimality
    def choice(t1: java.lang.Integer,
    t2: int): int =
    if (Random.nextBoolean())
    t1.intValue
    else
    t2
    execution
    inject
    coerce
    commit

    View full-size slide

  129. scala-ldl.org
    Optimality
    Optimality
    def choice(t1: java.lang.Integer,
    t2: int): int =
    if (Random.nextBoolean())
    t1.intValue
    else
    t2
    1 coercion
    execution
    inject
    coerce
    commit

    View full-size slide

  130. scala-ldl.org
    Optimality
    Optimality
    def choice(t1: java.lang.Integer,
    t2: int): int =
    if (Random.nextBoolean())
    t1.intValue
    else
    t2
    1 coercion
    no coercions
    execution
    inject
    coerce
    commit

    View full-size slide

  131. scala-ldl.org
    Optimality
    Optimality

    on any execution trace through the program
    – the number of coercions executed is minimum
    – assuming the program terminates
    inject
    coerce
    commit

    View full-size slide

  132. scala-ldl.org
    Optimality
    Optimality

    on any execution trace through the program
    – the number of coercions executed is minimum
    – assuming the program terminates

    modulo
    – annotations introduced by the inject phase

    unbox both parameters → no coercions at all
    inject
    coerce
    commit

    View full-size slide

  133. scala-ldl.org
    Optimality
    Optimality

    on any execution trace through the program
    – the number of coercions executed is minimum
    – assuming the program terminates

    modulo
    – annotations introduced by the inject phase

    unbox both parameters → no coercions at all
    – post-transformations done by the commit phase

    box(...) → new Integer(new Integer(...).intValue)
    inject
    coerce
    commit

    View full-size slide

  134. scala-ldl.org
    Optimality
    Optimality

    peephole optimization
    – propagates coercions

    type system
    – propagates types
    inject
    coerce
    commit

    View full-size slide

  135. scala-ldl.org
    Optimality
    Optimality

    peephole optimization
    – propagates coercions

    type system
    – propagates types
    – but types are fluid whereas coercions are not
    details in the paper
    inject
    coerce
    commit

    View full-size slide

  136. scala-ldl.org
    Selectivity
    Consistency
    Optimality (not formally proven yet)
    Properties
    Late Data Layout transformation

    View full-size slide

  137. scala-ldl.org
    Motivation
    Transformation
    Conclusion
    Properties
    Benchmarks

    View full-size slide

  138. scala-ldl.org
    LDL is used in
    LDL is used in

    Scala compiler plugins
    – miniboxing (specialization)
    – value-class plugin
    – staging plugin

    View full-size slide

  139. scala-ldl.org
    Benchmarks
    Benchmarks

    … in the paper
    in the paper

    implementation effort
    – Late Data Layout mechanism

    developed as part of miniboxing

    reused by the other compiler plugins
    – value class plugin → 2 developer-weeks
    – staging plugin → 1 developer-week

    View full-size slide

  140. scala-ldl.org
    Benchmarks
    Benchmarks

    … in the paper
    in the paper

    performance
    – baseline vs transformed code

    numbers
    – up to 2x speedup when transforming value classes
    – up to 22x speedup when using miniboxing
    – up to 59x speedup when staging

    View full-size slide

  141. scala-ldl.org
    Motivation
    Transformation
    Conclusion
    Properties
    Benchmarks

    View full-size slide

  142. scala-ldl.org
    Conclusion
    Conclusion
    Insights
    Insights

    use annotated types
    – to selectively mark values with the representation

    use expected type propagation
    – to provide optimal transformation

    use the type system
    – to provide representation consistency

    View full-size slide

  143. scala-ldl.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

    Xiaoya Xiang and Philip Stutz for trusting miniboxing enough to try it out
    Special thanks to the Scala Community for their support!
    (@StuHood, @vpatryshev and everyone else!)

    View full-size slide

  144. scala-ldl.org
    concept
    repr. 1 … repr. n
    repr. 2

    View full-size slide

  145. scala-ldl.org
    concept
    repr. 1 … repr. n
    repr. 2
    How would
    you use this?

    View full-size slide

  146. scala-ldl.org
    Thank you!
    concept
    repr. 1 … repr. n
    repr. 2
    How would
    you use this?

    View full-size slide