diff options
author | Vsevolod Tolstopyatov <qwwdfsad@gmail.com> | 2020-07-21 13:18:09 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-21 23:18:09 +0300 |
commit | c136b5697853622914e60bdf941802a662d97e85 (patch) | |
tree | b87eddc079076b4439e97f8dd721c544d8a2b1bd /benchmark | |
parent | 8654620b8e4b5e2b638deed2943d1206084e885c (diff) | |
download | kotlinx.serialization-c136b5697853622914e60bdf941802a662d97e85.tar.gz |
ProtoBuf enchancements (#923)
* Do not throw exceptions that are not subtypes of SerializationException on unexpected inputs
* Properly encode length for tagless top-level ProtoBuf data
* Get rid of dead code
* Significantly optimize ProtoBuf nested structures by avoiding an additional copy of nested objects during encoding and decoding
Fixes #870
Fixes #93
Diffstat (limited to 'benchmark')
3 files changed, 77 insertions, 7 deletions
diff --git a/benchmark/src/jmh/kotlin/kotlinx/benchmarks/ProtoBaseline.kt b/benchmark/src/jmh/kotlin/kotlinx/benchmarks/ProtoBaseline.kt index 729b6205..b6f414c4 100644 --- a/benchmark/src/jmh/kotlin/kotlinx/benchmarks/ProtoBaseline.kt +++ b/benchmark/src/jmh/kotlin/kotlinx/benchmarks/ProtoBaseline.kt @@ -4,7 +4,7 @@ package kotlinx.benchmarks -import kotlinx.serialization.Serializable +import kotlinx.serialization.* import kotlinx.serialization.protobuf.* import org.openjdk.jmh.annotations.* import java.util.concurrent.* @@ -24,20 +24,20 @@ open class ProtoBaseline { class HolderExplicit(@ProtoId(1) val a: Int, @ProtoId(2) val b: Int, @ProtoId(3) val c: Long, @ProtoId(4) val d: Double) private val holder = Holder(1, 2, 3L, 4.0) - private val holderBytes = ProtoBuf.dump(Holder.serializer(), holder) + private val holderBytes = ProtoBuf.encodeToByteArray(Holder.serializer(), holder) private val holderExplicit = HolderExplicit(1, 2, 3L, 4.0) - private val holderHolderExplicitBytes = ProtoBuf.dump(HolderExplicit.serializer(), holderExplicit) + private val holderHolderExplicitBytes = ProtoBuf.encodeToByteArray(HolderExplicit.serializer(), holderExplicit) @Benchmark - fun toBytes() = ProtoBuf.dump(Holder.serializer(), holder) + fun toBytes() = ProtoBuf.encodeToByteArray(Holder.serializer(), holder) @Benchmark - fun fromBytes() = ProtoBuf.load(Holder.serializer(), holderBytes) + fun fromBytes() = ProtoBuf.decodeFromByteArray(Holder.serializer(), holderBytes) @Benchmark - fun toBytesExplicit() = ProtoBuf.dump(HolderExplicit.serializer(), holderExplicit) + fun toBytesExplicit() = ProtoBuf.encodeToByteArray(HolderExplicit.serializer(), holderExplicit) @Benchmark - fun fromBytesExplicit() = ProtoBuf.load(HolderExplicit.serializer(), holderHolderExplicitBytes) + fun fromBytesExplicit() = ProtoBuf.decodeFromByteArray(HolderExplicit.serializer(), holderHolderExplicitBytes) } diff --git a/benchmark/src/jmh/kotlin/kotlinx/benchmarks/ProtoListBenchmark.kt b/benchmark/src/jmh/kotlin/kotlinx/benchmarks/ProtoListBenchmark.kt new file mode 100644 index 00000000..35d2dbab --- /dev/null +++ b/benchmark/src/jmh/kotlin/kotlinx/benchmarks/ProtoListBenchmark.kt @@ -0,0 +1,35 @@ +/* + * Copyright 2017-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.benchmarks + +import kotlinx.serialization.* +import kotlinx.serialization.protobuf.* +import org.openjdk.jmh.annotations.* +import java.util.concurrent.* + +@Warmup(iterations = 5, time = 1) +@Measurement(iterations = 5, time = 1) +@BenchmarkMode(Mode.Throughput) +@OutputTimeUnit(TimeUnit.MICROSECONDS) +@State(Scope.Benchmark) +@Fork(1) +open class ProtoListBenchmark { + + @Serializable + class Holder(val a: Int, val b: Int, val c: Long, val d: Double) + + @Serializable + class HolderList(val list: List<Holder>) + + private val h = Holder(1, 2, 3L, 4.0) + private val value = HolderList(listOf(h, h, h, h, h)) + private val bytes = ProtoBuf.encodeToByteArray(value) + + @Benchmark + fun toBytes() = ProtoBuf.encodeToByteArray(HolderList.serializer(), value) + + @Benchmark + fun fromBytes() = ProtoBuf.decodeFromByteArray(HolderList.serializer(), bytes) +} diff --git a/benchmark/src/jmh/kotlin/kotlinx/benchmarks/ProtoListLikeBenchmark.kt b/benchmark/src/jmh/kotlin/kotlinx/benchmarks/ProtoListLikeBenchmark.kt new file mode 100644 index 00000000..d6c5896c --- /dev/null +++ b/benchmark/src/jmh/kotlin/kotlinx/benchmarks/ProtoListLikeBenchmark.kt @@ -0,0 +1,35 @@ +/* + * Copyright 2017-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.benchmarks + +import kotlinx.serialization.* +import kotlinx.serialization.protobuf.* +import org.openjdk.jmh.annotations.* +import java.util.concurrent.* + +@Warmup(iterations = 5, time = 1) +@Measurement(iterations = 5, time = 1) +@BenchmarkMode(Mode.Throughput) +@OutputTimeUnit(TimeUnit.MICROSECONDS) +@State(Scope.Benchmark) +@Fork(1) +open class ProtoListLikeBenchmark { + + @Serializable + class Holder(val a: Int, val b: Int, val c: Long, val d: Double) + + @Serializable + class HolderList(val h1: Holder, val h2: Holder, val h3: Holder, val h4: Holder, val h5: Holder) + + private val h = Holder(1, 2, 3L, 4.0) + private val value = HolderList(h, h, h, h, h) + private val bytes = ProtoBuf.encodeToByteArray(value) + + @Benchmark + fun toBytes() = ProtoBuf.encodeToByteArray(HolderList.serializer(), value) + + @Benchmark + fun fromBytes() = ProtoBuf.decodeFromByteArray(HolderList.serializer(), bytes) +} |