Skip to content

Commit 05395a8

Browse files
committed
optimize Any
1 parent 8058807 commit 05395a8

40 files changed

+512
-3476
lines changed

demo/pom.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@
7676
<artifactId>dsl-json</artifactId>
7777
<version>1.3.2</version>
7878
</dependency>
79-
<dependency>
80-
<groupId>com.dslplatform</groupId>
81-
<artifactId>dsl-json-processor</artifactId>
82-
<version>1.4.1</version>
83-
</dependency>
79+
<!--<dependency>-->
80+
<!--<groupId>com.dslplatform</groupId>-->
81+
<!--<artifactId>dsl-json-processor</artifactId>-->
82+
<!--<version>1.4.1</version>-->
83+
<!--</dependency>-->
8484
<dependency>
8585
<groupId>com.alibaba</groupId>
8686
<artifactId>fastjson</artifactId>

demo/src/test/java/com/jsoniter/demo/ArrayBinding.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public void withDsljson(Blackhole bh) throws IOException {
8080
}
8181

8282
private int withJsoniter() throws IOException {
83-
iter.reset();
83+
iter.reset(input);
8484
int[] arr = iter.read(typeLiteral);
8585
int total = 0;
8686
for (int i = 0; i < arr.length; i++) {
@@ -108,7 +108,7 @@ private int withDsljson() throws IOException {
108108
}
109109

110110
private int withIterator() throws IOException {
111-
iter.reset();
111+
iter.reset(input);
112112
int total = 0;
113113
while (iter.readArray()) {
114114
total += iter.readInt();

demo/src/test/java/com/jsoniter/demo/ConstructorBinding.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public void withJackson(Blackhole bh) throws IOException {
115115
}
116116

117117
private TestObject withJsoniter() throws IOException {
118-
iter.reset();
118+
iter.reset(input);
119119
return iter.read(typeLiteral);
120120
}
121121

demo/src/test/java/com/jsoniter/demo/FieldMatching.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ public class FieldMatching {
2424
private TypeLiteral<TestObject4> testObject4Type;
2525
private JsonIterator iter0;
2626
private JsonIterator iter1Success;
27+
private byte[] iter0Input;
28+
private byte[] iter1SuccessInput;
2729

2830
public static class TestObject0 {
2931
public int field1;
@@ -60,8 +62,10 @@ public static class TestObject4 {
6062
public void benchSetup() {
6163
JsoniterAnnotationSupport.enable();
6264
JsonIterator.setMode(DecodingMode.DYNAMIC_MODE_AND_MATCH_FIELD_STRICTLY);
63-
iter0 = JsonIterator.parse("{'field1':101,'field2':101,'field3':101}".replace('\'', '"').getBytes());
64-
iter1Success = JsonIterator.parse("{'field1':101,'field2':101,'field3':101}".replace('\'', '"').getBytes());
65+
iter0Input = "{'field1':101,'field2':101,'field3':101}".replace('\'', '"').getBytes();
66+
iter0 = JsonIterator.parse(iter0Input);
67+
iter1SuccessInput = "{'field1':101,'field2':101,'field3':101}".replace('\'', '"').getBytes();
68+
iter1Success = JsonIterator.parse(iter1SuccessInput);
6569
testObject0Type = new TypeLiteral<TestObject0>() {
6670
};
6771
testObject1Type = new TypeLiteral<TestObject1>() {
@@ -118,13 +122,13 @@ public static void main(String[] args) throws Exception {
118122

119123
@Benchmark
120124
public void iter0(Blackhole bh) throws IOException {
121-
iter0.reset();
125+
iter0.reset(iter0Input);
122126
bh.consume(iter0.read(testObject0Type));
123127
}
124128

125129
@Benchmark
126130
public void iter1Success(Blackhole bh) throws IOException {
127-
iter1Success.reset();
131+
iter1Success.reset(iter1SuccessInput);
128132
bh.consume(iter1Success.read(testObject1Type));
129133
}
130134
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.jsoniter.demo;
2+
3+
import com.jsoniter.Any;
4+
import com.jsoniter.JsonIterator;
5+
import com.jsoniter.Slice;
6+
import org.junit.Test;
7+
import org.openjdk.jmh.Main;
8+
import org.openjdk.jmh.annotations.*;
9+
import org.openjdk.jmh.infra.BenchmarkParams;
10+
import org.openjdk.jmh.infra.Blackhole;
11+
12+
import java.io.IOException;
13+
import java.io.InputStream;
14+
import java.util.List;
15+
import java.util.Map;
16+
17+
@State(Scope.Thread)
18+
public class LazyAny {
19+
20+
private JsonIterator iter;
21+
private Slice input;
22+
23+
@Setup(Level.Trial)
24+
public void benchSetup(BenchmarkParams params) throws IOException {
25+
InputStream resourceAsStream = LazyAny.class.getResourceAsStream("/large.json");
26+
byte[] buf = new byte[32 * 1024];
27+
int size = resourceAsStream.read(buf);
28+
input = new Slice(buf, 0, size);
29+
iter = new JsonIterator();
30+
}
31+
32+
public static void main(String[] args) throws Exception {
33+
Main.main(new String[]{
34+
"LazyAny",
35+
"-i", "5",
36+
"-wi", "5",
37+
"-f", "1",
38+
"-prof", "stack",
39+
});
40+
}
41+
42+
@Test
43+
public void test() throws IOException {
44+
benchSetup(null);
45+
System.out.println(jsoniter());
46+
System.out.println(jsoniter_object());
47+
}
48+
49+
@Benchmark
50+
public void jsoniter(Blackhole bh) throws IOException {
51+
bh.consume(jsoniter());
52+
}
53+
54+
@Benchmark
55+
public void jsoniter_object(Blackhole bh) throws IOException {
56+
bh.consume(jsoniter_object());
57+
}
58+
59+
public int jsoniter() throws IOException {
60+
iter.reset(input);
61+
Any users = iter.readAny();
62+
int total = 0;
63+
for (Any user : users) {
64+
total += user.getValue("friends").size();
65+
}
66+
return total;
67+
}
68+
69+
public int jsoniter_object() throws IOException {
70+
iter.reset(input);
71+
List users = (List) iter.read();
72+
int total = 0;
73+
for (Object userObj : users) {
74+
Map user = (Map) userObj;
75+
List friends = (List) user.get("friends");
76+
total += friends.size();
77+
}
78+
return total;
79+
}
80+
}

demo/src/test/java/com/jsoniter/demo/ListBinding.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public void withDsljson(Blackhole bh) throws IOException {
7575
}
7676

7777
private List<String> withJsoniter() throws IOException {
78-
iter.reset();
78+
iter.reset(input);
7979
return iter.read(typeLiteral);
8080
}
8181

demo/src/test/java/com/jsoniter/demo/MapBinding.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public void withDsljson(Blackhole bh) throws IOException {
7777
}
7878

7979
private Map<String, Float> withJsoniter() throws IOException {
80-
iter.reset();
80+
iter.reset(input);
8181
return iter.read(typeLiteral);
8282
}
8383

demo/src/test/java/com/jsoniter/demo/ObjectOutput.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.dslplatform.json.DslJson;
66
import com.fasterxml.jackson.core.JsonGenerator;
77
import com.fasterxml.jackson.databind.ObjectMapper;
8+
import com.jsoniter.output.EncodingMode;
89
import com.jsoniter.output.JsonStream;
910
import com.jsoniter.spi.TypeLiteral;
1011
import org.junit.Test;
@@ -59,6 +60,7 @@ public void benchSetup(BenchmarkParams params) {
5960
baos = new ByteArrayOutputStream(1024 * 64);
6061
objectMapper = new ObjectMapper();
6162
objectMapper.getFactory().configure(JsonGenerator.Feature.ESCAPE_NON_ASCII, true);
63+
JsonStream.setMode(EncodingMode.DYNAMIC_MODE);
6264
stream = new JsonStream(baos, 4096);
6365
buffer = new byte[4096];
6466
dslJson = new DslJson();

demo/src/test/java/com/jsoniter/demo/PrivateFieldBinding.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public static void main(String[] args) throws Exception {
100100
}
101101

102102
private TestObject withJsoniter() throws IOException {
103-
iter.reset();
103+
iter.reset(input);
104104
return iter.read(typeLiteral);
105105
}
106106

demo/src/test/java/com/jsoniter/demo/SetterBinding.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public void withJackson(Blackhole bh) throws IOException {
110110
}
111111

112112
private ConstructorBinding.TestObject withJsoniter() throws IOException {
113-
iter.reset();
113+
iter.reset(input);
114114
return iter.read(typeLiteral);
115115
}
116116

0 commit comments

Comments
 (0)