Skip to content

Commit

Permalink
vector write
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Jan 11, 2025
1 parent 245ce08 commit a9e4453
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public static void fastjson2_str() {
long millis = System.currentTimeMillis() - start;
System.out.println("ClientsWriteUTF8Bytes-fastjson2 millis : " + millis);
// zulu17.40.19 :
// zulu17.40.19_vec : 1139 1313 1307
// zulu17.40.19_vec : 1139 1313 1307 1206
}
}

Expand Down
29 changes: 26 additions & 3 deletions core/src/main/java/com/alibaba/fastjson2/JSONWriterUTF16.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import static com.alibaba.fastjson2.JSONFactory.*;
import static com.alibaba.fastjson2.JSONWriter.Feature.*;
import static com.alibaba.fastjson2.JSONWriterUTF8.containsEscaped;
import static com.alibaba.fastjson2.util.IOUtils.*;
import static com.alibaba.fastjson2.util.JDKUtils.*;
import static com.alibaba.fastjson2.util.TypeUtils.*;
Expand Down Expand Up @@ -60,6 +61,7 @@ class JSONWriterUTF16

protected char[] chars;
final CacheItem cacheItem;
protected final long byteVectorQuote;

JSONWriterUTF16(Context ctx) {
super(ctx, null, false, StandardCharsets.UTF_16);
Expand All @@ -70,6 +72,7 @@ class JSONWriterUTF16
chars = new char[8192];
}
this.chars = chars;
this.byteVectorQuote = this.useSingleQuote ? 0x2727_2727_2727_2727L : 0x2222_2222_2222_2222L;
}

public final void writeNull() {
Expand Down Expand Up @@ -271,13 +274,29 @@ public void writeStringLatin1(byte[] value) {
final char[] chars = this.chars;
chars[off++] = quote;

for (byte c : value) {
if (c == '\\' || c == quote || c < ' ') {
int i = 0;
final long vecQuote = this.byteVectorQuote;
final int upperBound = (value.length - i) & ~7;
for (; i < upperBound; i += 8) {
long vec64 = getLongLittleEndian(value, i);
if (containsEscaped(vec64, vecQuote)) {
escape = true;
break;
}
IOUtils.putLong(chars, off, expand(vec64));
IOUtils.putLong(chars, off + 4, expand(vec64 >>> 32));
off += 8;
}
if (!escape) {
for (; i < value.length; i++) {
byte c = value[i];
if (c == '\\' || c == quote || c < ' ') {
escape = true;
break;
}

chars[off++] = (char) c;
chars[off++] = (char) c;
}
}

if (!escape) {
Expand All @@ -290,6 +309,10 @@ public void writeStringLatin1(byte[] value) {
writeStringEscape(value);
}

static long expand(long i) {
return (i & 0xFFL) | ((i & 0xFF00L) << 8) | ((i & 0xFF0000L) << 16) | ((i & 0xFF000000L) << 24);
}

protected final void writeStringLatin1BrowserSecure(byte[] value) {
boolean escape = false;
int off = this.off;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ public void writeStringLatin1(byte[] value) {
writeStringEscaped(value);
}

private static boolean containsEscaped(long data, long quote) {
static boolean containsEscaped(long data, long quote) {
// c == quote || c == '\\' || c < ' '
/*
for (int i = 0; i < 8; ++i) {
Expand Down
4 changes: 4 additions & 0 deletions core/src/main/java/com/alibaba/fastjson2/util/IOUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -1395,6 +1395,10 @@ public static int writeInt32(final char[] buf, int pos, final int value) {
return pos + 6;
}

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

public static void putShort(byte[] buf, int pos, short v) {
UNSAFE.putShort(
buf,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ final class JSONWriterUTF16Vector
static final Vector<Byte> V_BYTE_64_SLASH = ByteVector.SPECIES_64.broadcast('\\');
static final Vector<Byte> V_BYTE_64_DOUBLE_QUOTE = ByteVector.SPECIES_64.broadcast('"');
static final Vector<Byte> V_BYTE_64_SINGLE_QUOTE = ByteVector.SPECIES_64.broadcast('\'');
static final Vector<Byte> V_BYTE_64_LT = ByteVector.SPECIES_64.broadcast('<');
static final Vector<Byte> V_BYTE_64_GT = ByteVector.SPECIES_64.broadcast('>');
static final Vector<Byte> V_BYTE_64_LB = ByteVector.SPECIES_64.broadcast('(');
static final Vector<Byte> V_BYTE_64_RB = ByteVector.SPECIES_64.broadcast(')');

static final Vector<Short> V_SHORT_128_SLASH = ShortVector.SPECIES_128.broadcast('\\');
static final Vector<Short> V_SHORT_128_DOUBLE_QUOTE = ShortVector.SPECIES_128.broadcast('"');
Expand Down

0 comments on commit a9e4453

Please sign in to comment.