Skip to content

Instantly share code, notes, and snippets.

@douglaz
Last active December 5, 2016 17:14
Show Gist options
  • Save douglaz/c8a1c4ee2cb016db32a00077a85c27bc to your computer and use it in GitHub Desktop.
Save douglaz/c8a1c4ee2cb016db32a00077a85c27bc to your computer and use it in GitHub Desktop.
Scala Serialization: Set x List x Vector x Seq x Array using Kryo and Java
import scala.collection.mutable
object C {
case class CSet(s: Set[String])
case class CLis(s: List[String])
case class CVec(s: Vector[String])
case class CSeq(s: Seq[String])
case class CArr(s: Array[String])
}
object SerializationDemo extends App {
import C._
def measureBytes(obj: AnyRef) = {
def usingJava = {
import java.io._
val bout = new ByteArrayOutputStream()
val out = new ObjectOutputStream(bout)
out.writeObject(obj)
out.close()
bout.size()
}
def usingKryo() = {
import com.twitter.chill.{KryoPool, ScalaKryoInstantiator}
val instantiator = new ScalaKryoInstantiator
KryoPool.withByteArrayOutputStream(1, instantiator).toBytesWithClass(obj).size
}
Map('kryo -> usingKryo(), 'java -> usingJava)
}
val s = (1 to 20).map(_.toString)
println(s"Set: ${measureBytes(CSet(s.toSet))}")
println(s"List: ${measureBytes(CLis(s.toList))}")
println(s"Vector: ${measureBytes(CVec(s.toVector))}")
println(s"Seq as List: ${measureBytes(CSeq(s.toList))}")
println(s"Seq as ArraySeq: ${measureBytes(CSeq(mutable.ArraySeq.empty ++ s))}")
println(s"Seq as WrappedArray: ${measureBytes(CSeq(mutable.WrappedArray.empty ++ s))}")
println(s"Array: ${measureBytes(CArr(s.toArray))}")
println(s"Empty Set: ${measureBytes(CSet(Set.empty))}")
println(s"Empty List: ${measureBytes(CLis(List.empty))}")
println(s"Empty Vector: ${measureBytes(CVec(Vector.empty))}")
println(s"Empty Seq as List: ${measureBytes(CSeq(List.empty))}")
println(s"Empty Seq as ArraySeq: ${measureBytes(CSeq(mutable.ArraySeq.empty))}")
println(s"Empty Seq as WrappedArray: ${measureBytes(CSeq(mutable.WrappedArray.empty))}")
println(s"Empty Array: ${measureBytes(CArr(Array.empty))}")
}
@douglaz
Copy link
Author

douglaz commented Dec 2, 2016

Output:

Set: Map('kryo -> 98, 'java -> 240)
List: Map('kryo -> 138, 'java -> 293)
Vector: Map('kryo -> 97, 'java -> 451)
Seq as list: Map('kryo -> 138, 'java -> 282)
Array: Map('kryo -> 77, 'java -> 190)
Empty Set: Map('kryo -> 58, 'java -> 129)
Empty List: Map('kryo -> 50, 'java -> 202)
Empty Vector: Map('kryo -> 17, 'java -> 309)
Empty Seq as list: Map('kryo -> 50, 'java -> 191)
Empty Array: Map('kryo -> 17, 'java -> 99)

@douglaz
Copy link
Author

douglaz commented Dec 5, 2016

Set: Map('kryo -> 92, 'java -> 234)
List: Map('kryo -> 132, 'java -> 287)
Vector: Map('kryo -> 91, 'java -> 445)
Seq as List: Map('kryo -> 132, 'java -> 276)
Seq as ArraySeq: Map('kryo -> 149, 'java -> 280)
Seq as WrappedArray: Map('kryo -> 141, 'java -> 444)
Array: Map('kryo -> 71, 'java -> 184)
Empty Set: Map('kryo -> 52, 'java -> 123)
Empty List: Map('kryo -> 44, 'java -> 196)
Empty Vector: Map('kryo -> 11, 'java -> 303)
Empty Seq as List: Map('kryo -> 44, 'java -> 185)
Empty Seq as ArraySeq: Map('kryo -> 69, 'java -> 189)
Empty Seq as WrappedArray: Map('kryo -> 98, 'java -> 782)
Empty Array: Map('kryo -> 11, 'java -> 93)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment