This is an automated email from the ASF dual-hosted git repository. chaokunyang pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/fory.git
The following commit(s) were added to refs/heads/main by this push: new 5a1509795 feat(kotlin): add cache to kotlin default value support (#2419) 5a1509795 is described below commit 5a15097950c182b55f5813550220fd6592839303 Author: Shawn Yang <shawn.ck.y...@gmail.com> AuthorDate: Wed Jul 16 13:35:27 2025 +0800 feat(kotlin): add cache to kotlin default value support (#2419) ## What does this PR do? add cache to kotlin default value support ## Related issues #2416 ## Does this PR introduce any user-facing change? <!-- If any user-facing interface changes, please [open an issue](https://github.com/apache/fory/issues/new/choose) describing the need to do so and update the document if necessary. --> - [ ] Does this PR introduce any public API change? - [ ] Does this PR introduce any binary protocol compatibility change? ## Benchmark <!-- When the PR has an impact on performance (if you don't know whether the PR will have an impact on performance, you can submit the PR first, and if it will have impact on performance, the code reviewer will explain it), be sure to attach a benchmark data here. --> --- .../apache/fory/collection/ClassValueCache.java | 90 ++++++++++++++++++++++ .../org/apache/fory/util/DefaultValueUtils.java | 15 ++-- .../fory-core/native-image.properties | 1 + .../serializer/kotlin/KotlinDefaultValueSupport.kt | 42 +++++----- 4 files changed, 116 insertions(+), 32 deletions(-) diff --git a/java/fory-core/src/main/java/org/apache/fory/collection/ClassValueCache.java b/java/fory-core/src/main/java/org/apache/fory/collection/ClassValueCache.java new file mode 100644 index 000000000..c9668dc34 --- /dev/null +++ b/java/fory-core/src/main/java/org/apache/fory/collection/ClassValueCache.java @@ -0,0 +1,90 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.fory.collection; + +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; +import java.util.concurrent.Callable; +import org.apache.fory.annotation.Internal; +import org.apache.fory.util.GraalvmSupport; + +@Internal +public class ClassValueCache<T> { + + private final Cache<Class<?>, T> cache; + + private ClassValueCache(Cache<Class<?>, T> cache) { + this.cache = cache; + } + + public T getIfPresent(Class<?> k) { + return cache.getIfPresent(k); + } + + public T get(Class<?> k, Callable<? extends T> loader) throws Exception { + return cache.get(k, loader); + } + + public void put(Class<?> k, T v) { + cache.put(k, v); + } + + /** + * Create a cache with weak keys. + * + * <p>when in graalvm, the cache is a concurrent hash map. when in jvm, the cache is a weak hash + * map. + * + * @param concurrencyLevel the concurrency level + * @return the cache + */ + public static <T> ClassValueCache<T> newClassKeyCache(int concurrencyLevel) { + if (GraalvmSupport.isGraalBuildtime()) { + return new ClassValueCache<>( + CacheBuilder.newBuilder().concurrencyLevel(concurrencyLevel).build()); + } else { + return new ClassValueCache<>( + CacheBuilder.newBuilder().weakKeys().concurrencyLevel(concurrencyLevel).build()); + } + } + + /** + * Create a cache with weak keys and soft values. + * + * <p>when in graalvm, the cache is a concurrent hash map. when in jvm, the cache is a weak hash + * map. + * + * @param concurrencyLevel the concurrency level + * @return the cache + */ + public static <T> ClassValueCache<T> newClassKeySoftCache(int concurrencyLevel) { + if (GraalvmSupport.isGraalBuildtime()) { + return new ClassValueCache<>( + CacheBuilder.newBuilder().concurrencyLevel(concurrencyLevel).build()); + } else { + return new ClassValueCache<>( + CacheBuilder.newBuilder() + .weakKeys() + .softValues() + .concurrencyLevel(concurrencyLevel) + .build()); + } + } +} diff --git a/java/fory-core/src/main/java/org/apache/fory/util/DefaultValueUtils.java b/java/fory-core/src/main/java/org/apache/fory/util/DefaultValueUtils.java index f540d333a..892f78737 100644 --- a/java/fory-core/src/main/java/org/apache/fory/util/DefaultValueUtils.java +++ b/java/fory-core/src/main/java/org/apache/fory/util/DefaultValueUtils.java @@ -19,7 +19,6 @@ package org.apache.fory.util; -import com.google.common.cache.Cache; import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandles; import java.lang.reflect.Constructor; @@ -30,7 +29,7 @@ import java.util.List; import java.util.Map; import org.apache.fory.Fory; import org.apache.fory.annotation.Internal; -import org.apache.fory.collection.Collections; +import org.apache.fory.collection.ClassValueCache; import org.apache.fory.logging.Logger; import org.apache.fory.logging.LoggerFactory; import org.apache.fory.memory.Platform; @@ -50,12 +49,12 @@ import org.apache.fory.util.unsafe._JDKAccess; public class DefaultValueUtils { private static final Logger LOG = LoggerFactory.getLogger(DefaultValueUtils.class); - private static final Cache<Class<?>, Map<Integer, Object>> cachedCtrDefaultValues = - Collections.newClassKeySoftCache(32); - private static final Cache<Class<?>, DefaultValueField[]> defaultValueFieldsCache = - Collections.newClassKeySoftCache(32); - private static final Cache<Class<?>, Map<String, Object>> allDefaultValuesCache = - Collections.newClassKeySoftCache(32); + private static final ClassValueCache<Map<Integer, Object>> cachedCtrDefaultValues = + ClassValueCache.newClassKeySoftCache(32); + private static final ClassValueCache<DefaultValueField[]> defaultValueFieldsCache = + ClassValueCache.newClassKeySoftCache(32); + private static final ClassValueCache<Map<String, Object>> allDefaultValuesCache = + ClassValueCache.newClassKeySoftCache(32); /** Field info for scala/kotlin class fields with default values. */ public static final class DefaultValueField { diff --git a/java/fory-core/src/main/resources/META-INF/native-image/org.apache.fory/fory-core/native-image.properties b/java/fory-core/src/main/resources/META-INF/native-image/org.apache.fory/fory-core/native-image.properties index c64bb3891..319193bfa 100644 --- a/java/fory-core/src/main/resources/META-INF/native-image/org.apache.fory/fory-core/native-image.properties +++ b/java/fory-core/src/main/resources/META-INF/native-image/org.apache.fory/fory-core/native-image.properties @@ -201,6 +201,7 @@ Args=--initialize-at-build-time=org.apache.fory.memory.MemoryBuffer,\ org.apache.fory.codegen.Expression$Literal,\ org.apache.fory.codegen.Expression$Reference,\ org.apache.fory.codegen.JaninoUtils,\ + org.apache.fory.collection.ClassValueCache,\ org.apache.fory.collection.ForyObjectMap,\ org.apache.fory.collection.IdentityMap,\ org.apache.fory.collection.IdentityObjectIntMap,\ diff --git a/kotlin/src/main/kotlin/org/apache/fory/serializer/kotlin/KotlinDefaultValueSupport.kt b/kotlin/src/main/kotlin/org/apache/fory/serializer/kotlin/KotlinDefaultValueSupport.kt index 64ca7a5bc..ec0ba91cd 100644 --- a/kotlin/src/main/kotlin/org/apache/fory/serializer/kotlin/KotlinDefaultValueSupport.kt +++ b/kotlin/src/main/kotlin/org/apache/fory/serializer/kotlin/KotlinDefaultValueSupport.kt @@ -24,6 +24,7 @@ import kotlin.reflect.KParameter import kotlin.reflect.full.memberProperties import kotlin.reflect.full.primaryConstructor import kotlin.reflect.jvm.javaType +import org.apache.fory.collection.ClassValueCache import org.apache.fory.logging.Logger import org.apache.fory.logging.LoggerFactory import org.apache.fory.memory.Platform @@ -38,22 +39,24 @@ import org.apache.fory.util.DefaultValueUtils */ internal class KotlinDefaultValueSupport : DefaultValueUtils.DefaultValueSupport() { private val LOG: Logger = LoggerFactory.getLogger(KotlinDefaultValueSupport::class.java) + private val cachedKotlinDataClassDefaultValues = + ClassValueCache.newClassKeyCache<Map<String, Any>>(32) override fun hasDefaultValues(cls: Class<*>): Boolean { - return try { - if (!isKotlinDataClass(cls)) { - return false - } - getAllDefaultValues(cls).isNotEmpty() - } catch (e: Exception) { - LOG.warn("Error checking default values for class ${cls.name}: ${e.message}") - false + if (!isKotlinDataClass(cls)) { + return false + } else { + return getAllDefaultValues(cls).isNotEmpty() } } override fun getAllDefaultValues(cls: Class<*>): Map<String, Any> { + cachedKotlinDataClassDefaultValues.getIfPresent(cls)?.let { + return it + } return try { if (!isKotlinDataClass(cls)) { + cachedKotlinDataClassDefaultValues.put(cls, emptyMap()) return emptyMap() } @@ -68,6 +71,7 @@ internal class KotlinDefaultValueSupport : DefaultValueUtils.DefaultValueSupport if (defaultValue != null) { argsMap[parameter] = defaultValue } else { + cachedKotlinDataClassDefaultValues.put(cls, emptyMap()) // If we can't provide a default for a required parameter, we can't get any defaults return emptyMap() } @@ -89,34 +93,24 @@ internal class KotlinDefaultValueSupport : DefaultValueUtils.DefaultValueSupport } } } + cachedKotlinDataClassDefaultValues.put(cls, defaultValues) defaultValues } catch (e: Exception) { LOG.info("Error getting default values for class ${cls.name}: ${e.message}") + cachedKotlinDataClassDefaultValues.put(cls, emptyMap()) emptyMap() } } override fun getDefaultValue(cls: Class<*>, fieldName: String): Any? { - return try { - if (!isKotlinDataClass(cls)) { - return null - } - getAllDefaultValues(cls)[fieldName] - } catch (e: Exception) { - LOG.info( - "Error getting default value for field $fieldName in class ${cls.name}: ${e.message}" - ) - null + if (!isKotlinDataClass(cls)) { + return null } + return getAllDefaultValues(cls)[fieldName] } private fun isKotlinDataClass(cls: Class<*>): Boolean { - return try { - cls.kotlin.isData - } catch (e: Exception) { - LOG.info("Error checking if class ${cls.name} is a Kotlin data class: ${e.message}") - false - } + return cls.kotlin.isData } private fun getDefaultValueForType(type: Type): Any? { --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@fory.apache.org For additional commands, e-mail: commits-h...@fory.apache.org