From d7cc2db5b2305481afed880afde85ffd47089d38 Mon Sep 17 00:00:00 2001 From: chaokunyang Date: Mon, 22 Jul 2024 11:23:29 +0800 Subject: [PATCH 1/3] optimize map deep copy performance --- .../collection/AbstractMapSerializer.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/java/fury-core/src/main/java/org/apache/fury/serializer/collection/AbstractMapSerializer.java b/java/fury-core/src/main/java/org/apache/fury/serializer/collection/AbstractMapSerializer.java index a488402c8a..02b8e61a19 100644 --- a/java/fury-core/src/main/java/org/apache/fury/serializer/collection/AbstractMapSerializer.java +++ b/java/fury-core/src/main/java/org/apache/fury/serializer/collection/AbstractMapSerializer.java @@ -29,6 +29,7 @@ import org.apache.fury.memory.MemoryBuffer; import org.apache.fury.reflect.ReflectionUtils; import org.apache.fury.reflect.TypeRef; +import org.apache.fury.resolver.ClassInfo; import org.apache.fury.resolver.ClassInfoHolder; import org.apache.fury.resolver.ClassResolver; import org.apache.fury.resolver.RefResolver; @@ -417,8 +418,20 @@ public T xread(MemoryBuffer buffer) { } protected void copyEntry(Map originMap, Map newMap) { + ClassResolver classResolver = fury.getClassResolver(); for (Map.Entry entry : originMap.entrySet()) { - newMap.put(fury.copyObject(entry.getKey()), fury.copyObject(entry.getValue())); + K key = entry.getKey(); + if (key != null) { + ClassInfo classInfo = classResolver.getClassInfo(key.getClass(), keyClassInfoWriteCache); + key = fury.copyObject(key, classInfo.getClassId()); + } + V value = entry.getValue(); + if (value != null) { + ClassInfo classInfo = + classResolver.getClassInfo(value.getClass(), valueClassInfoWriteCache); + value = fury.copyObject(value, classInfo.getClassId()); + } + newMap.put(key, value); } } From 1e9cbe993eda5a47367bf30da92122c86e224c42 Mon Sep 17 00:00:00 2001 From: chaokunyang Date: Sat, 27 Jul 2024 14:31:32 +0800 Subject: [PATCH 2/3] optimize map serialization perf --- .../fury/serializer/collection/AbstractMapSerializer.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/java/fury-core/src/main/java/org/apache/fury/serializer/collection/AbstractMapSerializer.java b/java/fury-core/src/main/java/org/apache/fury/serializer/collection/AbstractMapSerializer.java index 02b8e61a19..620f14269f 100644 --- a/java/fury-core/src/main/java/org/apache/fury/serializer/collection/AbstractMapSerializer.java +++ b/java/fury-core/src/main/java/org/apache/fury/serializer/collection/AbstractMapSerializer.java @@ -423,13 +423,17 @@ protected void copyEntry(Map originMap, Map newMap) { K key = entry.getKey(); if (key != null) { ClassInfo classInfo = classResolver.getClassInfo(key.getClass(), keyClassInfoWriteCache); - key = fury.copyObject(key, classInfo.getClassId()); + if (!classInfo.getSerializer().isImmutable()) { + key = fury.copyObject(key, classInfo.getClassId()); + } } V value = entry.getValue(); if (value != null) { ClassInfo classInfo = classResolver.getClassInfo(value.getClass(), valueClassInfoWriteCache); - value = fury.copyObject(value, classInfo.getClassId()); + if (!classInfo.getSerializer().isImmutable()) { + value = fury.copyObject(value, classInfo.getClassId()); + } } newMap.put(key, value); } From bc5b80a8a255797caaa63fd6bc20775061d10091 Mon Sep 17 00:00:00 2001 From: chaokunyang Date: Sat, 27 Jul 2024 15:18:26 +0800 Subject: [PATCH 3/3] add container copy benchmark --- .../apache/fury/benchmark/CopyBenchmark.java | 32 ++++++++++++++++++- .../org/apache/fury/benchmark/data/Data.java | 18 +++++++++++ .../fury/benchmark/state/KryoState.java | 3 ++ 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/java/benchmark/src/main/java/org/apache/fury/benchmark/CopyBenchmark.java b/java/benchmark/src/main/java/org/apache/fury/benchmark/CopyBenchmark.java index 1940871fc6..90f7025ef4 100644 --- a/java/benchmark/src/main/java/org/apache/fury/benchmark/CopyBenchmark.java +++ b/java/benchmark/src/main/java/org/apache/fury/benchmark/CopyBenchmark.java @@ -42,10 +42,40 @@ public Object kryo_copy(KryoState.KryoUserTypeState state) { return state.kryo.copy(state.object); } + @Benchmark + public Object fury_copy_string_map(FuryState.DataState state) { + return state.fury.copy(state.data.stringMap); + } + + @Benchmark + public Object fury_copy_int_map(FuryState.DataState state) { + return state.fury.copy(state.data.intMap); + } + + @Benchmark + public Object kryo_copy_string_map(KryoState.DataState state) { + return state.kryo.copy(state.data.stringMap); + } + + @Benchmark + public Object kryo_copy_int_map(KryoState.DataState state) { + return state.kryo.copy(state.data.intMap); + } + + @Benchmark + public Object fury_copy_list(FuryState.DataState state) { + return state.fury.copy(state.data.intList); + } + + @Benchmark + public Object kryo_copy_list(KryoState.DataState state) { + return state.kryo.copy(state.data.intList); + } + public static void main(String[] args) throws IOException { if (args.length == 0) { String commandLine = - "org.apache.fury.*CopyBenchmark.* -f 1 -wi 3 -i 3 -t 1 -w 2s -r 2s -rf csv " + "org.apache.fury.*CopyBenchmark.*map -f 1 -wi 3 -i 3 -t 1 -w 2000s -r 2s -rf csv " + "-p bufferType=array -p references=false"; System.out.println(commandLine); args = commandLine.split(" "); diff --git a/java/benchmark/src/main/java/org/apache/fury/benchmark/data/Data.java b/java/benchmark/src/main/java/org/apache/fury/benchmark/data/Data.java index 09f595429d..b66309aef9 100644 --- a/java/benchmark/src/main/java/org/apache/fury/benchmark/data/Data.java +++ b/java/benchmark/src/main/java/org/apache/fury/benchmark/data/Data.java @@ -19,6 +19,10 @@ package org.apache.fury.benchmark.data; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import org.apache.fury.util.StringUtils; public class Data { @@ -93,4 +97,18 @@ public static String newLongStr() { } return StringUtils.random(strLength); } + + public List stringList = new ArrayList<>(); + public List intList = new ArrayList<>(); + public Map stringMap = new HashMap<>(); + public Map intMap = new HashMap<>(); + + { + for (int i = 0; i < 20; i++) { + stringList.add("hello, " + i); + intList.add(i); + stringMap.put("key" + i, "value" + i); + intMap.put(i, i * 2); + } + } } diff --git a/java/benchmark/src/main/java/org/apache/fury/benchmark/state/KryoState.java b/java/benchmark/src/main/java/org/apache/fury/benchmark/state/KryoState.java index b773caeb1a..836787722d 100644 --- a/java/benchmark/src/main/java/org/apache/fury/benchmark/state/KryoState.java +++ b/java/benchmark/src/main/java/org/apache/fury/benchmark/state/KryoState.java @@ -26,6 +26,7 @@ import com.esotericsoftware.kryo.unsafe.UnsafeByteBufferInput; import com.esotericsoftware.kryo.unsafe.UnsafeByteBufferOutput; import java.util.ArrayList; +import java.util.HashMap; import org.apache.fury.benchmark.IntsSerializationSuite; import org.apache.fury.benchmark.LongStringSerializationSuite; import org.apache.fury.benchmark.LongsSerializationSuite; @@ -85,6 +86,8 @@ public void setup() { kryo.setRegistrationRequired(registerClass); kryo.register(int[].class); kryo.register(long[].class); + kryo.register(ArrayList.class); + kryo.register(HashMap.class); } }