optimize JSONReaderUTF16::readString

This commit is contained in:
wenshao 2025-02-26 04:37:12 +08:00
parent 0e36eaa183
commit e8e9acb50d
4 changed files with 51 additions and 9 deletions

View File

@ -22,13 +22,17 @@ import java.util.concurrent.TimeUnit;
public class UsersParseUTF8Bytes {
static byte[] utf8Bytes;
static char[] chars;
static String str;
static final ObjectMapper mapper = new ObjectMapper();
static final Gson gson = new Gson();
static final DslJson<Object> dslJson = new DslJson<>(Settings.withRuntime().includeServiceLoader());
static {
try {
InputStream is = UsersParseUTF8Bytes.class.getClassLoader().getResourceAsStream("data/jjb/user.json");
utf8Bytes = IOUtils.toString(is, "UTF-8").getBytes(StandardCharsets.UTF_8);
str = IOUtils.toString(is, "UTF-8");
utf8Bytes = str.getBytes(StandardCharsets.UTF_8);
chars = str.toCharArray();
} catch (Throwable ex) {
ex.printStackTrace();
}
@ -41,6 +45,16 @@ public class UsersParseUTF8Bytes {
// zulu17.40.19_vec : 338
}
// @Benchmark
public void fastjson2_str(Blackhole bh) {
bh.consume(JSON.parseObject(str, Users.class));
}
// @Benchmark
public void fastjson2_chars(Blackhole bh) {
bh.consume(JSON.parseObject(chars, Users.class));
}
@Benchmark
public void wast(Blackhole bh) {
// zulu17.40.19 : 3221

View File

@ -13,9 +13,31 @@ public class UsersParseUTF8BytesTest {
}
long millis = System.currentTimeMillis() - start;
System.out.println("fastjson2 millis : " + millis);
// zulu17.40.19 : 2198 2189 1633 1576
// oracle-jdk-17.0.6 :
// oracle-jdk-17.0.6_vec :
// zulu17.40.19 : 2198 2189 1633 1576 1496
}
}
public static void fastjson2_str() {
for (int j = 0; j < 5; j++) {
long start = System.currentTimeMillis();
for (int i = 0; i < 1000 * 1000; ++i) {
benchmark.fastjson2_str(BH);
}
long millis = System.currentTimeMillis() - start;
System.out.println("fastjson2 millis : " + millis);
// zulu17.40.19 : 1479
}
}
public static void fastjson2_chars() {
for (int j = 0; j < 5; j++) {
long start = System.currentTimeMillis();
for (int i = 0; i < 1000 * 1000; ++i) {
benchmark.fastjson2_chars(BH);
}
long millis = System.currentTimeMillis() - start;
System.out.println("fastjson2 millis : " + millis);
// zulu17.40.19 :
}
}
@ -55,7 +77,10 @@ public class UsersParseUTF8BytesTest {
}
public static void main(String[] args) throws Exception {
wast();
// fastjson2();
fastjson2_str();
// fastjson2_chars();
// wast();
// dsljson();
// jackson();
}

View File

@ -4734,6 +4734,9 @@ public abstract class JSONReader
if ((features & MASK_TRIM_STRING) != 0) {
str = str.trim();
}
return (features & MASK_EMPTY_STRING_AS_NULL) != 0 && str.isEmpty() ? null : str;
if ((features & MASK_EMPTY_STRING_AS_NULL) != 0 && str.isEmpty()) {
return null;
}
return str;
}
}

View File

@ -3023,8 +3023,8 @@ final class JSONReaderUTF16
@Override
public String readString() {
final char[] chars = this.chars;
if (ch == '"' || ch == '\'') {
final char quote = ch;
final char quote = ch;
if (quote == '"' || quote == '\'') {
final long byteVectorQuote = quote == '\'' ? 0x2727_2727_2727_2727L : 0x2222_2222_2222_2222L;
int offset = this.offset;
@ -3050,7 +3050,7 @@ final class JSONReaderUTF16
for (; ; ++i) {
if (offset >= end) {
throw new JSONException(info("invalid escape character EOI"));
throw error("invalid escape character EOI");
}
char c = chars[offset];
if (c == '\\') {