From bcbe996a9d76d3b480c9f0be79d15e586a4c9414 Mon Sep 17 00:00:00 2001 From: "shaojin.wensj" Date: Sun, 12 May 2024 17:43:22 +0800 Subject: [PATCH 1/4] upgrade fastjson to 2.0.50 --- README.md | 2 +- build.gradle | 4 ++-- run | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 55b24f5..bab2943 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,7 @@ The results here-below were computed on January the 30th, 2024 with the followin | avaje-jsonb | 1.9 | | boon | 0.34 | | dsl-json | 1.10.0 | -| fastjson | 2.0.46 | +| fastjson | 2.0.50 | | flexjson | 3.3 | | genson | 1.6 | | gson | 2.10.1 | diff --git a/build.gradle b/build.gradle index 0c2c7b6..63a6055 100644 --- a/build.gradle +++ b/build.gradle @@ -45,8 +45,8 @@ dependencies { implementation group: 'com.dslplatform', name: 'dsl-json', version: "${dslJsonVersion}" annotationProcessor group: 'com.dslplatform', name: 'dsl-json', version: "${dslJsonVersion}" // FastJson - implementation group: 'com.alibaba.fastjson2', name: 'fastjson2', version: '2.0.48' - implementation group: 'com.alibaba.fastjson2', name: 'fastjson2-incubator-vector', version: '2.0.48' + implementation group: 'com.alibaba.fastjson2', name: 'fastjson2', version: '2.0.50' + implementation group: 'com.alibaba.fastjson2', name: 'fastjson2-incubator-vector', version: '2.0.50' // FlexJson implementation group: 'net.sf.flexjson', name: 'flexjson', version: '3.3' // GENSON diff --git a/run b/run index 57e5b96..887e94e 100755 --- a/run +++ b/run @@ -3,7 +3,7 @@ JAR=build/libs/app.jar HEAP_SIZE=2g -[ -z ${JVM_OPTIONS} ] && JVM_OPTIONS="-server -Xms${HEAP_SIZE} -Xmx${HEAP_SIZE} --add-opens=java.base/java.time=ALL-UNNAMED --add-modules=jdk.incubator.vector" +[ -z ${JVM_OPTIONS} ] && JVM_OPTIONS="-server -Xms${HEAP_SIZE} -Xmx${HEAP_SIZE} --add-opens=java.base/java.time=ALL-UNNAMED --add-modules=jdk.incubator.vector -Dfastjson2.features=disableReferenceDetect,disableArrayMapping,disableJSONB,disableAutoType,disableSmartMatch" [ -z ${SEED} ] && export SEED=${RANDOM} [ -z ${SHADOW} ] && echo ./gradlew clean build shadowJar && ./gradlew clean build shadowJar From 486b0885b3eff55291b45b1520f752d8079d989e Mon Sep 17 00:00:00 2001 From: "shaojin.wensj" Date: Sat, 1 Jun 2024 19:17:37 +0800 Subject: [PATCH 2/4] use fastjson 2.0.51 & add fastjson_features case --- README.md | 2 +- build.gradle | 4 ++-- run | 2 +- .../github/fabienrenaud/jjb/JsonBench.java | 4 ++++ .../jjb/databind/Deserialization.java | 18 +++++++++++++++++ .../jjb/databind/Serialization.java | 20 +++++++++++++++++++ 6 files changed, 46 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index bab2943..0e603f0 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,7 @@ The results here-below were computed on January the 30th, 2024 with the followin | avaje-jsonb | 1.9 | | boon | 0.34 | | dsl-json | 1.10.0 | -| fastjson | 2.0.50 | +| fastjson | 2.0.51 | | flexjson | 3.3 | | genson | 1.6 | | gson | 2.10.1 | diff --git a/build.gradle b/build.gradle index 63a6055..8ba6bd6 100644 --- a/build.gradle +++ b/build.gradle @@ -45,8 +45,8 @@ dependencies { implementation group: 'com.dslplatform', name: 'dsl-json', version: "${dslJsonVersion}" annotationProcessor group: 'com.dslplatform', name: 'dsl-json', version: "${dslJsonVersion}" // FastJson - implementation group: 'com.alibaba.fastjson2', name: 'fastjson2', version: '2.0.50' - implementation group: 'com.alibaba.fastjson2', name: 'fastjson2-incubator-vector', version: '2.0.50' + implementation group: 'com.alibaba.fastjson2', name: 'fastjson2', version: '2.0.51' + implementation group: 'com.alibaba.fastjson2', name: 'fastjson2-incubator-vector', version: '2.0.51' // FlexJson implementation group: 'net.sf.flexjson', name: 'flexjson', version: '3.3' // GENSON diff --git a/run b/run index 887e94e..57e5b96 100755 --- a/run +++ b/run @@ -3,7 +3,7 @@ JAR=build/libs/app.jar HEAP_SIZE=2g -[ -z ${JVM_OPTIONS} ] && JVM_OPTIONS="-server -Xms${HEAP_SIZE} -Xmx${HEAP_SIZE} --add-opens=java.base/java.time=ALL-UNNAMED --add-modules=jdk.incubator.vector -Dfastjson2.features=disableReferenceDetect,disableArrayMapping,disableJSONB,disableAutoType,disableSmartMatch" +[ -z ${JVM_OPTIONS} ] && JVM_OPTIONS="-server -Xms${HEAP_SIZE} -Xmx${HEAP_SIZE} --add-opens=java.base/java.time=ALL-UNNAMED --add-modules=jdk.incubator.vector" [ -z ${SEED} ] && export SEED=${RANDOM} [ -z ${SHADOW} ] && echo ./gradlew clean build shadowJar && ./gradlew clean build shadowJar diff --git a/src/main/java/com/github/fabienrenaud/jjb/JsonBench.java b/src/main/java/com/github/fabienrenaud/jjb/JsonBench.java index f17e137..2abd3cf 100644 --- a/src/main/java/com/github/fabienrenaud/jjb/JsonBench.java +++ b/src/main/java/com/github/fabienrenaud/jjb/JsonBench.java @@ -51,6 +51,10 @@ public Object fastjson() throws Exception { return null; } + public Object fastjson_features() throws Exception { + return null; + } + public Object jsonio() throws Exception { return null; } diff --git a/src/main/java/com/github/fabienrenaud/jjb/databind/Deserialization.java b/src/main/java/com/github/fabienrenaud/jjb/databind/Deserialization.java index 6e93923..6b912a3 100644 --- a/src/main/java/com/github/fabienrenaud/jjb/databind/Deserialization.java +++ b/src/main/java/com/github/fabienrenaud/jjb/databind/Deserialization.java @@ -1,6 +1,8 @@ package com.github.fabienrenaud.jjb.databind; import com.alibaba.fastjson2.JSON; +import com.alibaba.fastjson2.JSONReader; +import com.alibaba.fastjson2.reader.ObjectReaderProvider; import com.bluelinelabs.logansquare.LoganSquare; import com.github.fabienrenaud.jjb.JsonBench; import com.github.fabienrenaud.jjb.data.JsonSource; @@ -15,6 +17,16 @@ * @author Fabien Renaud */ public class Deserialization extends JsonBench { + static final ObjectReaderProvider featuresProvider = new ObjectReaderProvider(); + static final JSONReader.Context featuresContext; + static { + featuresProvider.setDisableArrayMapping(true); + featuresProvider.setDisableAutoType(true); + featuresProvider.setDisableJSONB(true); + featuresProvider.setDisableReferenceDetect(true); + featuresProvider.setDisableSmartMatch(true); + featuresContext = new JSONReader.Context(featuresProvider); + } public JsonSource JSON_SOURCE() { return CLI_JSON_SOURCE; @@ -62,6 +74,12 @@ public Object fastjson() { return JSON.parseObject(JSON_SOURCE().nextByteArray(), JSON_SOURCE().pojoType()); } + @Benchmark + @Override + public Object fastjson_features() { + return JSON.parseObject(JSON_SOURCE().nextByteArray(), JSON_SOURCE().pojoType(), featuresContext); + } + @Benchmark @Override public Object flexjson() throws JsonSyntaxException { diff --git a/src/main/java/com/github/fabienrenaud/jjb/databind/Serialization.java b/src/main/java/com/github/fabienrenaud/jjb/databind/Serialization.java index 03ea550..f2b4a96 100644 --- a/src/main/java/com/github/fabienrenaud/jjb/databind/Serialization.java +++ b/src/main/java/com/github/fabienrenaud/jjb/databind/Serialization.java @@ -1,6 +1,9 @@ package com.github.fabienrenaud.jjb.databind; import com.alibaba.fastjson2.JSON; +import com.alibaba.fastjson2.JSONFactory; +import com.alibaba.fastjson2.JSONWriter; +import com.alibaba.fastjson2.writer.ObjectWriterProvider; import com.bluelinelabs.logansquare.LoganSquare; import com.github.fabienrenaud.jjb.JsonBench; import com.github.fabienrenaud.jjb.JsonUtils; @@ -13,6 +16,15 @@ import java.io.ByteArrayOutputStream; public class Serialization extends JsonBench { + static final ObjectWriterProvider featuresProvider = new ObjectWriterProvider(); + static final JSONWriter.Context featuresContext; + static { + featuresProvider.setDisableAutoType(true); + featuresProvider.setDisableArrayMapping(true); + featuresProvider.setDisableJSONB(true); + featuresProvider.setDisableReferenceDetect(true); + featuresContext = JSONFactory.createWriteContext(featuresProvider); + } public JsonSource JSON_SOURCE() { return CLI_JSON_SOURCE; @@ -74,6 +86,14 @@ public Object fastjson() throws Exception { return baos; } + @Benchmark + @Override + public Object fastjson_features() throws Exception { + ByteArrayOutputStream baos = JsonUtils.byteArrayOutputStream(); + JSON.writeTo(baos, JSON_SOURCE().nextPojo(), featuresContext); + return baos; + } + @Benchmark @Override public Object flexjson() { From 1086101a43633edd47e094622bb005503f797563 Mon Sep 17 00:00:00 2001 From: "shaojin.wensj" Date: Fri, 7 Jun 2024 09:22:03 +0800 Subject: [PATCH 3/4] revert README fastjson version --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0e603f0..55b24f5 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,7 @@ The results here-below were computed on January the 30th, 2024 with the followin | avaje-jsonb | 1.9 | | boon | 0.34 | | dsl-json | 1.10.0 | -| fastjson | 2.0.51 | +| fastjson | 2.0.46 | | flexjson | 3.3 | | genson | 1.6 | | gson | 2.10.1 | From 5debaeda7acff3443f0f179a367bfa1c817ec4fc Mon Sep 17 00:00:00 2001 From: "shaojin.wensj" Date: Tue, 11 Jun 2024 09:42:27 +0800 Subject: [PATCH 4/4] fastjsonFeatures behind JsonProvider --- .../fabienrenaud/jjb/data/JsonSource.java | 31 +++++++++++++++++++ .../jjb/databind/Deserialization.java | 15 +-------- .../jjb/databind/Serialization.java | 15 +-------- 3 files changed, 33 insertions(+), 28 deletions(-) diff --git a/src/main/java/com/github/fabienrenaud/jjb/data/JsonSource.java b/src/main/java/com/github/fabienrenaud/jjb/data/JsonSource.java index ec7dbd3..8d542c1 100644 --- a/src/main/java/com/github/fabienrenaud/jjb/data/JsonSource.java +++ b/src/main/java/com/github/fabienrenaud/jjb/data/JsonSource.java @@ -1,5 +1,10 @@ package com.github.fabienrenaud.jjb.data; +import com.alibaba.fastjson2.JSONFactory; +import com.alibaba.fastjson2.JSONReader; +import com.alibaba.fastjson2.JSONWriter; +import com.alibaba.fastjson2.reader.ObjectReaderProvider; +import com.alibaba.fastjson2.writer.ObjectWriterProvider; import com.github.fabienrenaud.jjb.RandomUtils; import com.github.fabienrenaud.jjb.data.gen.DataGenerator; import com.github.fabienrenaud.jjb.provider.JsonProvider; @@ -29,6 +34,8 @@ public abstract class JsonSource { private final StreamSerializer streamSerializer; private final StreamDeserializer streamDeserializer; + private final FastjsonProvider fastjsonFeatures; + JsonSource(int quantity, int individualSize, JsonProvider provider, DataGenerator dataGenerator, StreamSerializer streamSerializer, StreamDeserializer streamDeserializer) { this.provider = provider; @@ -49,6 +56,24 @@ public abstract class JsonSource { } return arr; }); + + ObjectWriterProvider featuresWriterProvider = new ObjectWriterProvider(); + featuresWriterProvider.setDisableAutoType(true); + featuresWriterProvider.setDisableArrayMapping(true); + featuresWriterProvider.setDisableJSONB(true); + featuresWriterProvider.setDisableReferenceDetect(true); + JSONFactory.createWriteContext(featuresWriterProvider); + + ObjectReaderProvider featuresReaderProvider = new ObjectReaderProvider(); + featuresReaderProvider.setDisableArrayMapping(true); + featuresReaderProvider.setDisableAutoType(true); + featuresReaderProvider.setDisableJSONB(true); + featuresReaderProvider.setDisableReferenceDetect(true); + featuresReaderProvider.setDisableSmartMatch(true); + + fastjsonFeatures = new FastjsonProvider( + JSONFactory.createReadContext(featuresReaderProvider), + JSONFactory.createWriteContext(featuresWriterProvider)); } private void populateFields(int quantity, int individualSize) { @@ -124,4 +149,10 @@ public StreamDeserializer streamDeserializer() { private int index(int bound) { return bound == 1 ? 0 : RandomUtils.nextInt(bound); } + + public FastjsonProvider fastjsonFeatures() { + return fastjsonFeatures; + } + + public record FastjsonProvider (JSONReader.Context readerContext, JSONWriter.Context writerContext) {} } diff --git a/src/main/java/com/github/fabienrenaud/jjb/databind/Deserialization.java b/src/main/java/com/github/fabienrenaud/jjb/databind/Deserialization.java index 6b912a3..72511e3 100644 --- a/src/main/java/com/github/fabienrenaud/jjb/databind/Deserialization.java +++ b/src/main/java/com/github/fabienrenaud/jjb/databind/Deserialization.java @@ -1,8 +1,6 @@ package com.github.fabienrenaud.jjb.databind; import com.alibaba.fastjson2.JSON; -import com.alibaba.fastjson2.JSONReader; -import com.alibaba.fastjson2.reader.ObjectReaderProvider; import com.bluelinelabs.logansquare.LoganSquare; import com.github.fabienrenaud.jjb.JsonBench; import com.github.fabienrenaud.jjb.data.JsonSource; @@ -17,17 +15,6 @@ * @author Fabien Renaud */ public class Deserialization extends JsonBench { - static final ObjectReaderProvider featuresProvider = new ObjectReaderProvider(); - static final JSONReader.Context featuresContext; - static { - featuresProvider.setDisableArrayMapping(true); - featuresProvider.setDisableAutoType(true); - featuresProvider.setDisableJSONB(true); - featuresProvider.setDisableReferenceDetect(true); - featuresProvider.setDisableSmartMatch(true); - featuresContext = new JSONReader.Context(featuresProvider); - } - public JsonSource JSON_SOURCE() { return CLI_JSON_SOURCE; } @@ -77,7 +64,7 @@ public Object fastjson() { @Benchmark @Override public Object fastjson_features() { - return JSON.parseObject(JSON_SOURCE().nextByteArray(), JSON_SOURCE().pojoType(), featuresContext); + return JSON.parseObject(JSON_SOURCE().nextByteArray(), JSON_SOURCE().pojoType(), JSON_SOURCE().fastjsonFeatures().readerContext()); } @Benchmark diff --git a/src/main/java/com/github/fabienrenaud/jjb/databind/Serialization.java b/src/main/java/com/github/fabienrenaud/jjb/databind/Serialization.java index f2b4a96..b31296b 100644 --- a/src/main/java/com/github/fabienrenaud/jjb/databind/Serialization.java +++ b/src/main/java/com/github/fabienrenaud/jjb/databind/Serialization.java @@ -1,9 +1,6 @@ package com.github.fabienrenaud.jjb.databind; import com.alibaba.fastjson2.JSON; -import com.alibaba.fastjson2.JSONFactory; -import com.alibaba.fastjson2.JSONWriter; -import com.alibaba.fastjson2.writer.ObjectWriterProvider; import com.bluelinelabs.logansquare.LoganSquare; import com.github.fabienrenaud.jjb.JsonBench; import com.github.fabienrenaud.jjb.JsonUtils; @@ -16,16 +13,6 @@ import java.io.ByteArrayOutputStream; public class Serialization extends JsonBench { - static final ObjectWriterProvider featuresProvider = new ObjectWriterProvider(); - static final JSONWriter.Context featuresContext; - static { - featuresProvider.setDisableAutoType(true); - featuresProvider.setDisableArrayMapping(true); - featuresProvider.setDisableJSONB(true); - featuresProvider.setDisableReferenceDetect(true); - featuresContext = JSONFactory.createWriteContext(featuresProvider); - } - public JsonSource JSON_SOURCE() { return CLI_JSON_SOURCE; } @@ -90,7 +77,7 @@ public Object fastjson() throws Exception { @Override public Object fastjson_features() throws Exception { ByteArrayOutputStream baos = JsonUtils.byteArrayOutputStream(); - JSON.writeTo(baos, JSON_SOURCE().nextPojo(), featuresContext); + JSON.writeTo(baos, JSON_SOURCE().nextPojo(), JSON_SOURCE().fastjsonFeatures().writerContext()); return baos; }