Skip to content

Commit

Permalink
optimize codeSize
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed May 27, 2023
1 parent 4dffc98 commit df19106
Show file tree
Hide file tree
Showing 6 changed files with 665 additions and 127 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public static void jayway() throws Exception {
}

public static void main(String[] args) throws Exception {
// fastjson2();
fastjson2();
// fastjson2Compile();
jayway();
// jayway();
}
}
18 changes: 2 additions & 16 deletions core/src/main/java/com/alibaba/fastjson2/JSONReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -349,23 +349,9 @@ static char char4(int c1, int c2, int c3, int c4) {
+ DIGITS2[c4]);
}

public boolean nextIfObjectStart() {
if (this.ch != '{') {
return false;
}
next();
return true;
}

public abstract boolean nextIfObjectStart();
public abstract boolean nextIfNullOrEmptyString();

public boolean nextIfObjectEnd() {
if (this.ch != '}') {
return false;
}
next();
return true;
}
public abstract boolean nextIfObjectEnd();

public int startArray() {
if (!nextIfMatch('[')) {
Expand Down
104 changes: 88 additions & 16 deletions core/src/main/java/com/alibaba/fastjson2/JSONReaderASCII.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,60 +24,132 @@ class JSONReaderASCII

@Override
public final void next() {
int offset = this.offset;
if (offset >= end) {
ch = EOI;
return;
}

final byte[] bytes = this.bytes;
ch = (char) bytes[offset];
byte ch = bytes[offset];
while (ch == '\0' || (ch <= ' ' && ((1L << ch) & SPACE) != 0)) {
offset++;
if (offset >= end) {
ch = EOI;
this.offset = offset;
this.ch = EOI;
return;
}
ch = (char) bytes[offset];
ch = bytes[offset];
}
offset++;
this.offset = offset + 1;
this.ch = (char) (ch & 0xFF);

while (ch == '/' && offset < bytes.length && bytes[offset] == '/') {
while (this.ch == '/' && this.offset < bytes.length && bytes[this.offset] == '/') {
skipLineComment();
}
}

public final boolean nextIfObjectStart() {
if (this.ch != '{') {
return false;
}

int offset = this.offset;
if (offset >= end) {
ch = EOI;
} else {
final byte[] bytes = this.bytes;
byte ch = bytes[offset];
while (ch == '\0' || (ch <= ' ' && ((1L << ch) & SPACE) != 0)) {
offset++;
if (offset >= end) {
this.offset = offset;
this.ch = EOI;
return true;
}
ch = bytes[offset];
}
this.offset = offset + 1;
this.ch = (char) (ch & 0xFF);

while (this.ch == '/' && this.offset < bytes.length && bytes[this.offset] == '/') {
skipLineComment();
}
}

return true;
}

public final boolean nextIfObjectEnd() {
if (this.ch != '}') {
return false;
}

int offset = this.offset;
if (offset >= end) {
ch = EOI;
} else {
final byte[] bytes = this.bytes;
byte ch = bytes[offset];
while (ch == '\0' || (ch <= ' ' && ((1L << ch) & SPACE) != 0)) {
offset++;
if (offset >= end) {
this.offset = offset;
this.ch = EOI;
return true;
}
ch = bytes[offset];
}
this.offset = offset + 1;
this.ch = (char) (ch & 0xFF);

while (this.ch == '/' && this.offset < bytes.length && bytes[this.offset] == '/') {
skipLineComment();
}
}

return true;
}

@Override
public final boolean nextIfMatch(char ch) {
while (this.ch <= ' ' && ((1L << this.ch) & SPACE) != 0) {
public final boolean nextIfMatch(char m) {
final byte[] bytes = this.bytes;
int offset = this.offset;
int ch = this.ch;
while (ch <= ' ' && ((1L << ch) & SPACE) != 0) {
if (offset >= end) {
this.ch = EOI;
ch = EOI;
} else {
this.ch = (char) bytes[offset++];
ch = bytes[offset++];
}
}

if (this.ch != ch) {
if (ch != m) {
return false;
}
comma = ch == ',';
comma = m == ',';

if (offset >= end) {
this.offset = offset;
this.ch = EOI;
return true;
}

this.ch = (char) (bytes[offset] & 0xFF);
while (this.ch == '\0' || (this.ch <= ' ' && ((1L << this.ch) & SPACE) != 0)) {
ch = bytes[offset];
while (ch == '\0' || (ch <= ' ' && ((1L << ch) & SPACE) != 0)) {
offset++;
if (offset >= end) {
this.offset = offset;
this.ch = EOI;
return true;
}
this.ch = (char) (bytes[offset] & 0xFF);
ch = bytes[offset];
}
offset++;

while (this.ch == '/' && offset < bytes.length && bytes[offset] == '/') {
this.offset = offset + 1;
this.ch = (char) (ch & 0xFF);

while (this.ch == '/' && this.offset < bytes.length && bytes[this.offset] == '/') {
skipLineComment();
}

Expand Down
Loading

0 comments on commit df19106

Please sign in to comment.