Val Mypairpickler = Tuple2pickler(Intpickler, Stringpickler)

Val Mypairpickler = Tuple2pickler(Intpickler, Stringpickler)

On pICKLES sPORES IMPROVING SCALA’S SUPPORT FOR &DISTRIBUTED PROGRAMMING HEATHER MILLER with: PHILIPP HALLER TYPESAFE EUGENE BURMAKO EPFL MARTIN ODERSKY EPFL/TYPESAFE wHAT IS THIS TALK ABOUT? Making distributed programming easier in scala This kind of distributed system ALso! This kind of distributed system insert social network of your choice here Bottomline: Machines Communicating How can we simplify distribution at the language-level? Agenda sCALA pICKLING Spores scala.pickling Pickles! https://github.com/scala/pickling What is it? PICKLING == SERIALIZATION == MARSHALLING very different from java serialization https://github.com/scala/pickling wait, why do we care? not serializable Slow!exceptions at runtime can’tClosed! retroactively make classes serializable https://github.com/scala/pickling Enter: Scala Pickling fast: Serialization code generated at compile- time and inlined at the use-site. Flexible: Using typeclass pattern, retroactively make types serializable NO BOILERPLATE: Typeclass instances generated at compile-time pluggable formats: Effortlessly change format of serialized data: binary, JSON, invent your own! typesafe: Picklers are type-specialized. Catch errors at compile-time! https://github.com/scala/pickling What does it look like? scala> import scala.pickling._ import scala.pickling._ ! scala> import json._ import json._ ! scala> case class Person(name: String, age: Int) defined class Person ! scala> Person("John Oliver", 36) res0: Person = Person(John Oliver,36) scala> res0.pickle res1: scala.pickling.json.JSONPickle = JSONPickle({ "tpe": "Person", "name": "John Oliver", "age": 36 }) https://github.com/scala/pickling and... it’s pretty fast https://github.com/scala/pickling Benchmarks collections: Time collections: free MemoryBenchmarks (more is better) collections: size Benchmarks geotrellis: time Benchmarks evactor: time Benchmarks Java runs out of memory Benchmarks evactor: time (no java, more events) btw,that’s just the default behavior... you can really customize scala pickling too. https://github.com/scala/pickling Customizing Pickling Previous examples used default behavior Generated picklers Standard pickle format Pickling is very customizable Custom picklers for specific types Custom pickle format Before we can show these things,let's have a look at the building block of the framework... Wait,What about subclasses? abstract class Person { def name: String } Scala is object-oriented too, case class Firefighter(name: String, since: Int) extends Person case class Doctor(name: String, since: Int) extends Person ! remember? class Position(title: String, person: Person) To generate a pickler for Position, combine picklers for String and Person. but person could dynamically be a firefighter or doctor! https://github.com/scala/pickling Subtyping Goal: modular composition of picklers ! Generated picklers Standard pickle format Pickling is very customizable Custom picklers for specific types Custom pickle format A `Pickler[Person]` is not good enough, it can only pickle instances whose dynamic type is `Person`. ! Pickler Combinators Elegant programming pearl that comes from functional programming. A composable and "constructive" way to think about persisting data. Compose picklers for simple types to build picklers for more complicated types simplified version of what’s What is a pickler? actually used in scala- pickling trait Pickler[T] { // returns next write position def pickle(arr: Array[Byte], i: Int, x: T): Int // returns result plus next read position def unpickle(arr: Array[Byte], i: Int): (T, Int) } https://github.com/scala/pickling Pickler Combinators We need 2 things: fully-implemented picklers for some basic types like 1 Picklers for base types primitives Functions that combine existing picklers 2 to build compound picklers example: combinator that takes a Pickler[T] and returns a Pickler[List[T]] https://github.com/scala/pickling Pickler Combinators Build a pickler for pairs (Int, String), Goal: combine an Int pickler and a String pickler val myPairPickler = tuple2Pickler(intPickler, stringPickler) What’s the type? Pickler[T], can pickle objects of type T Can we combine them automatically? Can take intPickler and stringPickler as implicit parameters tuple2Pickler can be an implicit def def pickle(implicit pickler: Pickler[(Int, String)]) = { pickler.pickle((32, “yay!”)) } https://github.com/scala/pickling Implicit Picklers customize what you pickle! case class Person(name: String, age: Int, salary: Int) ! class CustomPersonPickler(implicit val format: PickleFormat) extends SPickler[Person] { def pickle(picklee: Person, builder: PBuilder): Unit = { builder.beginEntry(picklee) builder.putField("name", b => b.hintTag(FastTypeTag.ScalaString).beginEntry(picklee.name).endEntry()) builder.putField("age", b => b.hintTag(FastTypeTag.Int).beginEntry(picklee.age).endEntry()) builder.endEntry() } } ! implicit def genCustomPersonPickler(implicit format: PickleFormat) = new CustomPersonPickler https://github.com/scala/pickling Pickle Format output any format! trait PickleFormat { type PickleType <: Pickle def createBuilder(): PBuilder def createReader(pickle: PickleType, mirror: Mirror): PReader } ! trait PBuilder extends Hintable { def beginEntry(picklee: Any): this.type def putField(name: String, pickler: this.type => Unit): this.type def endEntry(): Unit def beginCollection(length: Int): this.type def putElement(pickler: this.type => Unit): this.type def endCollection(length: Int): Unit def result(): Pickle } https://github.com/scala/pickling Pickle Formatexample talk to a clojure app Output edn, Clojure’s data transfer format. scala> import scala.pickling._ import scala.pickling._ ! scala> import edn._ import edn._ ! scala> case class Person(name: String, kidsAges: Array[Int]) defined class Person ! scala> val joe = Person("Joe", Array(3, 4, 13)) joe: Person = Person(Joe,[I@3d925789) ! scala> joe.pickle.value res0: String = #pickling/Person { :name "Joe" :kidsAges [3, 4, 13] } toy builder implementation: https://gist.github.com/heathermiller/5760171 What can be pickled? types for which an implicit pickler is in scope if there is an implicit of type Pickler[Foo] in scope, you can pickle instances of it types for which our framework can generate picklers classes case classes IMPORTANT: The implicit picklers generic classes are used in the generation! singleton objects primitives & primitive arrays ... can’t (yet): instances of inner classes, Types https://github.com/scala/pickling Status Release 0.8.0 for Scala 2.10.2 https://github.com/scala/pickling ScalaCheck tests Very soon: support for cyclic object graphs (for release 0.9.0) No support for inner classes, yet Goal: Scala 2.11 as target Plan: 1.0 release within the next few months Integration with sbt, Spark, and Akka, ... SIP for Scala 2.11 Experiment: use Scala-pickling to speed up Scala compiler And now onto something completely different. scala spores! http://docs.scala-lang.org/sips/pending/spores.html Closures are great. but we can’t really distributethem. But, we can’t really distribute them WHY? oFTEN NOT SERIALIZABLE because they capture stuff that’s not serializable. ...enclosing ACCIDENTAL CAPTURE. Easy to reference something and unknowingly capture it. runtime errors instead of compile-time checks. Who ’ s fault is it? for a user, often unclear whether it’s a user-error or the framework But, we can’t really distribute them Consequences that follow from these problems... framework! builders avoid them ...not just in their public APIs, but private ones too. Users shoot themselves in the foot and blame framework. When picking battles, framework designers tend to avoid issues with closures. rightfully so. eNTER: spores wHAT ARE THEY? small units of possibly mobile functional behavior Proposed for inclusion in Scala 2.11 http://docs.scala-lang.org/sips/pending/spores.html Spores Potential hazards when using closures incorrectly:what are they? • Amemory closure-like leaks abstraction for use in • distributedrace conditions or due concurrent to capturing environments. mutable references • runtime serialization errors due to unintended capture of references goal: Well-behaved closures with controlled environments that can avoid various hazards. Proposed for inclusion in Scala 2.11 http://docs.scala-lang.org/sips/pending/spores.html motivating eample: spark class MyCoolRddApp { Problem: val param = 3.14 (x => x + param) val log = new Log(...) not serializable ... def work(rdd: RDD[Int]) { because it captures rdd.map(x => x + param) this of type .reduce(...) MyCoolRddApp } which is itself not } serializable Proposed for inclusion in Scala 2.11 http://docs.scala-lang.org/sips/pending/spores.html motivating eample: Akka/futures akka actor spawns a future to concurrently def receive = { process incoming reqs case Request(data) => future { val result = transform(data) sender ! Response(result) not a stable value! } it’s a method call! } Problem: Akka actor spawns future to concurrently process incoming results Proposed for inclusion in Scala 2.11 http://docs.scala-lang.org/sips/pending/spores.html motivating eample: Serialization case class Helper(name: String) Problem: ! class Main { fun not serializable. val helper = Helper("the helper") Accidentally captures ! this since val fun: Int => Unit = (x: Int) => { helper.toString val result = x + " " + helper.toString is really println("The result is: " + result) this.helper.toString, } and Main (the type of } this) is not serializable. Proposed for inclusion in Scala 2.11 http://docs.scala-lang.org/sips/pending/spores.html

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    53 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us