Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optim 202501 #3256

Merged
merged 11 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public static void jackson() throws Exception {
}

public static void main(String[] args) throws Exception {
wast();
// fastjson2();
// wast();
fastjson2();
// dsljson();
// jackson();
}
Expand Down
4 changes: 4 additions & 0 deletions core/src/main/java/com/alibaba/fastjson2/JSONReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -4650,6 +4650,10 @@ final JSONException valueError() {
return new JSONException(info("illegal value"));
}

final JSONException error(String message) {
return new JSONException(info(message));
}

final JSONException error(int offset, int ch) {
throw new JSONValidException("error, offset " + offset + ", char " + (char) ch);
}
Expand Down
173 changes: 75 additions & 98 deletions core/src/main/java/com/alibaba/fastjson2/JSONReaderASCII.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.alibaba.fastjson2;

import com.alibaba.fastjson2.util.Fnv;
import com.alibaba.fastjson2.util.IOUtils;
import com.alibaba.fastjson2.util.JDKUtils;
import com.alibaba.fastjson2.util.TypeUtils;

Expand Down Expand Up @@ -1424,115 +1425,44 @@ public String readString() {
int valueLength;
boolean valueEscape = false;

_for:
{
int i = 0;
byte c0 = 0, c1 = 0, c2 = 0, c3;

// vector optimize
boolean quoted = false;
int upperBound = offset + ((end - offset) & ~3);
while (offset < upperBound) {
c0 = bytes[offset];
c1 = bytes[offset + 1];
c2 = bytes[offset + 2];
c3 = bytes[offset + 3];
if (c0 == slash || c1 == slash || c2 == slash || c3 == slash) {
break;
}
if (c0 == quote || c1 == quote || c2 == quote || c3 == quote) {
quoted = true;
break;
}
offset += 4;
i += 4;
}
int index = IOUtils.indexOfChar(bytes, quote, offset, end);
if (index == -1) {
throw error("invalid escape character EOI");
}
int slashIndex = IOUtils.indexOfChar(bytes, '\\', offset, index);
if (slashIndex == -1) {
valueLength = index - offset;
offset = index;
} else {
valueEscape = true;
valueLength = slashIndex - offset;
offset = slashIndex;

if (quoted) {
if (c0 == quote) {
// skip
} else if (c1 == quote) {
offset++;
i++;
} else if (c2 == quote) {
offset += 2;
i += 2;
} else {
offset += 3;
i += 3;
for (;;) {
if (offset >= end) {
throw error("invalid escape character EOI");
}
valueLength = i;
} else {
for (; ; ++i) {
if (offset >= end) {
throw new JSONException("invalid escape character EOI");
}

byte c = bytes[offset];
if (c == slash) {
valueEscape = true;
c = bytes[offset + 1];
offset += (c == 'u' ? 6 : (c == 'x' ? 4 : 2));
continue;
}
byte c = bytes[offset];
if (c == slash) {
valueLength++;
c = bytes[offset + 1];
offset += (c == 'u' ? 6 : (c == 'x' ? 4 : 2));
continue;
}

if (c == quote) {
valueLength = i;
break _for;
}
offset++;
if (c == quote) {
break;
}
offset++;
valueLength++;
}
}

String str;
if (valueEscape) {
char[] buf = new char[valueLength];
offset = start;
for (int i = 0; ; ++i) {
char c = (char) (bytes[offset] & 0xff);
if (c == '\\') {
c = (char) bytes[++offset];
switch (c) {
case 'u': {
c = char4(bytes[offset + 1], bytes[offset + 2], bytes[offset + 3], bytes[offset + 4]);
offset += 4;
break;
}
case 'x': {
c = char2(bytes[offset + 1], bytes[offset + 2]);
offset += 2;
break;
}
case '\\':
case '"':
break;
case 'b':
c = '\b';
break;
case 't':
c = '\t';
break;
case 'n':
c = '\n';
break;
case 'f':
c = '\f';
break;
case 'r':
c = '\r';
break;
default:
c = char1(c);
break;
}
} else if (c == quote) {
break;
}
buf[i] = c;
offset++;
}

offset = readEscaped(bytes, start, quote, buf);
str = new String(buf);
} else {
if (this.str != null) {
Expand Down Expand Up @@ -1573,4 +1503,51 @@ public String readString() {

return readStringNotMatch();
}

private int readEscaped(byte[] bytes, int offset, byte quote, char[] buf) {
for (int i = 0; ; ++i) {
char c = (char) (bytes[offset] & 0xff);
if (c == '\\') {
c = (char) bytes[++offset];
switch (c) {
case 'u': {
c = char4(bytes[offset + 1], bytes[offset + 2], bytes[offset + 3], bytes[offset + 4]);
offset += 4;
break;
}
case 'x': {
c = char2(bytes[offset + 1], bytes[offset + 2]);
offset += 2;
break;
}
case '\\':
case '"':
break;
case 'b':
c = '\b';
break;
case 't':
c = '\t';
break;
case 'n':
c = '\n';
break;
case 'f':
c = '\f';
break;
case 'r':
c = '\r';
break;
default:
c = char1(c);
break;
}
} else if (c == quote) {
break;
}
buf[i] = c;
offset++;
}
return offset;
}
}
83 changes: 9 additions & 74 deletions core/src/main/java/com/alibaba/fastjson2/JSONReaderUTF16.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.alibaba.fastjson2.util.DateUtils;
import com.alibaba.fastjson2.util.Fnv;
import com.alibaba.fastjson2.util.IOUtils;
import com.alibaba.fastjson2.util.TypeUtils;

import java.io.Closeable;
Expand Down Expand Up @@ -5006,79 +5007,13 @@ public final OffsetDateTime readOffsetDateTime() {
&& chars[offset + 13] == ':'
&& chars[offset + 16] == ':'
) {
char y0 = chars[offset];
char y1 = chars[offset + 1];
char y2 = chars[offset + 2];
char y3 = chars[offset + 3];
char m0 = chars[offset + 5];
char m1 = chars[offset + 6];
char d0 = chars[offset + 8];
char d1 = chars[offset + 9];
char h0 = chars[offset + 11];
char h1 = chars[offset + 12];
char i0 = chars[offset + 14];
char i1 = chars[offset + 15];
char s0 = chars[offset + 17];
char s1 = chars[offset + 18];

int year;
int month;
if (y0 >= '0' && y0 <= '9'
&& y1 >= '0' && y1 <= '9'
&& y2 >= '0' && y2 <= '9'
&& y3 >= '0' && y3 <= '9'
) {
year = (y0 - '0') * 1000 + (y1 - '0') * 100 + (y2 - '0') * 10 + (y3 - '0');
} else {
ZonedDateTime zdt = readZonedDateTime();
return zdt == null ? null : zdt.toOffsetDateTime();
}

if (m0 >= '0' && m0 <= '9'
&& m1 >= '0' && m1 <= '9'
) {
month = (m0 - '0') * 10 + (m1 - '0');
} else {
ZonedDateTime zdt = readZonedDateTime();
return zdt == null ? null : zdt.toOffsetDateTime();
}

int dom;
if (d0 >= '0' && d0 <= '9'
&& d1 >= '0' && d1 <= '9'
) {
dom = (d0 - '0') * 10 + (d1 - '0');
} else {
ZonedDateTime zdt = readZonedDateTime();
return zdt == null ? null : zdt.toOffsetDateTime();
}

int hour;
if (h0 >= '0' && h0 <= '9'
&& h1 >= '0' && h1 <= '9'
) {
hour = (h0 - '0') * 10 + (h1 - '0');
} else {
ZonedDateTime zdt = readZonedDateTime();
return zdt == null ? null : zdt.toOffsetDateTime();
}

int minute;
if (i0 >= '0' && i0 <= '9'
&& i1 >= '0' && i1 <= '9'
) {
minute = (i0 - '0') * 10 + (i1 - '0');
} else {
ZonedDateTime zdt = readZonedDateTime();
return zdt == null ? null : zdt.toOffsetDateTime();
}

int second;
if (s0 >= '0' && s0 <= '9'
&& s1 >= '0' && s1 <= '9'
) {
second = (s0 - '0') * 10 + (s1 - '0');
} else {
int year = IOUtils.digit4(chars, offset);
int month = IOUtils.digit2(chars, offset + 5);
int dom = IOUtils.digit2(chars, offset + 8);
int hour = IOUtils.digit2(chars, offset + 11);
int minute = IOUtils.digit2(chars, offset + 14);
int second = IOUtils.digit2(chars, offset + 17);
if ((year | month | dom | minute | second) < 0) {
ZonedDateTime zdt = readZonedDateTime();
return zdt == null ? null : zdt.toOffsetDateTime();
}
Expand Down Expand Up @@ -5108,7 +5043,7 @@ public final OffsetDateTime readOffsetDateTime() {
OffsetDateTime oft = OffsetDateTime.of(ldt, ZoneOffset.UTC);
this.offset += len;
next();
if (comma = (ch == ',')) {
if (comma = (this.ch == ',')) {
next();
}
return oft;
Expand Down
Loading
Loading