Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d07fb882 authored by Jesse Wilson's avatar Jesse Wilson
Browse files

Optimizing skipValue() to avoid allocation.

I ran a quick benchmark on a desktop VM: this shortened the time
to parse a complete JSON document by ~25%.

Change-Id: Id479734654addfe86f4bf251f0dd6e78843023bf
http://microbenchmarks.appspot.com/run/jessewilson@google.com/twitter.JsonParseBenchmark/366001
parent 1ba41714
Loading
Loading
Loading
Loading
+23 −13
Original line number Diff line number Diff line
@@ -210,6 +210,9 @@ public final class JsonReader implements Closeable {
    /** The text of the next literal value. */
    private String value;

    /** True if we're currently handling a skipValue() call. */
    private boolean skipping = false;

    /**
     * Creates a new instance that reads a JSON-encoded stream from {@code in}.
     */
@@ -560,8 +563,8 @@ public final class JsonReader implements Closeable {
     * stream contains unrecognized or unhandled values.
     */
    public void skipValue() throws IOException {
        // TODO: suppress string creation while elements are being skipped!

        skipping = true;
        try {
            int count = 0;
            do {
                JsonToken token = advance();
@@ -571,6 +574,9 @@ public final class JsonReader implements Closeable {
                    count--;
                }
            } while (count != 0);
        } finally {
            skipping = false;
        }
    }

    private JsonScope peekStack() {
@@ -864,7 +870,9 @@ public final class JsonReader implements Closeable {
                int c = buffer[pos++];

                if (c == quote) {
                    if (builder == null) {
                    if (skipping) {
                        return "skipped!";
                    } else if (builder == null) {
                        return new String(buffer, start, pos - start - 1);
                    } else {
                        builder.append(buffer, start, pos - start - 1);
@@ -921,7 +929,9 @@ public final class JsonReader implements Closeable {
                    case '\r':
                    case '\n':
                        pos--;
                        if (builder == null) {
                        if (skipping) {
                            return "skipped!";
                        } else if (builder == null) {
                            return new String(buffer, start, pos - start);
                        } else {
                            builder.append(buffer, start, pos - start);