Skip to content

Commit

Permalink
improved basic number parse performance.
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Aug 10, 2017
1 parent d36e796 commit 29b669a
Show file tree
Hide file tree
Showing 32 changed files with 1,860 additions and 20 deletions.
186 changes: 176 additions & 10 deletions src/main/java/com/alibaba/fastjson/parser/JSONLexerBase.java
Expand Up @@ -2380,6 +2380,37 @@ public final float scanFieldFloat(char[] fieldName) {
String text = this.subString(start, count); String text = this.subString(start, count);
value = Float.parseFloat(text); value = Float.parseFloat(text);
} }
} else if (chLocal == 'n' && charAt(bp + offset) == 'u' && charAt(bp + offset + 1) == 'l' && charAt(bp + offset + 2) == 'l') {
matchStat = VALUE_NULL;
value = 0;
offset += 3;
chLocal = charAt(bp + offset++);

if (quote && chLocal == '"') {
chLocal = charAt(bp + offset++);
}

for (;;) {
if (chLocal == ',') {
bp += offset;
this.ch = charAt(bp);
matchStat = VALUE_NULL;
token = JSONToken.COMMA;
return value;
} else if (chLocal == '}') {
bp += offset;
this.ch = charAt(bp);
matchStat = VALUE_NULL;
token = JSONToken.RBRACE;
return value;
} else if (isWhitespace(chLocal)) {
chLocal = charAt(bp + offset++);
continue;
}
break;
}
matchStat = NOT_MATCH;
return 0;
} else { } else {
matchStat = NOT_MATCH; matchStat = NOT_MATCH;
return 0; return 0;
Expand Down Expand Up @@ -2441,9 +2472,8 @@ public final float scanFloat(char seperator) {


float value; float value;
if (chLocal >= '0' && chLocal <= '9') { if (chLocal >= '0' && chLocal <= '9') {
int intVal = chLocal - '0'; long intVal = chLocal - '0';

for (; ; ) {
for (;;) {
chLocal = charAt(bp + (offset++)); chLocal = charAt(bp + (offset++));
if (chLocal >= '0' && chLocal <= '9') { if (chLocal >= '0' && chLocal <= '9') {
intVal = intVal * 10 + (chLocal - '0'); intVal = intVal * 10 + (chLocal - '0');
Expand All @@ -2453,13 +2483,18 @@ public final float scanFloat(char seperator) {
} }
} }


if (chLocal == '.') { long power = 1;
boolean small = (chLocal == '.');
if (small) {
chLocal = charAt(bp + (offset++)); chLocal = charAt(bp + (offset++));
if (chLocal >= '0' && chLocal <= '9') { if (chLocal >= '0' && chLocal <= '9') {
for (;;) { intVal = intVal * 10 + (chLocal - '0');
power = 10;
for (; ; ) {
chLocal = charAt(bp + (offset++)); chLocal = charAt(bp + (offset++));
if (chLocal >= '0' && chLocal <= '9') { if (chLocal >= '0' && chLocal <= '9') {
intVal = intVal * 10 + (chLocal - '0'); intVal = intVal * 10 + (chLocal - '0');
power *= 10;
continue; continue;
} else { } else {
break; break;
Expand All @@ -2471,6 +2506,36 @@ public final float scanFloat(char seperator) {
} }
} }


boolean exp = chLocal == 'e' || chLocal == 'E';
if (exp) {
chLocal = charAt(bp + (offset++));
if (chLocal == '+' || chLocal == '-') {
chLocal = charAt(bp + (offset++));
}
for (; ; ) {
if (chLocal >= '0' && chLocal <= '9') {
chLocal = charAt(bp + (offset++));
} else {
break;
}
}
}
// int start, count;
// if (quote) {
// if (chLocal != '"') {
// matchStat = NOT_MATCH;
// return 0;
// } else {
// chLocal = charAt(bp + (offset++));
// }
// start = bp + 1;
// count = bp + offset - start - 2;
// } else {
// start = bp;
// count = bp + offset - start - 1;
// }
// String text = this.subString(start, count);
// value = Float.parseFloat(text);
int start, count; int start, count;
if (quote) { if (quote) {
if (chLocal != '"') { if (chLocal != '"') {
Expand All @@ -2485,8 +2550,47 @@ public final float scanFloat(char seperator) {
start = bp; start = bp;
count = bp + offset - start - 1; count = bp + offset - start - 1;
} }
String text = this.subString(start, count);
value = Float.parseFloat(text); if (!exp && count < 20) {
value = ((float) intVal) / power;
if (negative) {
value = -value;
}
} else {
String text = this.subString(start, count);
value = Float.parseFloat(text);
}
} else if (chLocal == 'n' && charAt(bp + offset) == 'u' && charAt(bp + offset + 1) == 'l' && charAt(bp + offset + 2) == 'l') {
matchStat = VALUE_NULL;
value = 0;
offset += 3;
chLocal = charAt(bp + offset++);

if (quote && chLocal == '"') {
chLocal = charAt(bp + offset++);
}

for (;;) {
if (chLocal == ',') {
bp += offset;
this.ch = charAt(bp);
matchStat = VALUE_NULL;
token = JSONToken.COMMA;
return value;
} else if (chLocal == ']') {
bp += offset;
this.ch = charAt(bp);
matchStat = VALUE_NULL;
token = JSONToken.RBRACKET;
return value;
} else if (isWhitespace(chLocal)) {
chLocal = charAt(bp + offset++);
continue;
}
break;
}
matchStat = NOT_MATCH;
return 0;
} else { } else {
matchStat = NOT_MATCH; matchStat = NOT_MATCH;
return 0; return 0;
Expand Down Expand Up @@ -2522,7 +2626,7 @@ public double scanDouble(char seperator) {
double value; double value;
if (chLocal >= '0' && chLocal <= '9') { if (chLocal >= '0' && chLocal <= '9') {
long intVal = chLocal - '0'; long intVal = chLocal - '0';
for (;;) { for (; ; ) {
chLocal = charAt(bp + (offset++)); chLocal = charAt(bp + (offset++));
if (chLocal >= '0' && chLocal <= '9') { if (chLocal >= '0' && chLocal <= '9') {
intVal = intVal * 10 + (chLocal - '0'); intVal = intVal * 10 + (chLocal - '0');
Expand All @@ -2539,7 +2643,7 @@ public double scanDouble(char seperator) {
if (chLocal >= '0' && chLocal <= '9') { if (chLocal >= '0' && chLocal <= '9') {
intVal = intVal * 10 + (chLocal - '0'); intVal = intVal * 10 + (chLocal - '0');
power = 10; power = 10;
for (;;) { for (; ; ) {
chLocal = charAt(bp + (offset++)); chLocal = charAt(bp + (offset++));
if (chLocal >= '0' && chLocal <= '9') { if (chLocal >= '0' && chLocal <= '9') {
intVal = intVal * 10 + (chLocal - '0'); intVal = intVal * 10 + (chLocal - '0');
Expand All @@ -2561,7 +2665,7 @@ public double scanDouble(char seperator) {
if (chLocal == '+' || chLocal == '-') { if (chLocal == '+' || chLocal == '-') {
chLocal = charAt(bp + (offset++)); chLocal = charAt(bp + (offset++));
} }
for (;;) { for (; ; ) {
if (chLocal >= '0' && chLocal <= '9') { if (chLocal >= '0' && chLocal <= '9') {
chLocal = charAt(bp + (offset++)); chLocal = charAt(bp + (offset++));
} else { } else {
Expand Down Expand Up @@ -2594,6 +2698,37 @@ public double scanDouble(char seperator) {
String text = this.subString(start, count); String text = this.subString(start, count);
value = Double.parseDouble(text); value = Double.parseDouble(text);
} }
} else if (chLocal == 'n' && charAt(bp + offset) == 'u' && charAt(bp + offset + 1) == 'l' && charAt(bp + offset + 2) == 'l') {
matchStat = VALUE_NULL;
value = 0;
offset += 3;
chLocal = charAt(bp + offset++);

if (quote && chLocal == '"') {
chLocal = charAt(bp + offset++);
}

for (;;) {
if (chLocal == ',') {
bp += offset;
this.ch = charAt(bp);
matchStat = VALUE_NULL;
token = JSONToken.COMMA;
return value;
} else if (chLocal == ']') {
bp += offset;
this.ch = charAt(bp);
matchStat = VALUE_NULL;
token = JSONToken.RBRACKET;
return value;
} else if (isWhitespace(chLocal)) {
chLocal = charAt(bp + offset++);
continue;
}
break;
}
matchStat = NOT_MATCH;
return 0;
} else { } else {
matchStat = NOT_MATCH; matchStat = NOT_MATCH;
return 0; return 0;
Expand Down Expand Up @@ -3049,6 +3184,37 @@ public final double scanFieldDouble(char[] fieldName) {
String text = this.subString(start, count); String text = this.subString(start, count);
value = Double.parseDouble(text); value = Double.parseDouble(text);
} }
} else if (chLocal == 'n' && charAt(bp + offset) == 'u' && charAt(bp + offset + 1) == 'l' && charAt(bp + offset + 2) == 'l') {
matchStat = VALUE_NULL;
value = 0;
offset += 3;
chLocal = charAt(bp + offset++);

if (quote && chLocal == '"') {
chLocal = charAt(bp + offset++);
}

for (;;) {
if (chLocal == ',') {
bp += offset;
this.ch = charAt(bp);
matchStat = VALUE_NULL;
token = JSONToken.COMMA;
return value;
} else if (chLocal == '}') {
bp += offset;
this.ch = charAt(bp);
matchStat = VALUE_NULL;
token = JSONToken.RBRACE;
return value;
} else if (isWhitespace(chLocal)) {
chLocal = charAt(bp + offset++);
continue;
}
break;
}
matchStat = NOT_MATCH;
return 0;
} else { } else {
matchStat = NOT_MATCH; matchStat = NOT_MATCH;
return 0; return 0;
Expand Down

0 comments on commit 29b669a

Please sign in to comment.