Skip to content

Commit

Permalink
optimize JSONReader
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Feb 12, 2025
1 parent 436f7fa commit fe32ca1
Show file tree
Hide file tree
Showing 11 changed files with 221 additions and 413 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ public static void fastjson2() {
}
long millis = System.currentTimeMillis() - start;
System.out.println("fastjson2 millis : " + millis);
// zulu8.70.0.23 : 5569 5630 4435 4199 3851
// zulu11.64.19 : 5022 5033 3754
// zulu17.42.19 : 5377 5193 5222 5154 5116 5083 4987 4079
// zulu8.70.0.23 : 5569 5630 4435 4199 3851 3791
// zulu11.64.19 : 5022 5033 3754 3579
// zulu17.42.19 : 5377 5193 5222 5154 5116 5083 4987 4079 3751
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,28 @@ public class EishayParseTreeStringTest {
static final Blackhole BH = new Blackhole("Today's password is swordfish. I understand instantiating Blackholes directly is dangerous.");
static final EishayParseTreeString benchmark = new EishayParseTreeString();

public static void fastjson2_test() {
for (int i = 0; i < 10; i++) {
fastjson2();
}
}

public static void fastjson2() {
long start = System.currentTimeMillis();
for (int i = 0; i < 1000 * 1000; ++i) {
benchmark.fastjson2(BH);
for (int j = 0; j < 10; j++) {
long start = System.currentTimeMillis();
for (int i = 0; i < 1000 * 1000; ++i) {
benchmark.fastjson2(BH);
}
long millis = System.currentTimeMillis() - start;
System.out.println("fastjson2 millis : " + millis);
// zulu8.62.0.19 : 666 765 727 725
// zulu11.52.13 : 821 688 667
// zulu17.32.13 : 601 666
// zulu18.28.13 : 598 667
// zulu19.0.75 : 673
// corretto-8 :
// corretto-11 :
// corretto-17 :
// corretto-18 :
// oracle-jdk-17.0.4 :
// oracle-jdk-18.0.2 :
// ibm-aarch64_mac_11.0.15_10 : 1240
// ibm-aarch64_mac_17.0.3_7 : 1311
}
long millis = System.currentTimeMillis() - start;
System.out.println("fastjson2 millis : " + millis);
// zulu8.62.0.19 : 666 765
// zulu11.52.13 : 821 688 667
// zulu17.32.13 : 601 666
// zulu18.28.13 : 598 667
// zulu19.0.75 : 673
// corretto-8 :
// corretto-11 :
// corretto-17 :
// corretto-18 :
// oracle-jdk-17.0.4 :
// oracle-jdk-18.0.2 :
// ibm-aarch64_mac_11.0.15_10 : 1240
// ibm-aarch64_mac_17.0.3_7 : 1311
}

public static void jackson_test() throws Exception {
Expand Down Expand Up @@ -113,7 +109,7 @@ public static void gson() throws Exception {
}

public static void main(String[] args) throws Exception {
fastjson2_test();
fastjson2();
// gson_test();
// jackson_test();
// wastjson_test();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static void fastjson2() {
System.out.println("fastjson2 millis : " + millis);
// zulu8.62.0.19 : 703 746 710 706 700 682 717 698 526 500 474 445 425
// zulu11.52.13 : 579 565 552 541 554 553 554 538 420 424 434 370
// zulu17.40.19 : 600 604 597 593 578 567 447 420 380
// zulu17.40.19 : 600 604 597 593 578 567 447 420 380 379
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static void fastjson2() {
}
long millis = System.currentTimeMillis() - start;
System.out.println("fastjson2 millis : " + millis);
// zulu17.40.19 : 2417 2236 2139 1985
// zulu17.40.19 : 2417 2236 2139 1985 2174
// oracle-jdk-17.0.6 :
// oracle-jdk-17.0.6_vec :
// oracle-jdk-17.0.6_reflect : 3566 3513 3476
Expand Down
31 changes: 24 additions & 7 deletions core/src/main/java/com/alibaba/fastjson2/JSONReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ public abstract class JSONReader
static final char EOI = 0x1A;
static final long SPACE = (1L << ' ') | (1L << '\n') | (1L << '\r') | (1L << '\f') | (1L << '\t') | (1L << '\b');

static final boolean[] INT_VALUE_END = new boolean[256];
static {
Arrays.fill(INT_VALUE_END, true);
char[] chars = {'.', 'e', 'E', 't', 'f', 'n', '{', '[', '0', '1', '2', '2', '3', '4', '5', '6', '7', '8', '9'};
for (char ch : chars) {
INT_VALUE_END[ch] = false;
}
}

protected final Context context;
public final boolean jsonb;
public final boolean utf8;
Expand Down Expand Up @@ -800,6 +809,16 @@ public short readInt16Value() {

public abstract Integer readInt32();

protected final int readInt32ValueOverflow() {
readNumber0();
return getInt32Value();
}

protected final long readInt64ValueOverflow() {
readNumber0();
return getInt64Value();
}

public final int getInt32Value() {
switch (valueType) {
case JSON_TYPE_INT8:
Expand Down Expand Up @@ -3339,7 +3358,7 @@ public static JSONReader of(byte[] bytes, int offset, int length, Charset charse
}

if (charset == StandardCharsets.US_ASCII || charset == StandardCharsets.ISO_8859_1) {
return JSONReaderASCII.of(context, null, bytes, offset, length);
return new JSONReaderASCII(context, null, bytes, offset, length);
}

throw new JSONException("not support charset " + charset);
Expand All @@ -3350,9 +3369,7 @@ private static JSONReader ofUTF16(byte[] bytes, int offset, int length, Context
}

private static JSONReader ofUTF16(String str, char[] chars, int offset, int length, Context ctx) {
return (str != null && str.indexOf('\\') == -1) || IOUtils.isNonSlashASCII(chars, offset, length)
? new JSONReaderUTF16NonSlash(ctx, str, chars, offset, length)
: new JSONReaderUTF16(ctx, str, chars, offset, length);
return new JSONReaderUTF16(ctx, str, chars, offset, length);
}

public static JSONReader of(byte[] bytes, int offset, int length, Charset charset, Context context) {
Expand All @@ -3365,7 +3382,7 @@ public static JSONReader of(byte[] bytes, int offset, int length, Charset charse
}

if (charset == StandardCharsets.US_ASCII || charset == StandardCharsets.ISO_8859_1) {
return JSONReaderASCII.of(context, null, bytes, offset, length);
return new JSONReaderASCII(context, null, bytes, offset, length);
}

throw new JSONException("not support charset " + charset);
Expand Down Expand Up @@ -3470,7 +3487,7 @@ public static JSONReader of(String str, Context context) {
int coder = STRING_CODER.applyAsInt(str);
if (coder == LATIN1) {
byte[] bytes = STRING_VALUE.apply(str);
return JSONReaderASCII.of(context, str, bytes, 0, bytes.length);
return new JSONReaderASCII(context, str, bytes, 0, bytes.length);
}
} catch (Exception e) {
throw new JSONException("unsafe get String.coder error");
Expand Down Expand Up @@ -3503,7 +3520,7 @@ public static JSONReader of(String str, int offset, int length, Context context)
int coder = STRING_CODER.applyAsInt(str);
if (coder == LATIN1) {
byte[] bytes = STRING_VALUE.apply(str);
return JSONReaderASCII.of(context, str, bytes, offset, length);
return new JSONReaderASCII(context, str, bytes, offset, length);
}
} catch (Exception e) {
throw new JSONException("unsafe get String.coder error");
Expand Down
8 changes: 2 additions & 6 deletions core/src/main/java/com/alibaba/fastjson2/JSONReaderASCII.java
Original file line number Diff line number Diff line change
Expand Up @@ -1473,6 +1473,8 @@ public String readString() {
char[] buf = new char[valueLength];
offset = readEscaped(bytes, start, quote, buf);
str = new String(buf);
} else if (STRING_CREATOR_JDK11 != null) {
str = STRING_CREATOR_JDK11.apply(Arrays.copyOfRange(bytes, start, offset), LATIN1);
} else {
str = new String(bytes, start, offset - start, StandardCharsets.ISO_8859_1);
}
Expand Down Expand Up @@ -1552,10 +1554,4 @@ protected final int readEscaped(byte[] bytes, int offset, byte quote, char[] buf
}
return offset;
}

public static JSONReaderASCII of(Context ctx, String str, byte[] bytes, int offset, int length) {
return IOUtils.isNonSlashASCII(bytes, offset, length)
? new JSONReaderASCIINonSlash(ctx, str, bytes, offset, length)
: new JSONReaderASCII(ctx, str, bytes, offset, length);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import com.alibaba.fastjson2.util.IOUtils;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;

import static com.alibaba.fastjson2.util.JDKUtils.LATIN1;
import static com.alibaba.fastjson2.util.JDKUtils.STRING_CREATOR_JDK11;

final class JSONReaderASCIINonSlash
extends JSONReaderASCII {
Expand All @@ -21,7 +25,12 @@ public String readString() {
throw new JSONException("invalid escape character EOI");
}

String str = new String(bytes, start, offset - start, StandardCharsets.ISO_8859_1);
String str;
if (STRING_CREATOR_JDK11 != null) {
str = STRING_CREATOR_JDK11.apply(Arrays.copyOfRange(bytes, start, offset), LATIN1);
} else {
str = new String(bytes, start, offset - start, StandardCharsets.ISO_8859_1);
}
long features = context.features;
if ((features & Feature.TrimString.mask) != 0) {
str = str.trim();
Expand Down
Loading

0 comments on commit fe32ca1

Please sign in to comment.