StringPooling. This makes things up to 20% faster on dalvikvm for some data sets.

This commit is contained in:
Jesse Wilson 2011-04-28 22:08:06 +00:00
parent 824635158c
commit ce79e16f7a
2 changed files with 65 additions and 3 deletions

View File

@ -188,6 +188,8 @@ public final class JsonReader implements Closeable {
/** The only non-execute prefix this parser permits */ /** The only non-execute prefix this parser permits */
private static final char[] NON_EXECUTE_PREFIX = ")]}'\n".toCharArray(); private static final char[] NON_EXECUTE_PREFIX = ")]}'\n".toCharArray();
private final StringPool stringPool = new StringPool();
/** The input JSON. */ /** The input JSON. */
private final Reader in; private final Reader in;
@ -942,7 +944,7 @@ public final class JsonReader implements Closeable {
if (skipping) { if (skipping) {
return "skipped!"; return "skipped!";
} else if (builder == null) { } else if (builder == null) {
return new String(buffer, start, pos - start - 1); return stringPool.get(buffer, start, pos - start - 1);
} else { } else {
builder.append(buffer, start, pos - start - 1); builder.append(buffer, start, pos - start - 1);
return builder.toString(); return builder.toString();
@ -1002,7 +1004,7 @@ public final class JsonReader implements Closeable {
if (skipping) { if (skipping) {
return "skipped!"; return "skipped!";
} else if (builder == null) { } else if (builder == null) {
return new String(buffer, start, pos - start); return stringPool.get(buffer, start, pos - start);
} else { } else {
builder.append(buffer, start, pos - start); builder.append(buffer, start, pos - start);
return builder.toString(); return builder.toString();
@ -1043,7 +1045,7 @@ public final class JsonReader implements Closeable {
if (pos + 4 > limit && !fillBuffer(4)) { if (pos + 4 > limit && !fillBuffer(4)) {
throw syntaxError("Unterminated escape sequence"); throw syntaxError("Unterminated escape sequence");
} }
String hex = new String(buffer, pos, 4); String hex = stringPool.get(buffer, pos, 4);
pos += 4; pos += 4;
return (char) Integer.parseInt(hex, 16); return (char) Integer.parseInt(hex, 16);

View File

@ -0,0 +1,60 @@
/*
* Copyright (C) 2011 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.gson.stream;
/**
* A pool of string instances. Unlike the {@link String#intern() VM's
* interned strings}, this pool provides no guarantee of reference equality.
* It is intended only to save allocations. This class is not thread safe.
*/
final class StringPool {
private final String[] pool = new String[512];
/**
* Returns a string equal to {@code new String(array, start, length)}.
*/
public String get(char[] array, int start, int length) {
// Compute an arbitrary hash of the content
int hashCode = 0;
for (int i = start; i < start + length; i++) {
hashCode = (hashCode * 31) + array[i];
}
// Pick a bucket using Doug Lea's supplemental secondaryHash function (from HashMap)
hashCode ^= (hashCode >>> 20) ^ (hashCode >>> 12);
hashCode ^= (hashCode >>> 7) ^ (hashCode >>> 4);
int index = hashCode & (pool.length - 1);
String pooled = pool[index];
if (pooled == null || pooled.length() != length) {
String result = new String(array, start, length);
pool[index] = result;
return result;
}
for (int i = 0; i < length; i++) {
if (pooled.charAt(i) != array[start + i]) {
String result = new String(array, start, length);
pool[index] = result;
return result;
}
}
return pooled;
}
}