Replace switch with if/else when processing whitespace. If/else is faster!

This commit is contained in:
Jesse Wilson 2012-08-27 04:17:29 +00:00
parent b0a172944a
commit bdf2cac6d4
2 changed files with 39 additions and 49 deletions

View File

@ -1312,14 +1312,11 @@ public class JsonReader implements Closeable {
} }
int c = buffer[p++]; int c = buffer[p++];
switch (c) { if (c == ' ' || c == '\n' || c == '\r' || c == '\t') {
case '\t':
case ' ':
case '\n':
case '\r':
continue; continue;
}
case '/': if (c == '/') {
pos = p; pos = p;
if (p == l) { if (p == l) {
pos--; // push back '/' so it's still in the buffer when this method returns pos--; // push back '/' so it's still in the buffer when this method returns
@ -1354,8 +1351,7 @@ public class JsonReader implements Closeable {
default: default:
return c; return c;
} }
} else if (c == '#') {
case '#':
pos = p; pos = p;
/* /*
* Skip a # hash end-of-line comment. The JSON RFC doesn't * Skip a # hash end-of-line comment. The JSON RFC doesn't
@ -1366,9 +1362,7 @@ public class JsonReader implements Closeable {
skipToEndOfLine(); skipToEndOfLine();
p = pos; p = pos;
l = limit; l = limit;
continue; } else {
default:
pos = p; pos = p;
return c; return c;
} }

View File

@ -146,45 +146,41 @@ public final class ParseBenchmark extends SimpleBenchmark {
jsonReader.close(); jsonReader.close();
} }
public void readObject(com.google.gson.stream.JsonReader reader) throws IOException {
reader.beginObject();
while (reader.hasNext()) {
reader.nextName();
readToken(reader);
}
reader.endObject();
}
public void readArray(com.google.gson.stream.JsonReader reader) throws IOException {
reader.beginArray();
while (reader.hasNext()) {
readToken(reader);
}
reader.endArray();
}
private void readToken(com.google.gson.stream.JsonReader reader) throws IOException { private void readToken(com.google.gson.stream.JsonReader reader) throws IOException {
switch (reader.peek()) { while (true) {
case BEGIN_ARRAY: switch (reader.peek()) {
readArray(reader); case BEGIN_ARRAY:
break; reader.beginArray();
case BEGIN_OBJECT: break;
readObject(reader); case END_ARRAY:
break; reader.endArray();
case BOOLEAN: break;
reader.nextBoolean(); case BEGIN_OBJECT:
break; reader.beginObject();
case NULL: break;
reader.nextNull(); case END_OBJECT:
break; reader.endObject();
case NUMBER: break;
reader.nextLong(); case NAME:
break; reader.nextName();
case STRING: break;
reader.nextString(); case BOOLEAN:
break; reader.nextBoolean();
default: break;
throw new IllegalArgumentException("Unexpected token" + reader.peek()); case NULL:
reader.nextNull();
break;
case NUMBER:
reader.nextLong();
break;
case STRING:
reader.nextString();
break;
case END_DOCUMENT:
return;
default:
throw new IllegalArgumentException("Unexpected token" + reader.peek());
}
} }
} }
} }