This repository has been archived by the owner on Sep 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Add jackson-cbor, jackson-smile, jsoniter-scala, and kryo-macros #8
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f8cfa80
Add jsoniter-scala
plokhotnyuk 25da091
Add sbt build
plokhotnyuk 1388084
Update version of dependencies
plokhotnyuk 71484d1
Add kryo-macros
plokhotnyuk 16874bf
Add Jackson CBOR + update versions of Jackson and jsoniter-scala
plokhotnyuk adcf85e
Revert redundant changes
plokhotnyuk 8901d82
Update version of JMH
plokhotnyuk 41adcf4
Add Jackson Smile
plokhotnyuk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
lazy val commonSettings = Seq( | ||
organization := "com.komanov", | ||
organizationHomepage := Some(url("https://komanov.com")), | ||
homepage := Some(url("https://dkomanov.github.io/scala-serialization/")), | ||
licenses := Seq(("MIT License", url("https://opensource.org/licenses/mit-license.html"))), | ||
startYear := Some(2016), | ||
scalaVersion := "2.11.12", | ||
resolvers += Resolver.bintrayRepo("evolutiongaming", "maven"), | ||
scalacOptions ++= Seq( | ||
"-deprecation", | ||
"-feature", | ||
"-Xmax-classfile-name", "240", | ||
//"-Xmacro-settings:print-serializers" //to log sources of serializaer which are generated by kryo-macros | ||
//"-Xmacro-settings:print-codecs" //to log sources of codecs which are generated by jsoniter-scala | ||
) | ||
) | ||
|
||
lazy val `scala-serialization-all` = project.in(file(".")) | ||
.aggregate(`scala-serialization`, `scala-serialization-test`) | ||
|
||
lazy val `scala-serialization` = project | ||
.settings(commonSettings: _*) | ||
.settings( | ||
fork in Test := true, | ||
libraryDependencies ++= Seq( | ||
"com.twitter" %% "util-core" % "18.3.0", | ||
"commons-io" % "commons-io" % "2.6", | ||
"com.evolutiongaming" %% "kryo-macros" % "1.1.8", | ||
"com.github.plokhotnyuk.jsoniter-scala" %% "macros" % "0.22.2", | ||
"com.fasterxml.jackson.core" % "jackson-databind" % "2.9.5", | ||
"com.fasterxml.jackson.core" % "jackson-core" % "2.9.5", | ||
"com.fasterxml.jackson.dataformat" % "jackson-dataformat-cbor" % "2.9.5", | ||
"com.fasterxml.jackson.dataformat" % "jackson-dataformat-smile" % "2.9.5", | ||
"com.fasterxml.jackson.module" %% "jackson-module-scala" % "2.9.5", | ||
"com.google.protobuf" % "protobuf-java" % "3.0.0", | ||
"com.trueaccord.scalapb" %% "scalapb-runtime" % "0.5.45", | ||
"org.scala-lang.modules" %% "scala-pickling" % "0.11.0-M2", | ||
"io.suzaku" %% "boopickle" % "1.2.6", | ||
"com.twitter" %% "chill" % "0.9.2", | ||
"com.esotericsoftware" % "kryo-shaded" % "4.0.1", //for chill (should match with version for kryo-macros dependencies) | ||
"org.apache.thrift" % "libthrift" % "0.11.0", | ||
"org.slf4j" % "slf4j-simple" % "1.7.21", //for thrift | ||
"com.twitter" %% "scrooge-core" % "18.3.0", | ||
"org.specs2" %% "specs2-core" % "3.8.3" % Test, | ||
"org.specs2" %% "specs2-matcher-extra" % "3.8.3" % Test, | ||
"org.specs2" %% "specs2-mock" % "3.8.3" % Test, | ||
"org.specs2" %% "specs2-junit" % "3.8.3" % Test | ||
) | ||
) | ||
|
||
lazy val `scala-serialization-test` = project | ||
.dependsOn(`scala-serialization`) | ||
.enablePlugins(JmhPlugin) | ||
.settings(commonSettings: _*) | ||
.settings( | ||
libraryDependencies ++= Seq( | ||
"pl.project13.scala" % "sbt-jmh-extras" % "0.3.3" | ||
) | ||
) |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
sbt.version=1.1.1 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
addSbtPlugin("pl.project13.scala" % "sbt-jmh" % "0.3.3") | ||
addSbtPlugin("net.virtual-void" % "sbt-dependency-graph" % "0.9.0") |
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
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
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
30 changes: 30 additions & 0 deletions
30
...lization/src/main/scala/com/komanov/serialization/converters/JsoniterScalaConverter.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.komanov.serialization.converters | ||
|
||
import java.time.Instant | ||
|
||
import com.github.plokhotnyuk.jsoniter_scala.core._ | ||
import com.github.plokhotnyuk.jsoniter_scala.macros._ | ||
import com.komanov.serialization.domain.{Site, SiteEvent} | ||
|
||
/** https://github.com/plokhotnyuk/jsoniter-scala */ | ||
object JsoniterScalaConverter extends MyConverter { | ||
private[this] val writerConfig = WriterConfig(preferredBufSize = 131072) | ||
private[this] val readerConfig = ReaderConfig(preferredBufSize = 131072, preferredCharBufSize = 131072) | ||
private[this] implicit val instantCodec: JsonValueCodec[Instant] = new JsonValueCodec[Instant] { | ||
override def nullValue: Instant = null | ||
|
||
override def decodeValue(in: JsonReader, default: Instant): Instant = Instant.ofEpochMilli(in.readLong()) | ||
|
||
override def encodeValue(x: Instant, out: JsonWriter): Unit = out.writeVal(x.toEpochMilli) | ||
} | ||
private[this] implicit val siteCodec: JsonValueCodec[Site] = JsonCodecMaker.make[Site](CodecMakerConfig()) | ||
private[this] implicit val siteEventCodec: JsonValueCodec[SiteEvent] = JsonCodecMaker.make[SiteEvent](CodecMakerConfig()) | ||
|
||
def toByteArray(site: Site): Array[Byte] = writeToArray(site, writerConfig) | ||
|
||
def fromByteArray(bytes: Array[Byte]): Site = readFromArray[Site](bytes, readerConfig) | ||
|
||
def toByteArray(event: SiteEvent): Array[Byte] = writeToArray(event, writerConfig) | ||
|
||
def siteEventFromByteArray(clazz: Class[_], bytes: Array[Byte]): SiteEvent = readFromArray[SiteEvent](bytes, readerConfig) | ||
} |
81 changes: 81 additions & 0 deletions
81
...rialization/src/main/scala/com/komanov/serialization/converters/KryoMacrosConverter.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package com.komanov.serialization.converters | ||
|
||
import com.esotericsoftware.kryo.Kryo | ||
import com.esotericsoftware.kryo.io.{FastInput, FastOutput} | ||
import com.evolutiongaming.kryo.Serializer | ||
import com.komanov.serialization.domain._ | ||
|
||
/** https://github.com/evolution-gaming/kryo-macros */ | ||
case class KryoMacrosConverter(kryo: Kryo, in: FastInput, out: FastOutput) { | ||
def read[A <: AnyRef](bs: Array[Byte])(implicit s: com.esotericsoftware.kryo.Serializer[A], m: Manifest[A]): A = { | ||
in.setBuffer(bs) | ||
kryo.readObject(in, m.runtimeClass.asInstanceOf[Class[A]], s) | ||
} | ||
|
||
def write[A <: AnyRef](a: A)(implicit s: com.esotericsoftware.kryo.Serializer[A]): Array[Byte] = { | ||
out.clear() | ||
kryo.writeObject(out, a, s) | ||
out.toBytes | ||
} | ||
} | ||
|
||
object KryoMacrosConverter extends MyConverter { | ||
private[this] val pool = new ThreadLocal[KryoMacrosConverter] { | ||
override def initialValue(): KryoMacrosConverter = | ||
KryoMacrosConverter(new Kryo, new FastInput(), new FastOutput(131072, 131072)) | ||
} | ||
private[this] implicit val domainSerializer = Serializer.make[Domain] | ||
private[this] implicit val metaTagSerializer = Serializer.make[MetaTag] | ||
private[this] implicit val entryPointSerializer = Serializer.makeCommon[EntryPoint] { | ||
case 0 => Serializer.inner[DomainEntryPoint] | ||
case 1 => Serializer.inner[FreeEntryPoint] | ||
} | ||
private[this] implicit val pageComponentDataSerializer = Serializer.makeCommon[PageComponentData] { | ||
case 0 => Serializer.inner[TextComponentData] | ||
case 1 => Serializer.inner[ButtonComponentData] | ||
case 2 => Serializer.inner[BlogComponentData] | ||
} | ||
private[this] implicit val pageComponentPositionSerializer = Serializer.make[PageComponentPosition] | ||
private[this] implicit val pageComponentSerializer = Serializer.make[PageComponent] | ||
private[this] implicit val pageSerializer = Serializer.make[Page] | ||
private[this] implicit val siteSerializer = Serializer.make[Site] | ||
private[this] implicit val siteEventSerializer = Serializer.makeCommon[SiteEvent] { | ||
case 0 => Serializer.inner[SiteCreated] | ||
case 1 => Serializer.inner[SiteNameSet] | ||
case 2 => Serializer.inner[SiteDescriptionSet] | ||
case 3 => Serializer.inner[SiteRevisionSet] | ||
case 4 => Serializer.inner[SitePublished] | ||
case 5 => Serializer.inner[SiteUnpublished] | ||
case 6 => Serializer.inner[SiteFlagAdded] | ||
case 7 => Serializer.inner[SiteFlagRemoved] | ||
case 8 => Serializer.inner[DomainAdded] | ||
case 9 => Serializer.inner[DomainRemoved] | ||
case 10 => Serializer.inner[PrimaryDomainSet] | ||
case 11 => Serializer.inner[DefaultMetaTagAdded] | ||
case 12 => Serializer.inner[DefaultMetaTagRemoved] | ||
case 13 => Serializer.inner[PageAdded] | ||
case 14 => Serializer.inner[PageRemoved] | ||
case 15 => Serializer.inner[PageNameSet] | ||
case 16 => Serializer.inner[PageMetaTagAdded] | ||
case 17 => Serializer.inner[PageMetaTagRemoved] | ||
case 18 => Serializer.inner[PageComponentAdded] | ||
case 19 => Serializer.inner[PageComponentRemoved] | ||
case 20 => Serializer.inner[PageComponentPositionSet] | ||
case 21 => Serializer.inner[PageComponentPositionReset] | ||
case 22 => Serializer.inner[TextComponentDataSet] | ||
case 23 => Serializer.inner[ButtonComponentDataSet] | ||
case 24 => Serializer.inner[BlogComponentDataSet] | ||
case 25 => Serializer.inner[DomainEntryPointAdded] | ||
case 26 => Serializer.inner[FreeEntryPointAdded] | ||
case 27 => Serializer.inner[EntryPointRemoved] | ||
case 28 => Serializer.inner[PrimaryEntryPointSet] | ||
} | ||
|
||
def toByteArray(site: Site): Array[Byte] = pool.get().write(site) | ||
|
||
def fromByteArray(bytes: Array[Byte]): Site = pool.get().read[Site](bytes) | ||
|
||
def toByteArray(event: SiteEvent): Array[Byte] = pool.get().write(event) | ||
|
||
def siteEventFromByteArray(clazz: Class[_], bytes: Array[Byte]): SiteEvent = pool.get().read[SiteEvent](bytes) | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Such size of a buffer seems like a hack. Other libraries do reallocations during serialization/deserialization.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is not a hack, it is using a provided feature for a competitive advantage)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@plokhotnyuk that's benchmarks-aware ad hocs
@dkomanov maybe you need to add 1Mb records ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using of preallocated thread-local buffers with the capacity that is enough for expected workload is a normal practice for any parformance-aware parser/serializer.
BTW, jsoniter-scala is able to serialize huge messages efficiently using ~32K buffers in the
writeToStream
routine:https://github.com/plokhotnyuk/jsoniter-scala/blob/master/jsoniter-scala-core/src/main/scala/com/github/plokhotnyuk/jsoniter_scala/core/package.scala#L190