Skip to content

Commit

Permalink
perf(java): optimize object array copy perf (apache#1770)
Browse files Browse the repository at this point in the history
## What does this PR do?

optimize object array copy perf

## Related issues

Closes apache#1749


## 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

```
Benchmark                             (bufferType)  (references)   Mode  Cnt        Score         Error  Units
CopyBenchmark.fury_copy_object_array         array         false  thrpt    3  3738130.731 ± 2558439.249  ops/s
CopyBenchmark.kryo_copy_object_array         array         false  thrpt    3  1840856.001 ± 3577628.201  ops/s

Benchmark                             (bufferType)  (references)   Mode  Cnt        Score         Error  Units
CopyBenchmark.fury_copy_object_array         array         false  thrpt    3  8025674.789 ± 7659990.718  ops/s
CopyBenchmark.kryo_copy_object_array         array         false  thrpt    3  1167037.754 ± 1372076.679  ops/s
```
  • Loading branch information
chaokunyang authored Jul 27, 2024
1 parent 4cdb9a2 commit f4865f0
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ public Object kryo_copy_list(KryoState.DataState state) {
return state.kryo.copy(state.data.intList);
}

@Benchmark
public Object fury_copy_object_array(FuryState.DataState state) {
return state.fury.copy(state.data.objectArray);
}

@Benchmark
public Object kryo_copy_object_array(KryoState.DataState state) {
return state.kryo.copy(state.data.objectArray);
}

public static void main(String[] args) throws IOException {
if (args.length == 0) {
String commandLine =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,14 @@ public static String newLongStr() {

public List<String> stringList = new ArrayList<>();
public List<Integer> intList = new ArrayList<>();
public Object[] objectArray = new Integer[20];
public Map<String, String> stringMap = new HashMap<>();
public Map<Integer, Integer> intMap = new HashMap<>();

{
for (int i = 0; i < 20; i++) {
stringList.add("hello, " + i);
objectArray[i] = i;
intList.add(i);
stringMap.put("key" + i, "value" + i);
intMap.put(i, i * 2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ public void setup() {
kryo.setRegistrationRequired(registerClass);
kryo.register(int[].class);
kryo.register(long[].class);
kryo.register(Object[].class);
kryo.register(Integer[].class);
kryo.register(String[].class);
kryo.register(ArrayList.class);
kryo.register(HashMap.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,19 @@ public void write(MemoryBuffer buffer, T[] arr) {
public T[] copy(T[] originArray) {
int length = originArray.length;
Object[] newArray = newArray(length);
for (int i = 0; i < length; i++) {
newArray[i] = fury.copyObject(originArray[i]);
Serializer componentSerializer = this.componentTypeSerializer;
if (componentSerializer != null) {
if (componentSerializer.isImmutable()) {
System.arraycopy(originArray, 0, newArray, 0, length);
} else {
for (int i = 0; i < length; i++) {
newArray[i] = componentSerializer.copy(originArray[i]);
}
}
} else {
for (int i = 0; i < length; i++) {
newArray[i] = fury.copyObject(originArray[i]);
}
}
return (T[]) newArray;
}
Expand Down

0 comments on commit f4865f0

Please sign in to comment.