From b7c3e0067cd2e69d0f12da7c3318a713c6bdcc27 Mon Sep 17 00:00:00 2001 From: Jesse Wilson Date: Wed, 14 Dec 2011 06:07:40 +0000 Subject: [PATCH] Avoid local field accesses in nextString(char). This saves a modest 2%. --- .../com/google/gson/stream/JsonReader.java | 34 ++++++++++++------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/gson/src/main/java/com/google/gson/stream/JsonReader.java b/gson/src/main/java/com/google/gson/stream/JsonReader.java index f273876e..45a68476 100644 --- a/gson/src/main/java/com/google/gson/stream/JsonReader.java +++ b/gson/src/main/java/com/google/gson/stream/JsonReader.java @@ -991,40 +991,50 @@ public class JsonReader implements Closeable { * malformed. */ private String nextString(char quote) throws IOException { + // Like nextNonWhitespace, this uses locals 'p' and 'l' to save inner-loop field access. + char[] buffer = this.buffer; StringBuilder builder = null; - do { + while (true) { + int p = pos; + int l = limit; /* the index of the first character not yet appended to the builder. */ - int start = pos; - while (pos < limit) { - int c = buffer[pos++]; + int start = p; + while (p < l) { + int c = buffer[p++]; if (c == quote) { + pos = p; if (skipping) { return "skipped!"; } else if (builder == null) { - return stringPool.get(buffer, start, pos - start - 1); + return stringPool.get(buffer, start, p - start - 1); } else { - builder.append(buffer, start, pos - start - 1); + builder.append(buffer, start, p - start - 1); return builder.toString(); } } else if (c == '\\') { + pos = p; if (builder == null) { builder = new StringBuilder(); } - builder.append(buffer, start, pos - start - 1); + builder.append(buffer, start, p - start - 1); builder.append(readEscapeCharacter()); - start = pos; + p = pos; + l = limit; + start = p; } } if (builder == null) { builder = new StringBuilder(); } - builder.append(buffer, start, pos - start); - } while (fillBuffer(1)); - - throw syntaxError("Unterminated string"); + builder.append(buffer, start, p - start); + pos = p; + if (!fillBuffer(1)) { + throw syntaxError("Unterminated string"); + } + } } /**