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/fury.git
The following commit(s) were added to refs/heads/main by this push:
new bc6a0b58 perf(java): Refactor field sorting in StructSerializer to
cache transformed field names and avoid redundant computation (#2091)
bc6a0b58 is described below
commit bc6a0b586aa2ab99e3a0059ee645c7b9901e92a3
Author: LouShaokun <[email protected]>
AuthorDate: Tue Mar 18 11:00:56 2025 +0800
perf(java): Refactor field sorting in StructSerializer to cache transformed
field names and avoid redundant computation (#2091)
<!--
**Thanks for contributing to Fury.**
**If this is your first time opening a PR on fury, you can refer to
[CONTRIBUTING.md](https://github.com/apache/fury/blob/main/CONTRIBUTING.md).**
Contribution Checklist
- The **Apache Fury (incubating)** community has restrictions on the
naming of pr titles. You can also find instructions in
[CONTRIBUTING.md](https://github.com/apache/fury/blob/main/CONTRIBUTING.md).
- Fury has a strong focus on performance. If the PR you submit will have
an impact on performance, please benchmark it first and provide the
benchmark result here.
-->
## What does this PR do?
This PR refactors the field sorting in `StructSerializer.java` by
caching transformed field names to avoid redundant computations during
sorting. The `lowerCamelToLowerUnderscore` transformation is now applied
once per field instead of multiple times.
## 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.
-->
---
.../main/java/org/apache/fury/serializer/StructSerializer.java | 9 +++++++--
1 file changed, 7 insertions(+), 2 deletions(-)
diff --git
a/java/fury-core/src/main/java/org/apache/fury/serializer/StructSerializer.java
b/java/fury-core/src/main/java/org/apache/fury/serializer/StructSerializer.java
index 8a289c2c..e3c1d460 100644
---
a/java/fury-core/src/main/java/org/apache/fury/serializer/StructSerializer.java
+++
b/java/fury-core/src/main/java/org/apache/fury/serializer/StructSerializer.java
@@ -21,9 +21,9 @@ package org.apache.fury.serializer;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
+import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Arrays;
-import java.util.Comparator;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.List;
@@ -83,7 +83,12 @@ public class StructSerializer<T> extends Serializer<T> {
this.constructor = ctr;
fieldAccessors =
Descriptor.getFields(cls).stream()
- .sorted(Comparator.comparing(f ->
StringUtils.lowerCamelToLowerUnderscore(f.getName())))
+ .map(
+ f ->
+ new AbstractMap.SimpleEntry<>(
+ f,
StringUtils.lowerCamelToLowerUnderscore(f.getName())))
+ .sorted(Map.Entry.comparingByValue())
+ .map(Map.Entry::getKey)
.map(FieldAccessor::createAccessor)
.toArray(FieldAccessor[]::new);
fieldGenerics = buildFieldGenerics(fury, TypeRef.of(cls), fieldAccessors);
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]