Skip to content

Commit

Permalink
chore(java): simplify generated codec name (#1850)
Browse files Browse the repository at this point in the history
## 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

<!--
Is there any related issue? Please attach here.

- #xxxx0
- #xxxx1
- #xxxx2
-->

## Does this PR introduce any user-facing change?

<!--
If any user-facing interface changes, please [open an
issue](https://github.com/apache/fury/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.
-->
  • Loading branch information
chaokunyang authored Sep 24, 2024
1 parent 1f1528f commit 89a8d01
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String, Integer> 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("$", "_");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, Map<String, Integer>> idGenerator = new ConcurrentHashMap<>();

public String codecClassName(Class<?> beanClass) {
String name = ReflectionUtils.getClassNameWithoutPackage(beanClass).replace("$", "_");
StringBuilder nameBuilder = new StringBuilder(name);
Expand All @@ -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<String, Integer> 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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Long, Integer> 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
Expand Down

0 comments on commit 89a8d01

Please sign in to comment.