Skip to content

Commit

Permalink
merge write quote2_colon
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Dec 31, 2024
1 parent 4849a2d commit 1fbd530
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
17 changes: 9 additions & 8 deletions core/src/main/java/com/alibaba/fastjson2/JSONWriterUTF16.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
class JSONWriterUTF16
extends JSONWriter {
static final char[] REF_PREF = "{\"$ref\":".toCharArray();
static final int QUOTE2_COLON, QUOTE_COLON;
static final int[] HEX256;
static {
int[] digits = new int[16 * 16];
Expand All @@ -47,6 +48,10 @@ class JSONWriterUTF16
}

HEX256 = digits;
char[] chars = new char[] {'\"', ':'};
QUOTE2_COLON = UNSAFE.getInt(chars, ARRAY_CHAR_BASE_OFFSET);
chars[0] = '\'';
QUOTE_COLON = UNSAFE.getInt(chars, ARRAY_CHAR_BASE_OFFSET);
}

protected char[] chars;
Expand Down Expand Up @@ -1682,8 +1687,7 @@ public final void writeName7Raw(long name) {
}

putLong(chars, off, name);
chars[off + 8] = quote;
chars[off + 9] = ':';
putIntUnaligned(chars, off + 8, useSingleQuote ? QUOTE_COLON : QUOTE2_COLON);
this.off = off + 10;
}

Expand All @@ -1707,8 +1711,7 @@ public final void writeName8Raw(long name) {

chars[off++] = quote;
putLong(chars, off, name);
chars[off + 8] = quote;
chars[off + 9] = ':';
putIntUnaligned(chars, off + 8, useSingleQuote ? QUOTE_COLON : QUOTE2_COLON);
this.off = off + 10;
}

Expand Down Expand Up @@ -1864,8 +1867,7 @@ public final void writeName15Raw(long name0, long name1) {
}

putLong(chars, off, name0, name1);
chars[off + 16] = quote;
chars[off + 17] = ':';
putIntUnaligned(chars, off + 16, useSingleQuote ? QUOTE_COLON : QUOTE2_COLON);
this.off = off + 18;
}

Expand All @@ -1889,8 +1891,7 @@ public final void writeName16Raw(long name0, long name1) {

chars[off++] = quote;
putLong(chars, off, name0, name1);
chars[off + 16] = quote;
chars[off + 17] = ':';
putIntUnaligned(chars, off + 16, useSingleQuote ? QUOTE_COLON : QUOTE2_COLON);
this.off = off + 18;
}

Expand Down
14 changes: 8 additions & 6 deletions core/src/main/java/com/alibaba/fastjson2/JSONWriterUTF8.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
class JSONWriterUTF8
extends JSONWriter {
static final byte[] REF_PREF = "{\"$ref\":".getBytes(StandardCharsets.ISO_8859_1);
static final short QUOTE2_COLON, QUOTE_COLON;
static final short[] HEX256;

static {
Expand All @@ -41,6 +42,10 @@ class JSONWriterUTF8
}

HEX256 = digits;
byte[] chars = new byte[] {'\"', ':'};
QUOTE2_COLON = UNSAFE.getShort(chars, ARRAY_BYTE_BASE_OFFSET);
chars[0] = '\'';
QUOTE_COLON = UNSAFE.getShort(chars, ARRAY_BYTE_BASE_OFFSET);
}

final CacheItem cacheItem;
Expand Down Expand Up @@ -1826,8 +1831,7 @@ public final void writeName8Raw(long name) {

bytes[off] = (byte) quote;
UNSAFE.putLong(bytes, ARRAY_BYTE_BASE_OFFSET + off + 1, name);
bytes[off + 9] = (byte) quote;
bytes[off + 10] = ':';
UNSAFE.putShort(bytes, ARRAY_BYTE_BASE_OFFSET + off + 9, useSingleQuote ? QUOTE_COLON : QUOTE2_COLON);
this.off = off + 11;
}

Expand Down Expand Up @@ -1983,8 +1987,7 @@ public final void writeName15Raw(long name0, long name1) {

UNSAFE.putLong(bytes, ARRAY_BYTE_BASE_OFFSET + off, name0);
UNSAFE.putLong(bytes, ARRAY_BYTE_BASE_OFFSET + off + 8, name1);
bytes[off + 16] = (byte) quote;
bytes[off + 17] = ':';
UNSAFE.putShort(bytes, ARRAY_BYTE_BASE_OFFSET + off + 16, useSingleQuote ? QUOTE_COLON : QUOTE2_COLON);
this.off = off + 18;
}

Expand All @@ -2008,8 +2011,7 @@ public final void writeName16Raw(long name0, long name1) {
bytes[off++] = (byte) quote;
UNSAFE.putLong(bytes, ARRAY_BYTE_BASE_OFFSET + off, name0);
UNSAFE.putLong(bytes, ARRAY_BYTE_BASE_OFFSET + off + 8, name1);
bytes[off + 16] = (byte) quote;
bytes[off + 17] = ':';
UNSAFE.putShort(bytes, ARRAY_BYTE_BASE_OFFSET + off + 16, useSingleQuote ? QUOTE_COLON : QUOTE2_COLON);
this.off = off + 18;
}

Expand Down
8 changes: 6 additions & 2 deletions core/src/main/java/com/alibaba/fastjson2/util/IOUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -1408,15 +1408,19 @@ public static void putInt(byte[] buf, int pos, int v) {
public static void putInt(char[] buf, int pos, int v) {
UNSAFE.putInt(
buf,
ARRAY_CHAR_BASE_OFFSET + (pos << 1),
ARRAY_CHAR_BASE_OFFSET + ((long) pos << 1),
BIG_ENDIAN ? Integer.reverseBytes(v) : v
);
}

public static void putIntUnaligned(char[] buf, int pos, int v) {
UNSAFE.putInt(buf, ARRAY_CHAR_BASE_OFFSET + ((long) pos << 1), v);
}

public static void putLong(char[] buf, int pos, long v) {
UNSAFE.putLong(
buf,
ARRAY_CHAR_BASE_OFFSET + (pos << 1),
ARRAY_CHAR_BASE_OFFSET + ((long) pos << 1),
BIG_ENDIAN ? Long.reverseBytes(v) : v
);
}
Expand Down

0 comments on commit 1fbd530

Please sign in to comment.