Skip to content

Commit 3eb89ab

Browse files
committed
update demo
1 parent e674ad8 commit 3eb89ab

3 files changed

Lines changed: 114 additions & 32 deletions

File tree

demo/src/main/java/com/jsoniter/demo/SimpleObjectBinding.java

Lines changed: 102 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77
import com.fasterxml.jackson.databind.ObjectMapper;
88
import com.fasterxml.jackson.module.afterburner.AfterburnerModule;
99
import com.jsoniter.JsonIterator;
10+
import com.jsoniter.ReflectionDecoder;
11+
import com.jsoniter.spi.ExtensionManager;
1012
import com.jsoniter.spi.TypeLiteral;
1113
import org.junit.Test;
1214
import org.openjdk.jmh.Main;
1315
import org.openjdk.jmh.annotations.*;
16+
import org.openjdk.jmh.infra.BenchmarkParams;
1417
import org.openjdk.jmh.infra.Blackhole;
1518

1619
import java.io.IOException;
@@ -25,6 +28,7 @@ public class SimpleObjectBinding {
2528
private DslJson dslJson;
2629
private Class<TestObject> clazz;
2730
private String inputStr;
31+
private TestObject testObject;
2832

2933
@CompiledJson
3034
public static class TestObject {
@@ -44,7 +48,7 @@ public String toString() {
4448
private JsonIterator iter;
4549

4650
@Setup(Level.Trial)
47-
public void benchSetup() {
51+
public void benchSetup(BenchmarkParams params) {
4852
inputStr = "{'field1':100,'field2':101}";
4953
input = inputStr.replace('\'', '"').getBytes();
5054
iter = JsonIterator.parse(input);
@@ -54,60 +58,98 @@ public void benchSetup() {
5458
};
5559
clazz = TestObject.class;
5660
jackson = new ObjectMapper();
57-
jackson.registerModule(new AfterburnerModule());
5861
dslJson = new DslJson();
62+
testObject = new TestObject();
63+
if (params != null) {
64+
if (params.getBenchmark().contains("withReflection")) {
65+
ExtensionManager.registerTypeDecoder(TestObject.class, new ReflectionDecoder(TestObject.class));
66+
}
67+
if (params.getBenchmark().contains("withBindApiStrictMode")) {
68+
JsonIterator.enableStrictMode();
69+
}
70+
if (params.getBenchmark().contains("withJacksonAfterburner")) {
71+
jackson.registerModule(new AfterburnerModule());
72+
}
73+
}
5974
}
6075

6176
@Test
6277
public void test() throws IOException {
63-
benchSetup();
78+
benchSetup(null);
6479
System.out.println(withIterator());
65-
System.out.println(withBindApiNoneStrictMode());
66-
System.out.println(withBindApiStrictMode());
80+
System.out.println(withIteratorIfElse());
81+
System.out.println(withIteratorIntern());
82+
System.out.println(withBindApi());
83+
System.out.println(withExistingObject());
6784
System.out.println(withJackson());
6885
System.out.println(withDsljson());
6986
System.out.println(withFastjson());
7087
}
7188

72-
@Benchmark
89+
public static void main(String[] args) throws Exception {
90+
Main.main(new String[]{
91+
"SimpleObjectBinding.*",
92+
"-i", "5",
93+
"-wi", "5",
94+
"-f", "1"
95+
});
96+
}
97+
98+
// @Benchmark
7399
public void withIterator(Blackhole bh) throws IOException {
74100
bh.consume(withIterator());
75101
}
76102

77-
@Benchmark
78-
public void withBindApiNoneStrictMode(Blackhole bh) throws IOException {
79-
bh.consume(withBindApiNoneStrictMode());
103+
// @Benchmark
104+
public void withIteratorIfElse(Blackhole bh) throws IOException {
105+
bh.consume(withIteratorIfElse());
80106
}
81107

82-
@Benchmark
108+
// @Benchmark
109+
public void withIteratorIntern(Blackhole bh) throws IOException {
110+
bh.consume(withIteratorIntern());
111+
}
112+
113+
// @Benchmark
114+
public void withoutExistingObject(Blackhole bh) throws IOException {
115+
bh.consume(withBindApi());
116+
}
117+
118+
// @Benchmark
83119
public void withBindApiStrictMode(Blackhole bh) throws IOException {
84-
bh.consume(withBindApiStrictMode());
120+
bh.consume(withBindApi());
121+
}
122+
123+
// @Benchmark
124+
public void withReflection(Blackhole bh) throws IOException {
125+
bh.consume(withBindApi());
126+
}
127+
128+
// @Benchmark
129+
public void withExistingObject(Blackhole bh) throws IOException {
130+
bh.consume(withExistingObject());
85131
}
86132

87133
@Benchmark
88-
public void withJackson(Blackhole bh) throws IOException {
134+
public void withJacksonAfterburner(Blackhole bh) throws IOException {
89135
bh.consume(withJackson());
90136
}
91137

92138
@Benchmark
139+
public void withJacksonNoAfterburner(Blackhole bh) throws IOException {
140+
bh.consume(withJackson());
141+
}
142+
143+
// @Benchmark
93144
public void withDsljson(Blackhole bh) throws IOException {
94145
bh.consume(withDsljson());
95146
}
96147

97-
@Benchmark
148+
// @Benchmark
98149
public void withFastjson(Blackhole bh) throws IOException {
99150
bh.consume(withFastjson());
100151
}
101152

102-
public static void main(String[] args) throws Exception {
103-
Main.main(new String[]{
104-
"SimpleObjectBinding.*",
105-
"-i", "5",
106-
"-wi", "5",
107-
"-f", "1"
108-
});
109-
}
110-
111153
private TestObject withIterator() throws IOException {
112154
iter.reset();
113155
TestObject obj = new TestObject();
@@ -126,17 +168,51 @@ private TestObject withIterator() throws IOException {
126168
return obj;
127169
}
128170

129-
private TestObject withBindApiNoneStrictMode() throws IOException {
171+
private TestObject withIteratorIfElse() throws IOException {
130172
iter.reset();
131-
return iter.read(typeLiteral);
173+
TestObject obj = new TestObject();
174+
for (String field = iter.readObject(); field != null; field = iter.readObject()) {
175+
if (field.equals("field1")) {
176+
obj.field1 = iter.readInt();
177+
continue;
178+
}
179+
if (field.equals("field2")) {
180+
obj.field2 = iter.readInt();
181+
continue;
182+
}
183+
iter.skip();
184+
}
185+
return obj;
186+
}
187+
188+
private TestObject withIteratorIntern() throws IOException {
189+
iter.reset();
190+
TestObject obj = new TestObject();
191+
for (String field = iter.readObject(); field != null; field = iter.readObject()) {
192+
field = field.intern();
193+
if (field == "field1") {
194+
obj.field1 = iter.readInt();
195+
continue;
196+
}
197+
if (field == "field2") {
198+
obj.field2 = iter.readInt();
199+
continue;
200+
}
201+
iter.skip();
202+
}
203+
return obj;
132204
}
133205

134-
private TestObject withBindApiStrictMode() throws IOException {
135-
JsonIterator.enableStrictMode();
206+
private TestObject withBindApi() throws IOException {
136207
iter.reset();
137208
return iter.read(typeLiteral);
138209
}
139210

211+
private TestObject withExistingObject() throws IOException {
212+
iter.reset();
213+
return iter.read(typeLiteral, testObject);
214+
}
215+
140216
private TestObject withJackson() throws IOException {
141217
return jackson.readValue(input, typeRef);
142218
}

src/main/java/com/jsoniter/CodegenImplObject.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ public static String genObjectUsingSlice(Class clazz, String cacheKey, ClassDesc
4141
// === if null, return null
4242
append(lines, "if (iter.readNull()) { com.jsoniter.CodegenAccess.resetExistingObject(iter); return null; }");
4343
// === if input is empty object, return empty object
44-
append(lines, "long tracker = 0;");
44+
if (hasMandatoryField) {
45+
append(lines, "long tracker = 0;");
46+
}
4547
if (desc.ctor.parameters.isEmpty()) {
4648
append(lines, "{{clazz}} obj = {{newInst}};");
4749
append(lines, "if (!com.jsoniter.CodegenAccess.readObjectStart(iter)) {");
@@ -104,9 +106,11 @@ public static String genObjectUsingSlice(Class clazz, String cacheKey, ClassDesc
104106
}
105107
appendOnUnknownField(lines, desc);
106108
append(lines, "}"); // end of while
107-
append(lines, "if (tracker != " + expectedTracker + "L) {");
108-
appendMissingMandatoryFields(lines);
109-
append(lines, "}");
109+
if (hasMandatoryField) {
110+
append(lines, "if (tracker != " + expectedTracker + "L) {");
111+
appendMissingMandatoryFields(lines);
112+
append(lines, "}");
113+
}
110114
if (!desc.ctor.parameters.isEmpty()) {
111115
append(lines, String.format("%s obj = {{newInst}};", CodegenImplNative.getTypeName(clazz)));
112116
for (Binding field : desc.fields) {
@@ -213,7 +217,6 @@ private static void addFieldDispatch(
213217
append(lines, String.format("field.at(%d)==%s", i, b));
214218
append(lines, ") {");
215219
addFieldDispatch(lines, len, i + 1, next, cacheKey, new ArrayList<Byte>());
216-
append(lines, "continue;");
217220
append(lines, "}");
218221
}
219222
}

src/test/java/com/jsoniter/TestCustomizeField.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,11 @@ public void test_rename_field() throws IOException {
5555
ExtensionManager.registerExtension(new EmptyExtension() {
5656
@Override
5757
public void updateClassDescriptor(ClassDescriptor desc) {
58+
if (desc.clazz != TestObject4.class) {
59+
return;
60+
}
5861
for (Binding field : desc.allDecoderBindings()) {
59-
if (field.clazz == TestObject4.class && field.name.equals("field1")) {
62+
if (field.name.equals("field1")) {
6063
field.fromNames = new String[]{"field_1", "Field1"};
6164
}
6265
}

0 commit comments

Comments
 (0)