Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented serializers caching for lookup
- Loading branch information
Showing
10 changed files
with
424 additions
and
53 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
benchmark/src/jmh/kotlin/kotlinx/benchmarks/json/LookupOverheadBenchmark.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,69 @@ | ||
/* | ||
* Copyright 2017-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.benchmarks.json | ||
|
||
import kotlinx.serialization.* | ||
import kotlinx.serialization.builtins.* | ||
import kotlinx.serialization.json.* | ||
import org.openjdk.jmh.annotations.* | ||
import java.util.concurrent.* | ||
|
||
@Warmup(iterations = 7, time = 1) | ||
@Measurement(iterations = 7, time = 1) | ||
@BenchmarkMode(Mode.Throughput) | ||
@OutputTimeUnit(TimeUnit.MICROSECONDS) | ||
@State(Scope.Benchmark) | ||
@Fork(2) | ||
open class LookupOverheadBenchmark { | ||
|
||
@Serializable | ||
class Holder(val a: String) | ||
|
||
@Serializable | ||
class Generic<T>(val a: T) | ||
|
||
@Serializable | ||
class DoubleGeneric<T1, T2>(val a: T1, val b: T2) | ||
|
||
@Serializable | ||
class PentaGeneric<T1, T2, T3, T4, T5>(val a: T1, val b: T2, val c: T3, val d: T4, val e: T5) | ||
|
||
private val data = """{"a":""}""" | ||
private val doubleData = """{"a":"","b":0}""" | ||
private val pentaData = """{"a":"","b":0,"c":1,"d":true,"e":" "}""" | ||
|
||
@Serializable | ||
object Object | ||
|
||
@Benchmark | ||
fun dataReified() = Json.decodeFromString<Holder>(data) | ||
|
||
@Benchmark | ||
fun dataPlain() = Json.decodeFromString(Holder.serializer(), data) | ||
|
||
@Benchmark | ||
fun genericReified() = Json.decodeFromString<Generic<String>>(data) | ||
|
||
@Benchmark | ||
fun genericPlain() = Json.decodeFromString(Generic.serializer(String.serializer()), data) | ||
|
||
@Benchmark | ||
fun doubleGenericReified() = Json.decodeFromString<DoubleGeneric<String, Int>>(doubleData) | ||
|
||
@Benchmark | ||
fun doubleGenericPlain() = Json.decodeFromString(DoubleGeneric.serializer(String.serializer(), Int.serializer()), doubleData) | ||
|
||
@Benchmark | ||
fun pentaGenericReified() = Json.decodeFromString<PentaGeneric<String, Int, Long, Boolean, Char>>(pentaData) | ||
|
||
@Benchmark | ||
fun pentaGenericPlain() = Json.decodeFromString(PentaGeneric.serializer(String.serializer(), Int.serializer(), Long.serializer(), Boolean.serializer(), Char.serializer()), pentaData) | ||
|
||
@Benchmark | ||
fun objectReified() = Json.decodeFromString<Object>("{}") | ||
|
||
@Benchmark | ||
fun objectPlain() = Json.decodeFromString(Object.serializer(), "{}") | ||
} |
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
74 changes: 74 additions & 0 deletions
74
core/commonMain/src/kotlinx/serialization/SerializersCache.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,74 @@ | ||
/* | ||
* Copyright 2017-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.serialization | ||
|
||
import kotlinx.serialization.builtins.nullable | ||
import kotlinx.serialization.internal.cast | ||
import kotlinx.serialization.internal.createCache | ||
import kotlinx.serialization.internal.createParametrizedCache | ||
import kotlinx.serialization.modules.EmptySerializersModule | ||
import kotlin.native.concurrent.ThreadLocal | ||
import kotlin.reflect.KClass | ||
import kotlin.reflect.KType | ||
|
||
|
||
/** | ||
* Cache for non-null non-parametrized and non-contextual serializers. | ||
*/ | ||
@ThreadLocal | ||
private val SERIALIZERS_CACHE = createCache { it.serializerOrNull() } | ||
|
||
/** | ||
* Cache for nullable non-parametrized and non-contextual serializers. | ||
*/ | ||
@ThreadLocal | ||
private val SERIALIZERS_CACHE_NULLABLE = createCache<Any?> { it.serializerOrNull()?.nullable?.cast() } | ||
|
||
/** | ||
* Cache for non-null parametrized and non-contextual serializers. | ||
*/ | ||
@ThreadLocal | ||
private val PARAMETRIZED_SERIALIZERS_CACHE = createParametrizedCache { clazz, types -> | ||
val serializers = EmptySerializersModule().serializersForParameters(types, true)!! | ||
clazz.parametrizedSerializerOrNull(types, serializers) | ||
} | ||
|
||
/** | ||
* Cache for nullable parametrized and non-contextual serializers. | ||
*/ | ||
@ThreadLocal | ||
private val PARAMETRIZED_SERIALIZERS_CACHE_NULLABLE = createParametrizedCache<Any?> { clazz, types -> | ||
val serializers = EmptySerializersModule().serializersForParameters(types, true)!! | ||
clazz.parametrizedSerializerOrNull(types, serializers)?.nullable?.cast() | ||
} | ||
|
||
/** | ||
* Find cacheable serializer in the cache. | ||
* If serializer is cacheable but missed in cache - it will be created, placed into the cache and returned. | ||
*/ | ||
internal fun findCachedSerializer(clazz: KClass<Any>, isNullable: Boolean): KSerializer<Any?>? { | ||
return if (!isNullable) { | ||
SERIALIZERS_CACHE.get(clazz)?.cast() | ||
} else { | ||
SERIALIZERS_CACHE_NULLABLE.get(clazz) | ||
} | ||
} | ||
|
||
/** | ||
* Find cacheable parametrized serializer in the cache. | ||
* If serializer is cacheable but missed in cache - it will be created, placed into the cache and returned. | ||
*/ | ||
internal fun findParametrizedCachedSerializer( | ||
clazz: KClass<Any>, | ||
types: List<KType>, | ||
isNullable: Boolean | ||
): Result<KSerializer<Any?>?> { | ||
return if (!isNullable) { | ||
@Suppress("UNCHECKED_CAST") | ||
PARAMETRIZED_SERIALIZERS_CACHE.get(clazz, types) as Result<KSerializer<Any?>?> | ||
} else { | ||
PARAMETRIZED_SERIALIZERS_CACHE_NULLABLE.get(clazz, types) | ||
} | ||
} |
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.