diff --git a/core/src/main/java/com/alibaba/fastjson2/JSONWriterUTF16.java b/core/src/main/java/com/alibaba/fastjson2/JSONWriterUTF16.java index 45a868560..7f5912d3a 100644 --- a/core/src/main/java/com/alibaba/fastjson2/JSONWriterUTF16.java +++ b/core/src/main/java/com/alibaba/fastjson2/JSONWriterUTF16.java @@ -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]; @@ -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; @@ -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; } @@ -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; } @@ -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; } @@ -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; } diff --git a/core/src/main/java/com/alibaba/fastjson2/JSONWriterUTF8.java b/core/src/main/java/com/alibaba/fastjson2/JSONWriterUTF8.java index ae1c12023..09333c2f8 100644 --- a/core/src/main/java/com/alibaba/fastjson2/JSONWriterUTF8.java +++ b/core/src/main/java/com/alibaba/fastjson2/JSONWriterUTF8.java @@ -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 { @@ -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; @@ -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; } @@ -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; } @@ -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; } diff --git a/core/src/main/java/com/alibaba/fastjson2/util/IOUtils.java b/core/src/main/java/com/alibaba/fastjson2/util/IOUtils.java index fe008767c..a886f5b4b 100644 --- a/core/src/main/java/com/alibaba/fastjson2/util/IOUtils.java +++ b/core/src/main/java/com/alibaba/fastjson2/util/IOUtils.java @@ -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 ); }