Skip to content

Commit

Permalink
merge write ref
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Dec 31, 2024
1 parent a3bd0ef commit c0b9549
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 17 deletions.
26 changes: 18 additions & 8 deletions core/src/main/java/com/alibaba/fastjson2/JSONWriterUTF16.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

class JSONWriterUTF16
extends JSONWriter {
static final char[] REF_PREF = "{\"$ref\":".toCharArray();
static final long REF_0, REF_1;
static final int QUOTE2_COLON, QUOTE_COLON;
static final int[] HEX256;
static {
Expand All @@ -48,10 +48,14 @@ 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);

// char[] chars = new char[] {'\"', ':'};
char[] chars = {'{', '"', '$', 'r', 'e', 'f', '"', ':'};
REF_0 = UNSAFE.getLong(chars, ARRAY_CHAR_BASE_OFFSET);
REF_1 = UNSAFE.getLong(chars, ARRAY_CHAR_BASE_OFFSET + 8);
QUOTE2_COLON = UNSAFE.getInt(chars, ARRAY_CHAR_BASE_OFFSET + 12);
chars[6] = '\'';
QUOTE_COLON = UNSAFE.getInt(chars, ARRAY_CHAR_BASE_OFFSET + 12);
}

protected char[] chars;
Expand Down Expand Up @@ -1160,11 +1164,17 @@ public final void writeString(String[] strings) {
@Override
public final void writeReference(String path) {
this.lastReference = path;

writeRaw(REF_PREF, 0, REF_PREF.length);
writeString(path);
int off = this.off;
char[] chars = this.chars;
if (off + 8 > chars.length) {
chars = grow(off + 8);
}
long address = ARRAY_BYTE_BASE_OFFSET + ((long) off << 1);
UNSAFE.putLong(chars, address, REF_0);
UNSAFE.putLong(chars, address + 8, REF_1);
this.off = off + 8;
writeString(path);
off = this.off;
if (off == chars.length) {
chars = grow(off + 1);
}
Expand Down
25 changes: 16 additions & 9 deletions core/src/main/java/com/alibaba/fastjson2/JSONWriterUTF8.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

class JSONWriterUTF8
extends JSONWriter {
static final byte[] REF_PREF = "{\"$ref\":".getBytes(StandardCharsets.ISO_8859_1);
static final long REF;
static final short QUOTE2_COLON, QUOTE_COLON;
static final short[] HEX256;

Expand All @@ -42,10 +42,12 @@ 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);

byte[] chars = {'{', '"', '$', 'r', 'e', 'f', '"', ':'};
REF = UNSAFE.getLong(chars, ARRAY_CHAR_BASE_OFFSET);
QUOTE2_COLON = UNSAFE.getShort(chars, ARRAY_CHAR_BASE_OFFSET + 6);
chars[6] = '\'';
QUOTE_COLON = UNSAFE.getShort(chars, ARRAY_CHAR_BASE_OFFSET + 6);
}

final CacheItem cacheItem;
Expand Down Expand Up @@ -75,12 +77,17 @@ public final void writeNull() {
@Override
public final void writeReference(String path) {
this.lastReference = path;

writeRaw(REF_PREF);
writeString(path);
int off = this.off;
byte[] bytes = this.bytes;
if (off + 8 > bytes.length) {
bytes = grow(off + 8);
}
UNSAFE.putLong(bytes, ARRAY_BYTE_BASE_OFFSET + off, REF);
this.off = off + 8;
writeString(path);
off = this.off;
if (off == bytes.length) {
grow(off + 1);
bytes = grow(off + 1);
}
bytes[off] = '}';
this.off = off + 1;
Expand Down

0 comments on commit c0b9549

Please sign in to comment.