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

Scala Miniboxing

Scala Miniboxing

0286822f506fc4621bd3ea0bcbfef238?s=128

Bucharest FP

July 17, 2014
Tweet

More Decks by Bucharest FP

Other Decks in Programming

Transcript

  1. scala-miniboxing.org Miniboxing Miniboxing the functional side of it :) BJUG,

    17th of July 2014 Vlad Ureche vlad.ureche@epfl.ch
  2. 2 scala-miniboxing.org Motivation Specialization Miniboxing Functions Evaluation WE ARE HERE

  3. 3 scala-miniboxing.org What's the type of x? def identity(x) =

    x
  4. 4 scala-miniboxing.org def identity(x: Int): Int = x Int is

    too restrictive identity(1) // okay identity(“three”) // error
  5. 5 scala-miniboxing.org def identity(x: Any): Any = x identity(1) //

    okay identity(“three”) // okay identity(1) + identity(3) // error No + operation on values of type Any Generics to our rescue!
  6. 6 scala-miniboxing.org def identity[T](x: T): T = x identity(1) //

    okay identity(“three”) // okay identity(1) + identity(3) // okay
  7. 7 scala-miniboxing.org def identity[T](x: T): T = x def identity(x:

    Any): Any = x Passed by reference (java.lang.Object) scalac / javac (erasure)
  8. 8 scala-miniboxing.org No-go for any performance-oriented application scala.Int int •

    fast access • no garbage collection • locality • indirect access • garbage collection • and object allocation • no locality guarantees • compatible with generics java.lang.Integer
  9. 9 scala-miniboxing.org Motivation Specialization Miniboxing Functions Evaluation WE ARE HERE

  10. 10 scala-miniboxing.org def identity[T](x: T): T = x def identity_V(x:

    Unit): Unit = x def identity_Z(x: Boolean): Boolean = x def identity_B(x: Byte): Byte = x def identity_C(x: Char): Char = x def identity_S(x: Short): Short = x def identity_I(x: Int): Int = x def identity_L(x: Long): Long = x def identity_F(x: Float): Float = x def identity_D(x: Double): Double = x For reference types (e.g. String) For primitive types
  11. 11 scala-miniboxing.org def identity[T](x: T): T = x def identity_V(x:

    Unit): Unit = x def identity_Z(x: Boolean): Boolean = x def identity_B(x: Byte): Byte = x def identity_C(x: Char): Char = x def identity_S(x: Short): Short = x def identity_I(x: Int): Int = x def identity_L(x: Long): Long = x def identity_F(x: Float): Float = x def identity_D(x: Double): Double = x identity(1) => identity_I(1) identity(“three”) => identity[String](“three”) identity(1) + identity(3) => identity_I(1) + identity_I(3) This is specialization (Iulian Dragos, 2009)
  12. 12 scala-miniboxing.org def tupled[T1, T2](t1: T1, t2: T2) = ...

    def tupled[T1, T2](t1: T1, t2: T2) = ... 10^2 methods
  13. 13 scala-miniboxing.org Motivation Specialization Miniboxing Functions Evaluation WE ARE HERE

  14. 14 scala-miniboxing.org def identity[T](x: T): T = x def identity_V(x:

    Unit): Unit = x def identity_Z(x: Boolean): Boolean = x def identity_B(x: Byte): Byte = x def identity_C(x: Char): Char = x def identity_S(x: Short): Short = x def identity_I(x: Int): Int = x def identity_L(x: Long): Long = x def identity_F(x: Float): Float = x def identity_D(x: Double): Double = x
  15. 15 scala-miniboxing.org But... But... we can do better we can

    do better One day in 2012 Miguel Garcia walked into my office and said: “From a low-level perspective, there are only values and pointers. Maybe you can use that!” ... LONG DOUBLE INT FLOAT SHORT a long integer
  16. 16 scala-miniboxing.org And then the And then the idea was

    born idea was born it started from the tagged union TAG DATA (VALUE) Stores the original type Stores the encoded value (as a long integer)
  17. 17 scala-miniboxing.org And then the And then the idea was

    born idea was born it started from the tagged union TAG DATA (VALUE) BOOL 0x1 true = INT 0x2A 42 = FLOAT bit representation 5.0f =
  18. 18 scala-miniboxing.org And then the And then the idea was

    born idea was born it started from the tagged union TAG DATA (VALUE) • somewhat similar to a boxed object • but not in the heap memory • direct access to the value Same benefits as for unboxed values
  19. 19 scala-miniboxing.org Let's take Let's take an example an example

    def choice[@miniboxed T](t1: T, t2: T): T = if (util.Random.nextBoolean()) t1 else t2 We'll have a version for primitive types
  20. 20 scala-miniboxing.org Let's take Let's take an example an example

    def choice_J[T](t1: ..., t2: ...): ... = if (util.Random.nextBoolean()) t1 else t2 But what's the signature?
  21. 21 scala-miniboxing.org Let's take Let's take an example an example

    def choice_J[T](t1: (Tag,Value), t2: (Tag,Value)):(Tag,Value)= if (util.Random.nextBoolean()) t1 else t2 That's wasteful: we carry the tag for T twice And we even return it, despite the caller having passed it This is naive tagged union Insight: we're in a statically typed language, use that!
  22. 22 scala-miniboxing.org Let's take Let's take an example an example

    def choice_J[T](T_Tag: Tag, t1: Value, t2: Value):Value= if (util.Random.nextBoolean()) t1 else t2 T_Tag corresponds to the type parameter Sort of a class tag Encoded as Long
  23. 23 scala-miniboxing.org Let's take Let's take an example an example

    def choice_J[T](T_Tag: Byte, t1: Long, t2: Long): Long= if (util.Random.nextBoolean()) t1 else t2
  24. 24 scala-miniboxing.org def identity[T](x: T): T = x def identity_V(x:

    Unit): Unit = x def identity_Z(x: Boolean): Boolean = x def identity_B(x: Byte): Byte = x def identity_C(x: Char): Char = x def identity_S(x: Short): Short = x def identity_I(x: Int): Int = x def identity_L(x: Long): Long = x def identity_F(x: Float): Float = x def identity_D(x: Double): Double = x
  25. 25 scala-miniboxing.org def identity[T](x: T): T = x def identity[T](T_Tag:

    Byte, x: Long): Long = x 2 methods
  26. 26 scala-miniboxing.org def tupled[T1, T2](t1: T1, t2: T2) = ...

    def tupled[T1, T2](t1: T1, t2: T2) = ... 2^2 methods
  27. 27 scala-miniboxing.org Motivation Specialization Miniboxing Functions Evaluation WE ARE HERE

  28. 28 scala-miniboxing.org Scala is object-oriented and functional … but first

    of all, object-oriented!
  29. 29 scala-miniboxing.org Functions Functions are also objects are also objects

    trait Function1[-T, +R] { def apply(t: T): R ... }
  30. 30 scala-miniboxing.org Functions Functions are also objects are also objects

    val f = (x: Int) => x + 1 val f = { class $anon extends Function1[Int, Int] { def apply(x: Int): Int = x + 1 } new $anon() }
  31. 31 scala-miniboxing.org Functions Functions are also objects are also objects

    f(4) f.apply(4)
  32. 32 scala-miniboxing.org with miniboxing... with miniboxing...

  33. 33 scala-miniboxing.org Function1[T, R] Function1_JL[T, R] Function1_LJ[T, R] Function1_LL[T, R]

    Function1_JJ[T, R] (x: Int) => x + 1 trait Function1[-T, +R] (x: Int) => x + 1
  34. 34 scala-miniboxing.org Functions Functions are also objects are also objects

    val f = (x: Int) => x + 1 val f = { class $anon extends Function1_JJ... { def apply_JJ(..., x: Long): Long = ... } new $anon() } Specialized Specialized
  35. 35 scala-miniboxing.org Functions Functions are also objects are also objects

    f(4) f.apply_JJ(INT, INT, int2minibox(4))
  36. 36 scala-miniboxing.org So, why all that trouble? For performance.

  37. 37 scala-miniboxing.org Motivation Specialization Miniboxing Functions Evaluation WE ARE HERE

  38. 38 scala-miniboxing.org Evaluation Evaluation on the Scala linked list on

    the Scala linked list • work with Aymeric Genet (github: @MelodyLucid) • mock-up of Scala linked list – Function1 / Function2 / Tuple2 – Traversable / TraversableLike – Iterator / Iterable / IterableLike – LinearSeqOptimized – Builder / CanBuildFrom
  39. 39 scala-miniboxing.org Benchmarks Benchmarks on the Scala library on the

    Scala library • benchmark: Least Squares Method
  40. 40 scala-miniboxing.org Benchmarks Benchmarks on the Scala library on the

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

    Scala library 30%-45% faster
  42. 42 scala-miniboxing.org Credits Credits • 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
  43. scala-miniboxing.org visit visit scala-miniboxing.org scala-miniboxing.org! ! functional or not, do

    try it!