From 89a8d012901c10981e9fe0b29ba06bfad6321716 Mon Sep 17 00:00:00 2001 From: Shawn Yang Date: Mon, 23 Sep 2024 23:09:34 -0700 Subject: [PATCH] chore(java): simplify generated codec name (#1850) ## What does this PR do? simplify generated codec name before: ![image](https://github.com/user-attachments/assets/bbb51fe7-23d1-405a-89c4-e4d0c4c8ab60) after: ![image](https://github.com/user-attachments/assets/f3540884-89e7-4a81-9d04-d3dff66ff379) ## Related issues ## Does this PR introduce any user-facing change? - [ ] Does this PR introduce any public API change? - [ ] Does this PR introduce any binary protocol compatibility change? ## Benchmark --- .../apache/fury/builder/AccessorHelper.java | 16 ++++++++++++---- .../fury/builder/BaseObjectCodecBuilder.java | 19 ++++++++++++++----- .../fury/builder/MetaSharedCodecBuilder.java | 13 ++++++++++++- 3 files changed, 38 insertions(+), 10 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 8a3315dc6c..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 @@ -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,9 @@ public BaseObjectCodecBuilder(TypeRef beanType, Fury fury, Class parentSer jitCallbackUpdateFields = new HashMap<>(); } + // 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("$", "_"); StringBuilder nameBuilder = new StringBuilder(name); @@ -167,12 +171,17 @@ 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").append(codecSuffix()); + Map subGenerator = + idGenerator.computeIfAbsent(nameBuilder.toString(), k -> new ConcurrentHashMap<>()); + String key = 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(); } 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..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 @@ -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,20 @@ public MetaSharedCodecBuilder(TypeRef beanType, Fury fury, ClassDef classDef) new ObjectCodecOptimizer(beanClass, grouper, !fury.isBasicTypesRefIgnored(), ctx); } + // Must be static to be shared across the whole process life. + private static 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