Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do not use tree-based decoding for fast-path polymorphism (#1919)
Do not use tree-based decoding for fast-path polymorphism and try to optimistically read it as very first key and then silently skip Fixes #1839
- Loading branch information
Showing
15 changed files
with
233 additions
and
28 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
54 changes: 54 additions & 0 deletions
54
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,54 @@ | ||
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) | ||
|
||
// As of 1.3.x | ||
// Baseline -- 1500 | ||
// v1, no skip -- 2000 | ||
// v2, with skip -- 3000 [withdrawn] | ||
@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
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
35 changes: 35 additions & 0 deletions
35
...ts/json/commonTest/src/kotlinx/serialization/features/DefaultPolymorphicSerializerTest.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,35 @@ | ||
/* | ||
* Copyright 2017-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
package kotlinx.serialization.features | ||
|
||
import kotlinx.serialization.* | ||
import kotlinx.serialization.json.* | ||
import kotlinx.serialization.modules.* | ||
import kotlin.test.* | ||
|
||
class DefaultPolymorphicSerializerTest : JsonTestBase() { | ||
|
||
@Serializable | ||
abstract class Project { | ||
abstract val name: String | ||
} | ||
|
||
@Serializable | ||
data class DefaultProject(override val name: String, val type: String): Project() | ||
|
||
val module = SerializersModule { | ||
polymorphic(Project::class) { | ||
defaultDeserializer { DefaultProject.serializer() } | ||
} | ||
} | ||
|
||
private val json = Json { serializersModule = module } | ||
|
||
@Test | ||
fun test() = parametrizedTest { | ||
assertEquals(DefaultProject("example", "unknown"), | ||
json.decodeFromString<Project>(""" {"type":"unknown","name":"example"}""", it)) | ||
} | ||
|
||
} |
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
Oops, something went wrong.