From 435ed3d2094cbed2117a57891c5f117118523353 Mon Sep 17 00:00:00 2001 From: chaokunyang Date: Mon, 23 Sep 2024 22:34:14 -0700 Subject: [PATCH 1/3] simplify generated codec name --- .../fury/builder/BaseObjectCodecBuilder.java | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/java/fury-core/src/main/java/org/apache/fury/builder/BaseObjectCodecBuilder.java b/java/fury-core/src/main/java/org/apache/fury/builder/BaseObjectCodecBuilder.java index 8a3315dc6c..305e4fa963 100644 --- a/java/fury-core/src/main/java/org/apache/fury/builder/BaseObjectCodecBuilder.java +++ b/java/fury-core/src/main/java/org/apache/fury/builder/BaseObjectCodecBuilder.java @@ -56,6 +56,7 @@ import java.util.Map; import java.util.Set; import java.util.StringJoiner; +import java.util.concurrent.ConcurrentHashMap; import java.util.function.Function; import java.util.function.Supplier; import org.apache.fury.Fury; @@ -157,6 +158,8 @@ public BaseObjectCodecBuilder(TypeRef beanType, Fury fury, Class parentSer jitCallbackUpdateFields = new HashMap<>(); } + private final Map> idGenerator = new ConcurrentHashMap<>(); + public String codecClassName(Class beanClass) { String name = ReflectionUtils.getClassNameWithoutPackage(beanClass).replace("$", "_"); StringBuilder nameBuilder = new StringBuilder(name); @@ -167,12 +170,22 @@ public String codecClassName(Class beanClass) { } else { nameBuilder.append("Fury"); } - nameBuilder.append(codecSuffix()).append("Codec"); - nameBuilder.append('_').append(fury.getConfig().getConfigHash()); - String classUniqueId = CodeGenerator.getClassUniqueId(beanClass); - if (StringUtils.isNotBlank(classUniqueId)) { - nameBuilder.append('_').append(classUniqueId); + nameBuilder.append("Codec"); + Map subGenerator = + idGenerator.computeIfAbsent(nameBuilder.toString(), k -> new ConcurrentHashMap<>()); + String key = + codecSuffix() + + "_" + + fury.getConfig().getConfigHash() + + "_" + + CodeGenerator.getClassUniqueId(beanClass); + Integer id = subGenerator.get(key); + if (id == null) { + synchronized (subGenerator) { + id = subGenerator.computeIfAbsent(key, k -> subGenerator.size()); + } } + nameBuilder.append('_').append(id); return nameBuilder.toString(); } From 7126ef6a7117c98a6ac93ea346d4ce648f55e633 Mon Sep 17 00:00:00 2001 From: chaokunyang Date: Mon, 23 Sep 2024 22:47:07 -0700 Subject: [PATCH 2/3] fix codecSuffix --- .../apache/fury/builder/BaseObjectCodecBuilder.java | 9 ++------- .../apache/fury/builder/MetaSharedCodecBuilder.java | 12 +++++++++++- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/java/fury-core/src/main/java/org/apache/fury/builder/BaseObjectCodecBuilder.java b/java/fury-core/src/main/java/org/apache/fury/builder/BaseObjectCodecBuilder.java index 305e4fa963..acb60ef27e 100644 --- a/java/fury-core/src/main/java/org/apache/fury/builder/BaseObjectCodecBuilder.java +++ b/java/fury-core/src/main/java/org/apache/fury/builder/BaseObjectCodecBuilder.java @@ -170,15 +170,10 @@ public String codecClassName(Class beanClass) { } else { nameBuilder.append("Fury"); } - nameBuilder.append("Codec"); + nameBuilder.append("Codec").append(codecSuffix()); Map subGenerator = idGenerator.computeIfAbsent(nameBuilder.toString(), k -> new ConcurrentHashMap<>()); - String key = - codecSuffix() - + "_" - + fury.getConfig().getConfigHash() - + "_" - + CodeGenerator.getClassUniqueId(beanClass); + String key = fury.getConfig().getConfigHash() + "_" + CodeGenerator.getClassUniqueId(beanClass); Integer id = subGenerator.get(key); if (id == null) { synchronized (subGenerator) { diff --git a/java/fury-core/src/main/java/org/apache/fury/builder/MetaSharedCodecBuilder.java b/java/fury-core/src/main/java/org/apache/fury/builder/MetaSharedCodecBuilder.java index fcfb86da90..980ac83b66 100644 --- a/java/fury-core/src/main/java/org/apache/fury/builder/MetaSharedCodecBuilder.java +++ b/java/fury-core/src/main/java/org/apache/fury/builder/MetaSharedCodecBuilder.java @@ -22,7 +22,9 @@ import static org.apache.fury.builder.Generated.GeneratedMetaSharedSerializer.SERIALIZER_FIELD_NAME; import java.util.Collection; +import java.util.Map; import java.util.SortedMap; +import java.util.concurrent.ConcurrentHashMap; import org.apache.fury.Fury; import org.apache.fury.builder.Generated.GeneratedMetaSharedSerializer; import org.apache.fury.codegen.CodeGenerator; @@ -86,11 +88,19 @@ public MetaSharedCodecBuilder(TypeRef beanType, Fury fury, ClassDef classDef) new ObjectCodecOptimizer(beanClass, grouper, !fury.isBasicTypesRefIgnored(), ctx); } + private final Map idGenerator = new ConcurrentHashMap<>(); + @Override protected String codecSuffix() { // For every class def sent from different peer, if the class def are different, then // a new serializer needs being generated. - return "MetaShared" + classDef.getId(); + Integer id = idGenerator.get(classDef.getId()); + if (id == null) { + synchronized (idGenerator) { + id = idGenerator.computeIfAbsent(classDef.getId(), k -> idGenerator.size()); + } + } + return "MetaShared" + id; } @Override From 824393f71d365c06316468450edae4d187a0a8ba Mon Sep 17 00:00:00 2001 From: chaokunyang Date: Mon, 23 Sep 2024 23:04:35 -0700 Subject: [PATCH 3/3] use static idGenerator --- .../org/apache/fury/builder/AccessorHelper.java | 16 ++++++++++++---- .../fury/builder/BaseObjectCodecBuilder.java | 3 ++- .../fury/builder/MetaSharedCodecBuilder.java | 3 ++- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/java/fury-core/src/main/java/org/apache/fury/builder/AccessorHelper.java b/java/fury-core/src/main/java/org/apache/fury/builder/AccessorHelper.java index cb52429034..44edd41412 100644 --- a/java/fury-core/src/main/java/org/apache/fury/builder/AccessorHelper.java +++ b/java/fury-core/src/main/java/org/apache/fury/builder/AccessorHelper.java @@ -27,6 +27,7 @@ import java.util.Collection; import java.util.Map; import java.util.WeakHashMap; +import java.util.concurrent.ConcurrentHashMap; import org.apache.fury.codegen.CodeGenerator; import org.apache.fury.codegen.CodegenContext; import org.apache.fury.codegen.CompileUnit; @@ -53,11 +54,18 @@ public class AccessorHelper { private static final String OBJ_NAME = "obj"; private static final String FIELD_VALUE = "fieldValue"; + // Must be static to be shared across the whole process life. + private static final Map idGenerator = new ConcurrentHashMap<>(); + public static String accessorClassName(Class beanClass) { - String name = - ReflectionUtils.getClassNameWithoutPackage(beanClass) - + "FuryAccessor_" - + CodeGenerator.getClassUniqueId(beanClass); + String key = CodeGenerator.getClassUniqueId(beanClass); + Integer id = idGenerator.get(key); + if (id == null) { + synchronized (idGenerator) { + id = idGenerator.computeIfAbsent(key, k -> idGenerator.size()); + } + } + String name = ReflectionUtils.getClassNameWithoutPackage(beanClass) + "FuryAccessor_" + id; return name.replace("$", "_"); } diff --git a/java/fury-core/src/main/java/org/apache/fury/builder/BaseObjectCodecBuilder.java b/java/fury-core/src/main/java/org/apache/fury/builder/BaseObjectCodecBuilder.java index acb60ef27e..e58098bb5d 100644 --- a/java/fury-core/src/main/java/org/apache/fury/builder/BaseObjectCodecBuilder.java +++ b/java/fury-core/src/main/java/org/apache/fury/builder/BaseObjectCodecBuilder.java @@ -158,7 +158,8 @@ public BaseObjectCodecBuilder(TypeRef beanType, Fury fury, Class parentSer jitCallbackUpdateFields = new HashMap<>(); } - private final Map> idGenerator = new ConcurrentHashMap<>(); + // Must be static to be shared across the whole process life. + private static final Map> idGenerator = new ConcurrentHashMap<>(); public String codecClassName(Class beanClass) { String name = ReflectionUtils.getClassNameWithoutPackage(beanClass).replace("$", "_"); diff --git a/java/fury-core/src/main/java/org/apache/fury/builder/MetaSharedCodecBuilder.java b/java/fury-core/src/main/java/org/apache/fury/builder/MetaSharedCodecBuilder.java index 980ac83b66..eb980c0280 100644 --- a/java/fury-core/src/main/java/org/apache/fury/builder/MetaSharedCodecBuilder.java +++ b/java/fury-core/src/main/java/org/apache/fury/builder/MetaSharedCodecBuilder.java @@ -88,7 +88,8 @@ public MetaSharedCodecBuilder(TypeRef beanType, Fury fury, ClassDef classDef) new ObjectCodecOptimizer(beanClass, grouper, !fury.isBasicTypesRefIgnored(), ctx); } - private final Map idGenerator = new ConcurrentHashMap<>(); + // Must be static to be shared across the whole process life. + private static final Map idGenerator = new ConcurrentHashMap<>(); @Override protected String codecSuffix() {