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

Miniboxing Library - Scala.world

Vlad Ureche
September 13, 2016

Miniboxing Library - Scala.world

Miniboxing Library - Scala.world

Vlad Ureche

September 13, 2016
Tweet

More Decks by Vlad Ureche

Other Decks in Programming

Transcript

  1. scala-miniboxing.org @miniboxing
    12th of September 2016
    scala.world
    Penrith, UK
    The Story of a Parallel Scala Library

    View full-size slide

  2. scala-miniboxing.org @miniboxing
    The Story of a Parallel Scala Library
    The constructs used by miniboxing to optimize your code
    12th of September 2016
    scala.world
    Penrith, UK

    View full-size slide

  3. scala-miniboxing.org @miniboxing
    Miniboxing
    Miniboxing

    Compiler transformation

    View full-size slide

  4. scala-miniboxing.org @miniboxing
    Miniboxing
    Miniboxing

    Compiler transformation

    Improves performance of generics
    – When instantiated by primitive types

    View full-size slide

  5. scala-miniboxing.org @miniboxing
    Miniboxing
    Miniboxing

    Compiler transformation

    Improves performance of generics
    – When instantiated by primitive types

    Introduces optimized constructs
    – Automatically
    – Or with programmer help

    View full-size slide

  6. scala-miniboxing.org @miniboxing
    Miniboxing
    Miniboxing

    Compiler transformation

    Improves performance of generics
    – When instantiated by primitive types

    Introduces optimized constructs
    – Automatically
    – Or with programmer help

    View full-size slide

  7. scala-miniboxing.org @miniboxing
    STOP
    Please ask if things
    are not clear!

    View full-size slide

  8. scala-miniboxing.org @miniboxing
    Motivation
    Motivation

    We build cool new constructs and paradigms
    for {
    conn getConnection \/> "Could not connect"

    ids conn.get("id") \/> "Coult not get ids"

    } yield user

    View full-size slide

  9. scala-miniboxing.org @miniboxing
    Motivation
    Motivation

    We build cool new constructs and paradigms

    But some constructs are deeply integrated

    Functions

    Tuples

    Arrays

    Ordering

    Numeric
    for {
    conn getConnection \/> "Could not connect"

    ids conn.get("id") \/> "Coult not get ids"

    } yield user

    View full-size slide

  10. scala-miniboxing.org @miniboxing
    Motivation
    Motivation
    val x = (x: Int) => x

    View full-size slide

  11. scala-miniboxing.org @miniboxing
    Motivation
    Motivation
    val x = (x: Int) => x
    Function syntax is sugar
    for anonymous classes
    extending FunctionX traits
    (or for Java8 lambdas)

    View full-size slide

  12. scala-miniboxing.org @miniboxing
    Motivation
    Motivation
    val x = (x: Int) => x
    Extending Function1 behavior?
    Sure, add an implicit.
    Function syntax is sugar
    for anonymous classes
    extending FunctionX traits
    (or for Java8 lambdas)

    View full-size slide

  13. scala-miniboxing.org @miniboxing
    Motivation
    Motivation
    val x = (x: Int) => x
    Extending Function1 behavior?
    Sure, add an implicit.
    Changing behavior (e.g. PartialFunction)?
    This is what we'll talk about today.
    Function syntax is sugar
    for anonymous classes
    extending FunctionX traits
    (or for Java8 lambdas)

    View full-size slide

  14. scala-miniboxing.org @miniboxing
    Extending vs Changing Behavior
    Extending vs Changing Behavior
    Adding a new
    method to the
    trait/class

    View full-size slide

  15. scala-miniboxing.org @miniboxing
    Extending vs Changing Behavior
    Extending vs Changing Behavior
    Adding a new
    method to the
    trait/class
    Making it
    conform to an
    interface

    View full-size slide

  16. scala-miniboxing.org @miniboxing
    Extending vs Changing Behavior
    Extending vs Changing Behavior
    Adding a new
    method to the
    trait/class
    Making it
    conform to an
    interface
    Changing a
    method's
    signature

    View full-size slide

  17. scala-miniboxing.org @miniboxing
    Extending vs Changing Behavior
    Extending vs Changing Behavior
    Adding a new
    method to the
    trait/class
    Making it
    conform to an
    interface
    Changing a
    method's
    signature
    Updating
    the body of
    a method

    View full-size slide

  18. scala-miniboxing.org @miniboxing
    Extending vs Changing Behavior
    Extending vs Changing Behavior
    Adding a new
    method to the
    trait/class
    Making it
    conform to an
    interface
    Changing a
    method's
    signature
    Updating
    the body of
    a method
    Changing the
    fields of a class

    View full-size slide

  19. scala-miniboxing.org @miniboxing
    Extending vs Changing Behavior
    Extending vs Changing Behavior
    Adding a new
    method to the
    trait/class
    Making it
    conform to an
    interface
    Changing a
    method's
    signature
    Updating
    the body of
    a method
    Changing the
    fields of a class
    Extending behavior Changing behavior

    View full-size slide

  20. scala-miniboxing.org @miniboxing
    Extending vs Changing Behavior
    Extending vs Changing Behavior
    Why would you need that?
    Adding a new
    method to the
    trait/class
    Making it
    conform to an
    interface
    Changing a
    method's
    signature
    Updating
    the body of
    a method
    Changing the
    fields of a class
    Extending behavior Changing behavior

    View full-size slide

  21. scala-miniboxing.org @miniboxing
    Functions
    Transformation
    Experiment
    Conclusion
    Miniboxing

    View full-size slide

  22. scala-miniboxing.org @miniboxing
    Functions
    Transformation
    Experiment
    Conclusion
    Miniboxing
    Data Representation
    Mixing Representations
    Library Interoperation

    View full-size slide

  23. scala-miniboxing.org @miniboxing
    Functions
    Transformation
    Experiment
    Conclusion
    Miniboxing
    Data Representation
    Mixing Representations
    Library Interoperation

    View full-size slide

  24. scala-miniboxing.org @miniboxing
    Erased Generics
    Erased Generics
    def identity[T](t: T): T = t
    def identity(t: Object): Object = t
    scalac / javac

    View full-size slide

  25. scala-miniboxing.org @miniboxing
    Erased Generics
    Erased Generics
    identity(5)
    identity(java.lang.Integer.valueOf(5))
    scalac / javac

    View full-size slide

  26. scala-miniboxing.org @miniboxing
    Erased Generics
    Erased Generics
    identity(5)
    identity(java.lang.Integer.valueOf(5))
    scalac / javac
    boxed data representation

    View full-size slide

  27. scala-miniboxing.org @miniboxing
    Erased Generics
    Erased Generics
    java.lang.Integer.valueOf(5)
    boxing is not great ...
    boxing is not great ...
    inflates heap requirements
    produces garbage
    value is accessed indirectly (slow)
    breaks locality guarantees

    View full-size slide

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

    View full-size slide

  29. scala-miniboxing.org @miniboxing
    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 full-size slide

  30. scala-miniboxing.org @miniboxing
    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 full-size slide

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

    View full-size slide

  32. scala-miniboxing.org @miniboxing
    Specialization
    Specialization
    identity(5)
    identity_I(5)
    specialization
    No boxing, it automatically
    calls the specialized
    variant for integers

    View full-size slide

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

    View full-size slide

  34. scala-miniboxing.org @miniboxing
    Specialization
    Specialization
    def tupled[T1, T2](t1: T1, t2: T2) ...
    // 100 methods (102)
    specialization
    Can we do
    better?

    View full-size slide

  35. scala-miniboxing.org @miniboxing
    The idea behind
    The idea behind
    ...
    LONG
    DOUBLE
    INT
    FLOAT
    SHORT
    ...
    LONG
    DOUBLE
    INT
    FLOAT
    SHORT
    a long integer
    miniboxing
    miniboxing

    View full-size slide

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

    View full-size slide

  37. scala-miniboxing.org @miniboxing
    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 full-size slide

  38. scala-miniboxing.org @miniboxing
    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 full-size slide

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

    View full-size slide

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

    View full-size slide

  41. scala-miniboxing.org @miniboxing
    Miniboxing
    Miniboxing
    identity(3)
    identity_M(..., int2minibox(3))
    miniboxing
    The miniboxed
    variant of identity
    Unlike boxing,
    these conversions
    do not affect
    performance

    View full-size slide

  42. scala-miniboxing.org @miniboxing
    Functions
    Transformation
    Experiment
    Conclusion
    Miniboxing
    Data Representation
    Mixing Representations
    Library Interoperation

    View full-size slide

  43. scala-miniboxing.org @miniboxing
    Functions
    Transformation
    Experiment
    Conclusion
    Miniboxing
    Data Representation
    Mixing Representations
    Library Interoperation

    View full-size slide

  44. scala-miniboxing.org @miniboxing
    Call Graph
    Call Graph
    def foo[T](t: T): T = bar(t)
    def bar[T](t: T): T = baz(t)
    def baz[T](t: T): T = t
    call graph

    View full-size slide

  45. scala-miniboxing.org @miniboxing
    Call Graph
    Call Graph
    foo
    bar
    baz

    View full-size slide

  46. scala-miniboxing.org @miniboxing
    Miniboxed Call Graph
    Miniboxed Call Graph
    def foo[@miniboxed T](t: T): T = bar(t)
    def bar[@miniboxed T](t: T): T = baz(t)
    def baz[@miniboxed T](t: T): T = t
    miniboxing

    View full-size slide

  47. scala-miniboxing.org @miniboxing
    Miniboxed Call Graph
    Miniboxed Call Graph
    def foo(...): ... = bar(...)
    def foo_M(...): ... = bar_M(...)
    def bar(...): ... = baz(...)
    def bar_M(...): ... = baz_M(...)
    def baz(...): ... = t
    def baz_M(...): ... = t

    View full-size slide

  48. scala-miniboxing.org @miniboxing
    Miniboxed Call Graph
    Miniboxed Call Graph
    foo foo_M
    bar bar_M
    baz baz_M

    View full-size slide

  49. scala-miniboxing.org @miniboxing
    Miniboxed Call Graph
    Miniboxed Call Graph
    foo foo_M
    bar bar_M
    baz baz_M
    Data is passed in the
    miniboxed encoding
    no boxing necessary!

    View full-size slide

  50. scala-miniboxing.org @miniboxing
    Suboptimal Call Graph
    Suboptimal Call Graph
    def foo[@miniboxed T](t: T): T = bar(t)
    def bar[@miniboxed T](t: T): T = baz(t)
    def baz[@miniboxed T](t: T): T = t
    miniboxing

    View full-size slide

  51. scala-miniboxing.org @miniboxing
    Suboptimal Call Graph
    Suboptimal Call Graph
    foo foo_M
    bar
    baz baz_M

    View full-size slide

  52. scala-miniboxing.org @miniboxing
    Suboptimal Call Graph
    Suboptimal Call Graph
    foo foo_M
    bar
    baz baz_M
    no bar_M to call box

    View full-size slide

  53. scala-miniboxing.org @miniboxing
    Suboptimal Call Graph
    Suboptimal Call Graph
    foo foo_M
    bar
    baz baz_M
    no bar_M to call box

    View full-size slide

  54. scala-miniboxing.org @miniboxing
    Suboptimal Call Graph
    Suboptimal Call Graph
    foo foo_M
    bar
    baz baz_M
    no bar_M to call box

    View full-size slide

  55. scala-miniboxing.org @miniboxing
    Suboptimal Call Graph
    Suboptimal Call Graph
    foo foo_M
    bar
    baz baz_M
    no bar_M to call box

    not enough info to return
    to the optimized trace

    View full-size slide

  56. scala-miniboxing.org @miniboxing
    Suboptimal Call Graph
    Suboptimal Call Graph
    scala> object Test {
    | def foo[@miniboxed T](t: T): T = bar(t)
    | def bar[ T](t: T): T = baz(t)
    | def baz[@miniboxed T](t: T): T = t
    | }
    :10: warning: The method Test.bar would benefit
    from miniboxing type parameter T, since it is instantiated
    by miniboxed type parameter T of method foo:
    def foo[@miniboxed T](t: T): T = bar(t)
    ^
    :11: warning: The following code could benefit
    from miniboxing specialization if the type parameter T
    of method bar would be marked as "@miniboxed T":
    def bar[T](t: T): T = baz(t)
    ^
    defined object Test

    View full-size slide

  57. scala-miniboxing.org @miniboxing
    Functions
    Transformation
    Experiment
    Conclusion
    Miniboxing
    Data Representation
    Mixing Representations
    Library Interoperation

    View full-size slide

  58. scala-miniboxing.org @miniboxing
    Functions
    Transformation
    Experiment
    Conclusion
    Miniboxing
    Data Representation
    Mixing Representations
    Library Interoperation

    View full-size slide

  59. scala-miniboxing.org @miniboxing
    Library Interoperation
    Library Interoperation
    scala> def identityFun[T] = (t: T) => t
    defined method identityFun

    View full-size slide

  60. scala-miniboxing.org @miniboxing
    Library Interoperation
    Library Interoperation
    scala> def identityFun[T] = (t: T) => t
    defined method identityFun
    scala> identityFun(3)
    :11: warning: The method identityFun would benefit
    from miniboxing type parameter T, since it is instantiated
    by a primitive type.
    identityFun(3)
    ^
    res1: Int = 3

    View full-size slide

  61. scala-miniboxing.org @miniboxing
    scala> def identityFun[@miniboxed T] = (t: T) => t
    Library Interoperation
    Library Interoperation

    View full-size slide

  62. scala-miniboxing.org @miniboxing
    scala> def identityFun[@miniboxed T] = (t: T) => t
    :12: warning: The type parameter T1 of trait
    scala.Function1 is specialized but to interoperate
    efficiently with miniboxing, it would have to be miniboxed.
    def identityFun[@miniboxed T] = (t: T) => t
    ^
    :12: warning: The type parameter R of trait
    scala.Function1 is specialized but to interoperate
    efficiently with miniboxing, it would have to be miniboxed.
    def identityFun[@miniboxed T] = (t: T) => t
    ^
    defined method identityFun
    Library Interoperation
    Library Interoperation

    View full-size slide

  63. scala-miniboxing.org @miniboxing
    scala> def identityFun[@miniboxed T] = (t: T) => t
    :12: warning: The type parameter T1 of trait
    scala.Function1 is specialized but to interoperate
    efficiently with miniboxing, it would have to be miniboxed.
    def identityFun[@miniboxed T] = (t: T) => t
    ^
    :12: warning: The type parameter R of trait
    scala.Function1 is specialized but to interoperate
    efficiently with miniboxing, it would have to be miniboxed.
    def identityFun[@miniboxed T] = (t: T) => t
    ^
    defined method identityFun
    Library Interoperation
    Library Interoperation
    Not actionable

    View full-size slide

  64. scala-miniboxing.org @miniboxing
    scala> def identityFun[@miniboxed T] = (t: T) => t
    :12: warning: The type parameter T1 of trait
    scala.Function1 is specialized but to interoperate
    efficiently with miniboxing, it would have to be miniboxed.
    def identityFun[@miniboxed T] = (t: T) => t
    ^
    :12: warning: The type parameter R of trait
    scala.Function1 is specialized but to interoperate
    efficiently with miniboxing, it would have to be miniboxed.
    def identityFun[@miniboxed T] = (t: T) => t
    ^
    defined method identityFun
    Library Interoperation
    Library Interoperation
    Not actionable
    Need to fix it

    View full-size slide

  65. scala-miniboxing.org @miniboxing
    scala> def identityFun[@miniboxed T] = (t: T) => t
    :12: warning: The type parameter T1 of trait
    scala.Function1 is specialized but to interoperate
    efficiently with miniboxing, it would have to be miniboxed.
    def identityFun[@miniboxed T] = (t: T) => t
    ^
    :12: warning: The type parameter R of trait
    scala.Function1 is specialized but to interoperate
    efficiently with miniboxing, it would have to be miniboxed.
    def identityFun[@miniboxed T] = (t: T) => t
    ^
    defined method identityFun
    Library Interoperation
    Library Interoperation
    Not actionable
    Need to fix it

    View full-size slide

  66. scala-miniboxing.org @miniboxing
    Library Interoperation
    Library Interoperation

    Multiple such constructs
    – Functions automatic, most interesting

    – Arrays need user intervention, warnings

    – Tuples automatic

    – Numeric automatic, but I was lazy

    View full-size slide

  67. scala-miniboxing.org @miniboxing
    Functions
    Transformation
    Experiment
    Conclusion
    Miniboxing
    Data Representation
    Mixing Representations
    Library Interoperation

    View full-size slide

  68. scala-miniboxing.org @miniboxing
    Functions
    Transformation
    Experiment
    Conclusion
    Miniboxing

    View full-size slide

  69. scala-miniboxing.org @miniboxing
    MbFunctionX
    MbFunctionX

    Miniboxed replacement for FunctionX

    Assumptions
    – Function created once
    – Called multiple times optimize for calling

    overhead

    View full-size slide

  70. scala-miniboxing.org @miniboxing
    MbFunctionX
    MbFunctionX

    Miniboxed replacement for FunctionX

    Assumptions
    – Function created once
    – Called multiple times optimize for calling

    overhead
    Let's see how we
    transform

    View full-size slide

  71. scala-miniboxing.org @miniboxing
    MbFunctionX
    MbFunctionX

    Three approaches
    – trait Function0[@miniboxed @specialized R]

    Incompatible transformations, won't fly
    – Call-site adaptation (implicit + value class)

    call overhead
    – Replace FunctionX body

    no call overhead

    View full-size slide

  72. scala-miniboxing.org @miniboxing
    Library Interoperation
    Library Interoperation
    scala> def identityFun[@miniboxed T] = (t: T) => t
    defined method identityFun

    View full-size slide

  73. scala-miniboxing.org @miniboxing
    Library Interoperation
    Library Interoperation
    scala> def identityFun[@miniboxed T] = (t: T) => t
    defined method identityFun
    def identityFun[@miniboxed T]: MbFunction1[T, T] = ...

    View full-size slide

  74. scala-miniboxing.org @miniboxing
    Library Interoperation
    Library Interoperation
    scala> def identityFun[@miniboxed T] = (t: T) => t
    defined method identityFun
    def identityFun[@miniboxed T]: MbFunction1[T, T] = ...
    Once this is done,
    miniboxing does its magic

    View full-size slide

  75. scala-miniboxing.org @miniboxing
    Library Interoperation
    Library Interoperation
    scala> def identityFun[@miniboxed T] = (t: T) => t
    defined method identityFun
    def identityFun[@miniboxed T]: MbFunction1[T, T] = ...
    Once this is done,
    miniboxing does its magic
    def identityFun[T]: MbFunction1[T, T] = {
    class $anon extends MbFunction1_L[T, T] {
    def apply(t: T) = t
    }
    new $anon()
    }
    def identityFun_M[Tsp](...): MbFunction1[T, T] = {
    class $anon extends MbFunction1_M[T, T](...) {
    def apply_M(..., t: T) = t
    }
    new $anon()
    }

    View full-size slide

  76. scala-miniboxing.org @miniboxing
    Library Interoperation
    Library Interoperation

    Multiple such constructs
    – Functions automatic, most interesting

    – Arrays need user intervention, warnings

    – Tuples automatic

    – Numeric automatic, but I was lazy

    View full-size slide

  77. scala-miniboxing.org @miniboxing
    Extending vs Changing Behavior
    Extending vs Changing Behavior
    Adding a new
    method to the
    trait/class
    Making it
    conform to an
    interface
    Changing a
    method's
    signature
    Updating
    the body of
    a method
    Changing the
    fields of a class
    Extending behavior Changing behavior

    View full-size slide

  78. scala-miniboxing.org @miniboxing
    Extending vs Changing Behavior
    Extending vs Changing Behavior
    Adding a new
    method to the
    trait/class
    Making it
    conform to an
    interface
    Changing a
    method's
    signature
    Updating
    the body of
    a method
    Changing the
    fields of a class
    Extending behavior Changing behavior
    Completely
    replacing a
    library class

    View full-size slide

  79. scala-miniboxing.org @miniboxing
    Functions
    Transformation
    Experiment
    Conclusion
    Miniboxing

    View full-size slide

  80. scala-miniboxing.org @miniboxing
    Functions
    Transformation
    Experiment
    Conclusion
    Miniboxing
    Dependencies
    Reverse deps
    Object model

    View full-size slide

  81. scala-miniboxing.org @miniboxing
    Functions
    Transformation
    Experiment
    Conclusion
    Miniboxing
    Dependencies
    Reverse deps
    Object model

    View full-size slide

  82. scala-miniboxing.org @miniboxing
    Dependencies
    Dependencies
    Your library

    View full-size slide

  83. scala-miniboxing.org @miniboxing
    Dependencies
    Dependencies
    Your library
    Scala stdlib Other library Other library dependencies

    View full-size slide

  84. scala-miniboxing.org @miniboxing
    Dependencies
    Dependencies
    Your library
    Scala stdlib Other library Other library dependencies

    View full-size slide

  85. scala-miniboxing.org @miniboxing
    Dependencies
    Dependencies
    Your library
    Scala stdlib Other library Other library dependencies
    Each library is free
    to use its desired
    FunctionX version

    View full-size slide

  86. scala-miniboxing.org @miniboxing
    Dependencies
    Dependencies
    Your library
    Scala stdlib Other library Other library dependencies
    Each library is free
    to use its desired
    FunctionX version
    Talk to each in its
    own "language"

    View full-size slide

  87. scala-miniboxing.org @miniboxing
    Dependencies
    Dependencies
    Your library
    Scala stdlib Other library Other library dependencies
    Each library is free
    to use its desired
    FunctionX version
    Talk to each in its
    own "language"
    Have to track of the
    transformations

    View full-size slide

  88. scala-miniboxing.org @miniboxing
    Marking Changes
    Marking Changes
    Source
    Signatures
    Bytecode
    Source
    Signatures
    Bytecode
    Source
    Signatures
    Bytecode
    JVM
    scalac
    scalac
    scalac

    View full-size slide

  89. scala-miniboxing.org @miniboxing
    Marking Changes
    Marking Changes
    Source
    Signatures
    Bytecode
    Source
    Signatures
    Bytecode
    Source
    Signatures
    Bytecode
    JVM
    scalac
    scalac
    scalac

    View full-size slide

  90. scala-miniboxing.org @miniboxing
    Marking Changes
    Marking Changes
    Source
    Signatures
    Bytecode
    Source
    Signatures
    Bytecode
    Source
    Signatures
    Bytecode
    JVM
    scalac
    scalac
    scalac

    View full-size slide

  91. scala-miniboxing.org @miniboxing
    Marking Changes
    Marking Changes

    Keep track of changes to
    – Methods
    – Fields
    – Classes
    – Traits

    Carry transformation info with types
    – Automatically annotate the code
    – The pickler phase stores them in signatures
    – Introduce conversions where necessary

    View full-size slide

  92. scala-miniboxing.org @miniboxing
    Marking Changes
    Marking Changes

    View full-size slide

  93. scala-miniboxing.org @miniboxing
    Marking Changes
    Marking Changes

    View full-size slide

  94. scala-miniboxing.org @miniboxing
    Marking Changes
    Marking Changes

    View full-size slide

  95. scala-miniboxing.org @miniboxing
    Marking Changes
    Marking Changes
    place a
    compiler
    phase here

    View full-size slide

  96. scala-miniboxing.org @miniboxing
    Marking Changes
    Marking Changes
    def identityFun[@miniboxed T] = (t: T) => t
    typer

    View full-size slide

  97. scala-miniboxing.org @miniboxing
    Marking Changes
    Marking Changes
    def identityFun[@miniboxed T] = (t: T) => t
    def identityFun[@miniboxed T]: Function1[T, T] =
    (t: T) => t
    typer
    interop-inject

    View full-size slide

  98. scala-miniboxing.org @miniboxing
    Marking Changes
    Marking Changes
    def identityFun[@miniboxed T] = (t: T) => t
    def identityFun[@miniboxed T]: Function1[T, T] =
    (t: T) => t
    typer
    interop-inject
    @...
    def identityFun[@miniboxed T]: Function1[T, T] @mbFun =
    (t: T) => t
    pickler

    View full-size slide

  99. scala-miniboxing.org @miniboxing
    Marking Changes
    Marking Changes
    def identityFun[@miniboxed T] = (t: T) => t
    def identityFun[@miniboxed T]: Function1[T, T] =
    (t: T) => t
    typer
    interop-inject
    @...
    def identityFun[@miniboxed T]: Function1[T, T] @mbFun =
    (t: T) => t
    Future compilation runs
    are aware of the change
    pickler

    View full-size slide

  100. scala-miniboxing.org @miniboxing
    Conversions
    Conversions

    What do to if
    – We have an MbFunction1 and
    – Callee expects Function1?

    Conversions
    – Between the two cases
    – Functions defined in miniboxed code

    Miniboxing-friendly and specialization-friendly
    – Functions defined in non-miniboxed code

    Specialization-friendly

    Need conversion Miniboxing friendly

    View full-size slide

  101. scala-miniboxing.org @miniboxing
    Conversions
    Conversions
    def identityFun[@miniboxed T] = Predef.identity
    typer

    View full-size slide

  102. scala-miniboxing.org @miniboxing
    Conversions
    Conversions
    def identityFun[@miniboxed T] = Predef.identity
    def identityFun[@miniboxed T]: Function1[T, T] =
    Predef.identity[T] // : Function1[T, T] from classpath
    typer
    interop-inject

    View full-size slide

  103. scala-miniboxing.org @miniboxing
    Conversions
    Conversions
    def identityFun[@miniboxed T] = Predef.identity
    def identityFun[@miniboxed T]: Function1[T, T] =
    Predef.identity[T] // : Function1[T, T] from classpath
    typer
    interop-inject
    @...
    def identityFun[@miniboxed T]: Function1[T, T] @mbFun =
    Predef.identity[T] // : Function1[T, T] no annotation
    interop-coerce

    View full-size slide

  104. scala-miniboxing.org @miniboxing
    @...
    def identityFun[@miniboxed T]: Function1[T, T] @mbFun =
    toMbFun(Predef.identity[T]) // : Function1[T, T] @mbFun
    Conversions
    Conversions
    def identityFun[@miniboxed T] = Predef.identity
    def identityFun[@miniboxed T]: Function1[T, T] =
    Predef.identity[T] // : Function1[T, T] from classpath
    typer
    interop-inject
    @...
    def identityFun[@miniboxed T]: Function1[T, T] @mbFun =
    Predef.identity[T] // : Function1[T, T] no annotation
    interop-coerce

    View full-size slide

  105. scala-miniboxing.org @miniboxing
    Conversions
    Conversions
    Conversions are introduced based on types
    See scala-miniboxing.org/ldl for details

    View full-size slide

  106. scala-miniboxing.org @miniboxing
    Functions
    Transformation
    Experiment
    Conclusion
    Miniboxing
    Dependencies
    Reverse deps
    Object model

    View full-size slide

  107. scala-miniboxing.org @miniboxing
    Functions
    Transformation
    Experiment
    Conclusion
    Miniboxing
    Dependencies
    Reverse deps
    Object model

    View full-size slide

  108. scala-miniboxing.org @miniboxing
    Reverse Dependencies
    Reverse Dependencies
    Your library

    View full-size slide

  109. scala-miniboxing.org @miniboxing
    Reverse Dependencies
    Reverse Dependencies
    Your library
    Scala stdlib Other library Other library dependencies

    View full-size slide

  110. scala-miniboxing.org @miniboxing
    Reverse Dependencies
    Reverse Dependencies
    Your library
    Scala stdlib Other library Other library
    Other lib 1 Other lib 2 Other lib 3
    dependencies
    reverse
    dependencies

    View full-size slide

  111. scala-miniboxing.org @miniboxing
    Reverse Dependencies
    Reverse Dependencies
    Your library
    Scala stdlib Other library Other library
    Other lib 1 Other lib 2 Other lib 3
    dependencies
    reverse
    dependencies

    View full-size slide

  112. scala-miniboxing.org @miniboxing
    Reverse Dependencies
    Reverse Dependencies
    Your library
    Scala stdlib Other library Other library
    Other lib 1 Other lib 2 Other lib 3
    dependencies
    reverse
    dependencies
    Libraries not compiled
    with the miniboxing
    plugin are not aware of
    the transformation →
    incorrect bytecode

    View full-size slide

  113. scala-miniboxing.org @miniboxing
    Reverse Dependencies
    Reverse Dependencies
    $ scala
    Welcome to Scala version 2.11.7 (Java HotSpot(TM)
    64-Bit Server VM, Java 1.8.0_101).
    Type in expressions to have them evaluated.
    Type :help for more information.
    scala> Test.identityFun(2)
    :11: error: This method was compiled with the
    miniboxing plugin and can only be called by code also
    compiled with the miniboxing plugin (see
    scala-miniboxing.org/compatibility)
    [mbox=0.4-M8,scala=2.11.7]
    Test.identityFun(2)
    ^

    View full-size slide

  114. scala-miniboxing.org @miniboxing
    Reverse Dependencies
    Reverse Dependencies
    def identityFun[@miniboxed T] = (t: T) => t
    def identityFun[@miniboxed T]: Function1[T, T] =
    (t: T) => t
    typer
    interop-inject
    @compileTimeOnly(...)
    def identityFun[@miniboxed T]: Function1[T, T] @mbFun =
    (t: T) => t
    pickler

    View full-size slide

  115. scala-miniboxing.org @miniboxing
    Reverse Dependencies
    Reverse Dependencies
    def identityFun[@miniboxed T] = (t: T) => t
    def identityFun[@miniboxed T]: Function1[T, T] =
    (t: T) => t
    typer
    interop-inject
    @compileTimeOnly(...)
    def identityFun[@miniboxed T]: Function1[T, T] @mbFun =
    (t: T) => t
    Built into the Scala stdlib
    and recognized by scalac
    (checked by refchecks)
    pickler

    View full-size slide

  116. scala-miniboxing.org @miniboxing
    Reverse Dependencies
    Reverse Dependencies
    miniboxing
    introduces
    annotation
    here

    View full-size slide

  117. scala-miniboxing.org @miniboxing
    Reverse Dependencies
    Reverse Dependencies
    miniboxing
    introduces
    annotation
    here
    ... and
    removes it
    just before
    refchecks

    View full-size slide

  118. scala-miniboxing.org @miniboxing
    Functions
    Transformation
    Experiment
    Conclusion
    Miniboxing
    Dependencies
    Reverse deps
    Object model

    View full-size slide

  119. scala-miniboxing.org @miniboxing
    Functions
    Transformation
    Experiment
    Conclusion
    Miniboxing
    Dependencies
    Reverse deps
    Object model

    View full-size slide

  120. scala-miniboxing.org @miniboxing
    Object Model
    Object Model
    trait IdentityFun {
    def identityFun[T]: T => T // = Function1 [T, T]
    }

    View full-size slide

  121. scala-miniboxing.org @miniboxing
    Object Model
    Object Model
    trait IdentityFun {
    def identityFun[T]: T => T // = Function1 [T, T]
    }
    object MbIdentityFun extends IdentityFun {
    def identityFun[@miniboxed T]: T => T = (t: T) => t
    }

    View full-size slide

  122. scala-miniboxing.org @miniboxing
    Object Model
    Object Model
    trait IdentityFun {
    def identityFun[T]: T => T // = Function1 [T, T]
    }
    object MbIdentityFun extends IdentityFun {
    def identityFun[@miniboxed T]: T => T = (t: T) => t
    }
    scala> MbIdentityFun.identityFun(2)
    :11: error: ...
    scala> val id: IdentityFun = MbIdentityFun
    id: IdentityFun = MbIdentityFun$@23223dd8
    scala> id.identityFun(3)
    res1: Int = 3

    View full-size slide

  123. scala-miniboxing.org @miniboxing
    Object Model
    Object Model
    trait IdentityFun {
    def identityFun[T]: T => T // = Function1 [T, T]
    }
    object MbIdentityFun extends IdentityFun {
    def identityFun[@miniboxed T]: T => T = (t: T) => t
    }
    scala> MbIdentityFun.identityFun(2)
    :11: error: ...
    scala> val id: IdentityFun = MbIdentityFun
    id: IdentityFun = MbIdentityFun$@23223dd8
    scala> id.identityFun(3)
    res1: Int = 3
    You can "hide" miniboxed
    code behind an interface

    View full-size slide

  124. scala-miniboxing.org @miniboxing
    Object Model
    Object Model

    Allows "hiding" miniboxed code
    – Under an erased interface (example)
    – Under a specialized interface

    Pro: no dependency on the plugin downstream

    Cons: have to write some glue code

    View full-size slide

  125. scala-miniboxing.org @miniboxing
    Functions
    Transformation
    Experiment
    Conclusion
    Miniboxing
    Dependencies
    Reverse deps
    Object model

    View full-size slide

  126. scala-miniboxing.org @miniboxing
    Functions
    Transformation
    Experiment
    Conclusion
    Miniboxing

    View full-size slide

  127. scala-miniboxing.org @miniboxing
    RRBVector Experiment
    RRBVector Experiment

    RRBVector is an immutable vector data structure
    – With constant-time split and merge (for parallel ops)

    Idea by Phil Bagwell and Tiark Rompf

    Implementation by Nicolas Stucki
    – 4 developer-weeks of work
    – Hand-tuned for maximum performance
    – Scaling almost linearly with the number of cores
    – Summing a vector is 2x as slow as summing an array

    Presented at ICFP '15

    View full-size slide

  128. scala-miniboxing.org @miniboxing
    RRBVector Experiment
    RRBVector Experiment

    Questions:
    – How difficult is it to specialize RRBVector with miniboxing?
    – What are the speedups obtained by miniboxing?

    Preparation:
    – Copied the Scala stdlib suppor necessary for RRBVector (3k LOC)
    – Eliminated the parallel execution support (3k 2k LOC)

    – Prepared benchmarks

    Subject: Milos Stojanovic
    – Master student working in the lab
    – Experienced with miniboxing
    – Never saw the RRBVector code base before

    View full-size slide

  129. scala-miniboxing.org @miniboxing
    RRBVector Experiment
    RRBVector Experiment

    It took 30 minutes to optimize the code base
    by following the miniboxing warnings → 0.3%
    of the development time
    Benchmark
    Microbenchmarks (5M elements) Macrobenchmark
    Builder map fold reverse*
    Least squares linear
    regression (5M points)
    Erased
    (seconds)
    43.2 103.0 94.1 31.4 4864.6
    Miniboxed
    (seconds)
    22.9 60.4 42.1 35.3 1818.1
    Speedup
    (ratio)
    1.88x 1.71x 2.24x 0.89x 2.68x
    * reversing a vector does not perform
    any operations that could benefit from
    miniboxing.

    View full-size slide

  130. scala-miniboxing.org @miniboxing
    RRBVector Experiment
    RRBVector Experiment

    Used library features
    – MbFunction
    – MbArray
    – Tuples
    – Numeric
    – Ordering

    View full-size slide

  131. scala-miniboxing.org @miniboxing
    RRBVector Experiment
    RRBVector Experiment

    Used library features
    – MbFunction
    – MbArray
    – Tuples
    – Numeric
    – Ordering
    Impossible to cover today

    View full-size slide

  132. scala-miniboxing.org @miniboxing

    View full-size slide

  133. scala-miniboxing.org @miniboxing
    Functions
    Transformation
    Experiment
    Conclusion
    Miniboxing

    View full-size slide

  134. scala-miniboxing.org @miniboxing
    Functions
    Transformation
    Experiment
    Conclusion
    Miniboxing

    View full-size slide

  135. scala-miniboxing.org @miniboxing
    Miniboxed Library
    Miniboxed Library

    Replaces deeply integrated Scala constructs
    – Preserve the syntactic sugar
    – No source code changes

    Scalac/miniboxing does a lot of the work

    Use cases
    – Miniboxing: performance

    View full-size slide

  136. scala-miniboxing.org @miniboxing
    scala-miniboxing.org
    Special thanks:
    Alexandru Nedelcu
    Milos Stojanovic
    Romain Beguet
    Nicolas Stucki
    Scala team @ EPFL

    View full-size slide