Skip to content

Commit f161804

Browse files
committed
optimize ser
1 parent a67f109 commit f161804

File tree

13 files changed

+327
-167
lines changed

13 files changed

+327
-167
lines changed

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

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22

33

44
import com.dslplatform.json.DslJson;
5+
import com.dslplatform.json.JsonWriter;
6+
import com.dslplatform.json.NumberConverter;
57
import com.fasterxml.jackson.core.JsonGenerator;
68
import com.fasterxml.jackson.databind.ObjectMapper;
9+
import com.jsoniter.annotation.JsonWrapper;
710
import com.jsoniter.output.JsonStream;
811
import org.junit.Test;
912
import org.openjdk.jmh.Main;
1013
import org.openjdk.jmh.annotations.*;
1114
import org.openjdk.jmh.infra.BenchmarkParams;
15+
import org.openjdk.jmh.infra.Blackhole;
1216

1317
import java.io.ByteArrayOutputStream;
1418
import java.io.IOException;
@@ -21,24 +25,26 @@ public class IntegerOutput {
2125
private JsonStream stream;
2226
private byte[] buffer;
2327
private DslJson dslJson;
28+
private JsonWriter jsonWriter;
2429

2530
public static void main(String[] args) throws Exception {
2631
Main.main(new String[]{
2732
"IntegerOutput",
2833
"-i", "5",
2934
"-wi", "5",
3035
"-f", "1",
36+
"-prof", "stack"
3137
});
3238
}
3339

3440
@Test
3541
public void test() throws IOException {
3642
benchSetup(null);
37-
jsoniter();
43+
jsoniter(null);
3844
System.out.println(baos.toString());
3945
jackson();
4046
System.out.println(baos.toString());
41-
dsljson();
47+
dsljson(null);
4248
System.out.println(baos.toString());
4349
}
4450

@@ -50,14 +56,16 @@ public void benchSetup(BenchmarkParams params) {
5056
stream = new JsonStream(baos, 4096);
5157
buffer = new byte[4096];
5258
dslJson = new DslJson();
59+
jsonWriter = new JsonWriter();
5360
}
5461

5562
@Benchmark
56-
public void jsoniter() throws IOException {
63+
public void jsoniter(Blackhole bh) throws IOException {
5764
baos.reset();
5865
stream.reset(baos);
5966
stream.writeVal(1024);
6067
stream.flush();
68+
// bh.consume(stream);
6169
}
6270

6371
@Benchmark
@@ -66,9 +74,12 @@ public void jackson() throws IOException {
6674
objectMapper.writeValue(baos, 1024);
6775
}
6876

69-
@Benchmark
70-
public void dsljson() throws IOException {
71-
baos.reset();
72-
dslJson.serialize(1024, baos);
77+
// @Benchmark
78+
public void dsljson(Blackhole bh) throws IOException {
79+
// baos.reset();
80+
jsonWriter.reset();
81+
NumberConverter.serialize(1024, jsonWriter);
82+
// bh.consume(jsonWriter);
83+
// jsonWriter.toStream(baos);
7384
}
7485
}

src/main/java/com/jsoniter/IterImplNumber.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,34 @@
1+
/*
2+
this implementations contains significant code from https://github.com/ngs-doo/dsl-json/blob/master/LICENSE
3+
4+
Copyright (c) 2015, Nova Generacija Softvera d.o.o.
5+
All rights reserved.
6+
7+
Redistribution and use in source and binary forms, with or without
8+
modification, are permitted provided that the following conditions are met:
9+
10+
* Redistributions of source code must retain the above copyright notice,
11+
this list of conditions and the following disclaimer.
12+
13+
* Redistributions in binary form must reproduce the above copyright notice,
14+
this list of conditions and the following disclaimer in the documentation
15+
and/or other materials provided with the distribution.
16+
17+
* Neither the name of Nova Generacija Softvera d.o.o. nor the names of its
18+
contributors may be used to endorse or promote products derived from this
19+
software without specific prior written permission.
20+
21+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
22+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+
*/
132
package com.jsoniter;
233

334
import java.io.IOException;

src/main/java/com/jsoniter/JsonIterator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ public void skip() throws IOException {
339339
IterImplSkip.skip(this);
340340
}
341341

342-
private static ThreadLocal<JsonIterator> tlsIter = new ThreadLocal<JsonIterator>() {
342+
public static ThreadLocal<JsonIterator> tlsIter = new ThreadLocal<JsonIterator>() {
343343
@Override
344344
protected JsonIterator initialValue() {
345345
return new JsonIterator();

src/main/java/com/jsoniter/any/LazyAny.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,6 @@
1010

1111
abstract class LazyAny extends Any {
1212

13-
protected final static ThreadLocal<JsonIterator> tlsIter = new ThreadLocal<JsonIterator>() {
14-
@Override
15-
protected JsonIterator initialValue() {
16-
return new JsonIterator();
17-
}
18-
};
1913
protected final byte[] data;
2014
protected final int head;
2115
protected final int tail;
@@ -65,7 +59,7 @@ public String toString() {
6559
}
6660

6761
public final JsonIterator parse() {
68-
JsonIterator iter = tlsIter.get();
62+
JsonIterator iter = JsonIterator.tlsIter.get();
6963
iter.reset(data, head, tail);
7064
return iter;
7165
}

src/main/java/com/jsoniter/any/ObjectLazyAny.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ private Any fillCache(Object target) throws IOException {
104104
return value;
105105
}
106106
}
107-
JsonIterator iter = tlsIter.get();
107+
JsonIterator iter = JsonIterator.tlsIter.get();
108108
iter.reset(data, lastParsedPos, tail);
109109
if (cache == null) {
110110
cache = new HashMap<Object, Any>(4);
@@ -140,7 +140,7 @@ private void fillCache() {
140140
return;
141141
}
142142
try {
143-
JsonIterator iter = tlsIter.get();
143+
JsonIterator iter = JsonIterator.tlsIter.get();
144144
iter.reset(data, lastParsedPos, tail);
145145
if (cache == null) {
146146
cache = new HashMap<Object, Any>(4);

src/main/java/com/jsoniter/output/CodegenImplArray.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,35 @@ public static CodegenResult genCollection(Class clazz, Type[] typeArgs) {
5656
} else if (clazz == Set.class) {
5757
clazz = HashSet.class;
5858
}
59-
return genCollection(clazz, compType);
59+
if (List.class.isAssignableFrom(clazz)) {
60+
return genList(clazz, compType);
61+
} else {
62+
return genCollection(clazz, compType);
63+
}
64+
}
65+
66+
private static CodegenResult genList(Class clazz, Type compType) {
67+
CodegenResult ctx = new CodegenResult();
68+
ctx.append("public static void encode_(java.lang.Object obj, com.jsoniter.output.JsonStream stream) throws java.io.IOException {");
69+
ctx.append("if (obj == null) { stream.writeNull(); return; }");
70+
ctx.append("java.util.List list = (java.util.List)obj;");
71+
ctx.append("int size = list.size();");
72+
ctx.append("if (size == 0) { return; }");
73+
ctx.buffer('[');
74+
ctx.append("java.lang.Object e = list.get(0);");
75+
ctx.append("if (e == null) { stream.writeNull(); } else {");
76+
CodegenImplNative.genWriteOp(ctx, "e", compType, true);
77+
ctx.append("}");
78+
ctx.append("for (int i = 1; i < size; i++) {");
79+
ctx.append("stream.write(',');");
80+
ctx.append("e = list.get(i);");
81+
ctx.append("if (e == null) { stream.writeNull(); } else {");
82+
CodegenImplNative.genWriteOp(ctx, "e", compType, true);
83+
ctx.append("}");
84+
ctx.append("}");
85+
ctx.buffer(']');
86+
ctx.append("}");
87+
return ctx;
6088
}
6189

6290
private static CodegenResult genCollection(Class clazz, Type compType) {

src/main/java/com/jsoniter/output/CodegenImplMap.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.jsoniter.output;
22

33
import java.lang.reflect.Type;
4+
import java.util.Collection;
45

56
class CodegenImplMap {
67
public static CodegenResult genMap(Class clazz, Type[] typeArgs) {

src/main/java/com/jsoniter/output/CodegenResult.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package com.jsoniter.output;
22

3+
import com.jsoniter.JsonException;
4+
import com.jsoniter.JsonIterator;
5+
6+
import java.io.IOException;
7+
38
class CodegenResult {
49

510
String prelude = null; // first
@@ -17,7 +22,16 @@ public static String bufferToWriteOp(String buffered) {
1722
return String.format("stream.write('%s', '%s', '%s');",
1823
escape(buffered.charAt(0)), escape(buffered.charAt(1)), escape(buffered.charAt(2)));
1924
} else {
20-
return String.format("stream.writeRaw(\"%s\");", buffered);
25+
JsonIterator iter = JsonIterator.tlsIter.get();
26+
String escapedStr = '"' + buffered + '"';
27+
iter.reset(escapedStr.getBytes());
28+
int unescapedLen;
29+
try {
30+
unescapedLen = iter.readString().length();
31+
} catch (IOException e) {
32+
throw new JsonException(e);
33+
}
34+
return String.format("stream.writeRaw(\"%s\", %s);", buffered, unescapedLen);
2135
}
2236
}
2337

src/main/java/com/jsoniter/output/JsonStream.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public void reset(OutputStream out) {
3535
}
3636

3737
public final void write(int b) throws IOException {
38-
if (count >= buf.length) {
38+
if (count == buf.length) {
3939
flushBuffer();
4040
}
4141
buf[count++] = (byte) b;
@@ -98,15 +98,16 @@ public final void writeVal(String val) throws IOException {
9898
if (val == null) {
9999
writeNull();
100100
} else {
101-
write('"');
102101
StreamImplString.writeString(this, val);
103-
write('"');
104102
}
105103
}
106104

107105
public final void writeRaw(String val) throws IOException {
106+
writeRaw(val, val.length());
107+
}
108+
109+
public final void writeRaw(String val, int remaining) throws IOException {
108110
int i = 0;
109-
int remaining = val.length();
110111
for(;;) {
111112
int available = buf.length - count;
112113
if (available < remaining) {

0 commit comments

Comments
 (0)