Skip to content

Commit

Permalink
refactor (#3282)
Browse files Browse the repository at this point in the history
* refactor

* add testcase

* refactor

* checkstyle

* refactor
  • Loading branch information
wenshao authored Jan 15, 2025
1 parent e6f17d2 commit 41662c7
Show file tree
Hide file tree
Showing 9 changed files with 249 additions and 231 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

public class CSVBankTest {
static final CSVBank benchmark = new CSVBank();
static final int LOOP = 1000;
static final int LOOP = 10000;

public static void fastjson2() {
for (int j = 0; j < 5; j++) {
Expand All @@ -16,7 +16,7 @@ public static void fastjson2() {
System.out.println("fastjson2 millis : " + millis);
// zulu8.68.0.21 : 213
// zulu11.62.17 : 148
// zulu17.40.19 : 150
// zulu17.40.19 : 150 1746 1639
}
}

Expand Down Expand Up @@ -49,8 +49,8 @@ public static void cainiao() throws Exception {
}

public static void main(String[] args) throws Exception {
// fastjson2();
univocity();
fastjson2();
// univocity();
// cainiao();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,7 @@ public void writeBool(boolean value) {
if ((context.features & WriteBooleanAsNumber.mask) != 0) {
chars[off++] = value ? '1' : '0';
} else {
if (value) {
IOUtils.putTrue(chars, off);
off += 4;
} else {
IOUtils.putFalse(chars, off);
off += 5;
}
off = IOUtils.putBoolean(chars, off, value);
}
this.off = off;
}
Expand Down
8 changes: 1 addition & 7 deletions core/src/main/java/com/alibaba/fastjson2/JSONWriterUTF8.java
Original file line number Diff line number Diff line change
Expand Up @@ -2927,13 +2927,7 @@ public void writeBool(boolean value) {
if ((context.features & WriteBooleanAsNumber.mask) != 0) {
bytes[off++] = (byte) (value ? '1' : '0');
} else {
if (value) {
IOUtils.putTrue(bytes, off);
off += 4;
} else {
IOUtils.putFalse(bytes, off);
off += 5;
}
off = IOUtils.putBoolean(bytes, off, value);
}
this.off = off;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.function.Consumer;
import java.util.function.Function;

import static com.alibaba.fastjson2.support.csv.CSVReaderUTF8.containsQuoteOrLineSeparator;
import static com.alibaba.fastjson2.util.DateUtils.DEFAULT_ZONE_ID;

final class CSVReaderUTF16<T>
Expand Down Expand Up @@ -97,19 +98,11 @@ protected boolean seekLine() throws IOException {
for (int k = 0; k < 3; ++k) {
lineTerminated = false;

for (int i = off; i < end; i++) {
if (i + 4 < end) {
char b0 = buf[i];
char b1 = buf[i + 1];
char b2 = buf[i + 2];
char b3 = buf[i + 3];
if (b0 > '"' && b1 > '"' && b2 > '"' && b3 > '"') {
lineSize += 4;
i += 3;
continue;
}
}

int i = off, end = this.end;
while (i + 4 < end && !containsQuoteOrLineSeparator(IOUtils.getLongUnaligned(buf, i))) {
i += 4;
}
for (; i < end; i++) {
char ch = buf[i];
if (ch == '"') {
lineSize++;
Expand Down Expand Up @@ -194,6 +187,7 @@ protected boolean seekLine() throws IOException {
}
} else {
end += cnt;
this.end = end;
continue;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,26 @@ final class CSVReaderUTF8<T>
this.valueConsumer = valueConsumer;
}

static boolean containsQuoteOrLineSeparator(long v) {
/*
for (int i = 0; i < 8; ++i) {
byte c = (byte) v;
if (c == '"' || c == '\n' || c == '\r') {
return true;
}
v >>>= 8;
}
return false;
*/
long x22 = v ^ 0x2222222222222222L; // " -> 0x22
long x0a = v ^ 0x0A0A0A0A0A0A0A0AL; // \n -> 0x0a
long x0d = v ^ 0x0D0D0D0D0D0D0D0DL; // \r -> 0x0d
x22 = (x22 - 0x0101010101010101L) & ~x22;
x0a = (x0a - 0x0101010101010101L) & ~x0a;
x0d = (x0d - 0x0101010101010101L) & ~x0d;
return ((x22 | x0a | x0d) & 0x8080808080808080L) != 0;
}

protected boolean seekLine() throws IOException {
byte[] buf = this.buf;
int off = this.off;
Expand All @@ -100,20 +120,21 @@ protected boolean seekLine() throws IOException {
}
this.end = cnt;

if (end > 3) {
// UTF8-BOM EF BB BF
if (buf[0] == -17 && buf[1] == -69 && buf[2] == -65) {
off = 3;
lineNextStart = off;
}
if (end > 4 && IOUtils.isUTF8BOM(buf, 0)) {
off = 3;
lineNextStart = off;
}
}
}

for (int k = 0; k < 3; ++k) {
lineTerminated = false;

for (int i = off; i < end; i++) {
int i = off, end = this.end;
while (i + 8 < end && !containsQuoteOrLineSeparator(IOUtils.getLongUnaligned(buf, i))) {
i += 8;
}
for (; i < end; i++) {
byte ch = buf[i];
if (ch == '"') {
lineSize++;
Expand Down Expand Up @@ -198,6 +219,7 @@ protected boolean seekLine() throws IOException {
}
} else {
end += cnt;
this.end = end;
continue;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@
import java.time.LocalDateTime;
import java.time.ZoneId;

import static com.alibaba.fastjson2.util.IOUtils.*;

final class CSVWriterUTF16
extends CSVWriter {
final Writer out;
final char[] chars;
private static final int DOUBLE_QUOTE_2_UTF16 = '"' | ('"' << 16);

CSVWriterUTF16(
Writer out,
Expand Down Expand Up @@ -52,17 +51,9 @@ public void writeLine() {
chars[off++] = '\n';
}

public void writeBoolean(boolean booleanValue) {
int size = booleanValue ? 4 : 5;
checkCapacity(size);
char[] chars = this.chars;
int off = this.off;
if (booleanValue) {
IOUtils.putTrue(chars, off);
} else {
IOUtils.putFalse(chars, off);
}
this.off = off + size;
public void writeBoolean(boolean v) {
checkCapacity(5);
this.off = IOUtils.putBoolean(chars, off, v);
}

public void writeInt64(long longValue) {
Expand All @@ -88,11 +79,7 @@ public void writeDateTime19(
int off = this.off;
off = IOUtils.writeLocalDate(chars, off, year, month, dayOfMonth);
chars[off] = ' ';
writeDigitPair(chars, off + 1, hour);
chars[off + 3] = ':';
writeDigitPair(chars, off + 4, minute);
chars[off + 6] = ':';
writeDigitPair(chars, off + 7, second);
IOUtils.writeLocalTime(chars, off + 1, hour, minute, second);
this.off = off + 9;
}

Expand Down Expand Up @@ -153,8 +140,7 @@ public void writeString(final String str) {
for (int i = 0; i < len; ) {
char ch = str.charAt(i++);
if (ch == '"') {
chars[off] = '"';
chars[off + 1] = '"';
IOUtils.putIntUnaligned(chars, off, DOUBLE_QUOTE_2_UTF16);
off += 2;
} else {
chars[off++] = ch;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
import java.time.LocalDateTime;
import java.time.ZoneId;

import static com.alibaba.fastjson2.util.IOUtils.*;

final class CSVWriterUTF8
extends CSVWriter {
private static final short DOUBLE_QUOTE_2_LATIN1 = 0x22 | (0x22 << 8);

final OutputStream out;
final Charset charset;
final byte[] bytes;
Expand Down Expand Up @@ -55,28 +55,18 @@ public void writeLine() {
bytes[off++] = '\n';
}

public void writeBoolean(boolean booleanValue) {
int size = booleanValue ? 4 : 5;
checkCapacity(size);
byte[] chars = this.bytes;
int off = this.off;
if (booleanValue) {
IOUtils.putTrue(chars, off);
} else {
IOUtils.putFalse(chars, off);
}
this.off = off + size;
public void writeBoolean(boolean v) {
checkCapacity(5);
this.off = IOUtils.putBoolean(this.bytes, off, v);
}

public void writeInt64(long longValue) {
checkCapacity(20); // -9223372036854775808

off = IOUtils.writeInt64(bytes, off, longValue);
}

public void writeDateYYYMMDD10(int year, int month, int dayOfMonth) {
checkCapacity(10);

off = IOUtils.writeLocalDate(bytes, off, year, month, dayOfMonth);
}

Expand All @@ -94,11 +84,7 @@ public void writeDateTime19(
int off = this.off;
off = IOUtils.writeLocalDate(bytes, off, year, month, dayOfMonth);
bytes[off] = ' ';
writeDigitPair(bytes, off + 1, hour);
bytes[off + 3] = ':';
writeDigitPair(bytes, off + 4, minute);
bytes[off + 6] = ':';
writeDigitPair(bytes, off + 7, second);
IOUtils.writeLocalTime(bytes, off + 1, hour, minute, second);
this.off = off + 9;
}

Expand Down Expand Up @@ -187,8 +173,7 @@ public void writeString(byte[] utf8) {
bytes[off++] = '"';
for (byte ch : utf8) {
if (ch == '"') {
bytes[off] = '"';
bytes[off + 1] = '"';
IOUtils.putShortUnaligned(bytes, off, DOUBLE_QUOTE_2_LATIN1);
off += 2;
} else {
bytes[off++] = ch;
Expand Down
Loading

0 comments on commit 41662c7

Please sign in to comment.