-
Notifications
You must be signed in to change notification settings - Fork 647
Implemented serializers caching for lookup #2015
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9bfdad5
Implemented non-parametrized serializers caching for lookup
shanshin 6360cfa
Marked common val as @SharedImmutable
shanshin 0c6a0a5
Parametrized serializers caching
shanshin f6eb35f
review fixes
shanshin 9655d85
~fix
shanshin fb14eaf
~remove nullability of serializers module
shanshin 3c86e42
~review fixes
shanshin 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
69 changes: 69 additions & 0 deletions
69
benchmark/src/jmh/kotlin/kotlinx/benchmarks/json/LookupOverheadBenchmark.kt
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.