Skip to content

Commit

Permalink
optimization for readLocalDate (#3270)
Browse files Browse the repository at this point in the history
* optimization for readLocalDate

* use IOUtils.digit2

* check style
  • Loading branch information
wenshao authored Jan 10, 2025
1 parent fc96048 commit 91e54fc
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 83 deletions.
66 changes: 24 additions & 42 deletions core/src/main/java/com/alibaba/fastjson2/JSONReaderUTF16.java
Original file line number Diff line number Diff line change
Expand Up @@ -4539,47 +4539,29 @@ public final LocalDate readLocalDate() {
&& chars[offset + 7] == '-'
&& chars[offset + 10] == quote
) {
char y0 = chars[offset];
char y1 = chars[offset + 1];
char y2 = chars[offset + 2];
char y3 = chars[offset + 3];
char m0 = chars[offset + 5];
char m1 = chars[offset + 6];
char d0 = chars[offset + 8];
char d1 = chars[offset + 9];

int year;
int month;
if (y0 >= '0' && y0 <= '9'
&& y1 >= '0' && y1 <= '9'
&& y2 >= '0' && y2 <= '9'
&& y3 >= '0' && y3 <= '9'
) {
year = (y0 - '0') * 1000 + (y1 - '0') * 100 + (y2 - '0') * 10 + (y3 - '0');
if (m0 >= '0' && m0 <= '9' && m1 >= '0' && m1 <= '9') {
month = (m0 - '0') * 10 + (m1 - '0');
int dom;
if (d0 >= '0' && d0 <= '9' && d1 >= '0' && d1 <= '9') {
dom = (d0 - '0') * 10 + (d1 - '0');

LocalDate ldt;
try {
ldt = year == 0 && month == 0 && dom == 0
? null
: LocalDate.of(year, month, dom);
} catch (DateTimeException ex) {
throw new JSONException(info("read date error"), ex);
}

this.offset = offset + 11;
next();
if (comma = (this.ch == ',')) {
next();
}
return ldt;
}
}
int year = IOUtils.digit4(chars, offset);
int month = IOUtils.digit2(chars, offset + 5);
int dom = IOUtils.digit2(chars, offset + 8);

if ((year | month | dom) < 0) {
throw new JSONException(info("read date error"));
}

LocalDate ldt;
try {
ldt = year == 0 && month == 0 && dom == 0
? null
: LocalDate.of(year, month, dom);
} catch (DateTimeException ex) {
throw new JSONException(info("read date error"), ex);
}

this.offset = offset + 11;
next();
if (comma = (this.ch == ',')) {
next();
}
return ldt;
}

int nextQuoteOffset = -1;
Expand All @@ -4594,8 +4576,8 @@ public final LocalDate readLocalDate() {
&& chars[nextQuoteOffset - 3] == '-'
) {
int year = TypeUtils.parseInt(chars, offset, nextQuoteOffset - offset - 6);
int month = TypeUtils.parseInt(chars, nextQuoteOffset - 5, 2);
int dayOfMonth = TypeUtils.parseInt(chars, nextQuoteOffset - 2, 2);
int month = IOUtils.digit2(chars, nextQuoteOffset - 5);
int dayOfMonth = IOUtils.digit2(chars, nextQuoteOffset - 2);
LocalDate localDate = LocalDate.of(year, month, dayOfMonth);
this.offset = nextQuoteOffset + 1;
next();
Expand Down
64 changes: 23 additions & 41 deletions core/src/main/java/com/alibaba/fastjson2/JSONReaderUTF8.java
Original file line number Diff line number Diff line change
Expand Up @@ -5680,47 +5680,29 @@ public final LocalDate readLocalDate() {
&& bytes[offset + 7] == '-'
&& bytes[offset + 10] == quote
) {
byte y0 = bytes[offset];
byte y1 = bytes[offset + 1];
byte y2 = bytes[offset + 2];
byte y3 = bytes[offset + 3];
byte m0 = bytes[offset + 5];
byte m1 = bytes[offset + 6];
byte d0 = bytes[offset + 8];
byte d1 = bytes[offset + 9];

int year;
int month;
if (y0 >= '0' && y0 <= '9'
&& y1 >= '0' && y1 <= '9'
&& y2 >= '0' && y2 <= '9'
&& y3 >= '0' && y3 <= '9'
) {
year = (y0 - '0') * 1000 + (y1 - '0') * 100 + (y2 - '0') * 10 + (y3 - '0');
if (m0 >= '0' && m0 <= '9' && m1 >= '0' && m1 <= '9') {
month = (m0 - '0') * 10 + (m1 - '0');
int dom;
if (d0 >= '0' && d0 <= '9' && d1 >= '0' && d1 <= '9') {
dom = (d0 - '0') * 10 + (d1 - '0');

LocalDate ldt;
try {
ldt = year == 0 && month == 0 && dom == 0
? null
: LocalDate.of(year, month, dom);
} catch (DateTimeException ex) {
throw new JSONException(info("read date error"), ex);
}
int year = IOUtils.digit4(bytes, offset);
int month = IOUtils.digit2(bytes, offset + 5);
int dom = IOUtils.digit2(bytes, offset + 8);

this.offset = offset + 11;
next();
if (comma = (this.ch == ',')) {
next();
}
return ldt;
}
}
if ((year | month | dom) < 0) {
throw new JSONException(info("read date error"));
}

LocalDate ldt;
try {
ldt = year == 0 && month == 0 && dom == 0
? null
: LocalDate.of(year, month, dom);
} catch (DateTimeException ex) {
throw new JSONException(info("read date error"), ex);
}

this.offset = offset + 11;
next();
if (comma = (this.ch == ',')) {
next();
}
return ldt;
}

int nextQuoteOffset = -1;
Expand All @@ -5735,8 +5717,8 @@ public final LocalDate readLocalDate() {
&& bytes[nextQuoteOffset - 3] == '-'
) {
int year = TypeUtils.parseInt(bytes, offset, nextQuoteOffset - offset - 6);
int month = TypeUtils.parseInt(bytes, nextQuoteOffset - 5, 2);
int dayOfMonth = TypeUtils.parseInt(bytes, nextQuoteOffset - 2, 2);
int month = IOUtils.digit2(bytes, nextQuoteOffset - 5);
int dayOfMonth = IOUtils.digit2(bytes, nextQuoteOffset - 2);
LocalDate localDate = LocalDate.of(year, month, dayOfMonth);
this.offset = nextQuoteOffset + 1;
next();
Expand Down

0 comments on commit 91e54fc

Please sign in to comment.