Last active
December 5, 2016 17:14
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))}") | |
} |
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
Output: