Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Prototype] do not use tree-based decoding for fast-path polymorphism
Fixes #1839
- Loading branch information
Showing
8 changed files
with
141 additions
and
20 deletions.
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
53 changes: 53 additions & 0 deletions
53
benchmark/src/jmh/kotlin/kotlinx/benchmarks/json/PolymorphismOverheadBenchmark.kt
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,53 @@ | ||
package kotlinx.benchmarks.json | ||
|
||
import kotlinx.serialization.* | ||
import kotlinx.serialization.json.* | ||
import kotlinx.serialization.modules.* | ||
import org.openjdk.jmh.annotations.* | ||
import java.util.concurrent.* | ||
|
||
@Warmup(iterations = 7, time = 1) | ||
@Measurement(iterations = 5, time = 1) | ||
@BenchmarkMode(Mode.Throughput) | ||
@OutputTimeUnit(TimeUnit.MILLISECONDS) | ||
@State(Scope.Benchmark) | ||
@Fork(1) | ||
open class PolymorphismOverheadBenchmark { | ||
|
||
@Serializable | ||
@JsonClassDiscriminator("poly") | ||
data class PolymorphicWrapper(val i: @Polymorphic Poly, val i2: Impl) // amortize the cost a bit | ||
|
||
@Serializable | ||
data class BaseWrapper(val i: Impl, val i2: Impl) | ||
|
||
@JsonClassDiscriminator("poly") | ||
interface Poly | ||
|
||
@Serializable | ||
@JsonClassDiscriminator("poly") | ||
class Impl(val a: Int, val b: String) : Poly | ||
|
||
private val impl = Impl(239, "average_size_string") | ||
private val module = SerializersModule { | ||
polymorphic(Poly::class) { | ||
subclass(Impl.serializer()) | ||
} | ||
} | ||
|
||
private val json = Json { serializersModule = module } | ||
private val implString = json.encodeToString(impl) | ||
private val polyString = json.encodeToString<Poly>(impl) | ||
private val serializer = serializer<Poly>() | ||
|
||
// 5000 | ||
@Benchmark | ||
fun base() = json.decodeFromString(Impl.serializer(), implString) | ||
|
||
// Baseline -- 1500 | ||
// v1, no skip -- 2000 | ||
// v2, with skip -- 3000 | ||
@Benchmark | ||
fun poly() = json.decodeFromString(serializer, polyString) | ||
|
||
} |
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
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