Remove usage of StringPool as it appears to slow things down and avoid Java String intern for the common cases.
This commit is contained in:
parent
83e1f45653
commit
70f9280cb6
@ -263,11 +263,6 @@ public class JsonReader implements Closeable {
|
||||
*/
|
||||
private String peekedString;
|
||||
|
||||
/**
|
||||
* A pool of short strings intended to prevent object allocation.
|
||||
*/
|
||||
private static final StringPool stringPool = new StringPool();
|
||||
|
||||
/*
|
||||
* The nesting stack. Using a manual array rather than an ArrayList saves 20%.
|
||||
*/
|
||||
@ -979,8 +974,7 @@ public class JsonReader implements Closeable {
|
||||
private String nextQuotedValue(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;
|
||||
int hashCode = 0;
|
||||
StringBuilder builder = new StringBuilder();
|
||||
while (true) {
|
||||
int p = pos;
|
||||
int l = limit;
|
||||
@ -991,36 +985,21 @@ public class JsonReader implements Closeable {
|
||||
|
||||
if (c == quote) {
|
||||
pos = p;
|
||||
if (builder == null) {
|
||||
return stringPool.get(buffer, start, p - start - 1, hashCode);
|
||||
} else {
|
||||
builder.append(buffer, start, p - start - 1);
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
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, p - start - 1);
|
||||
builder.append(readEscapeCharacter());
|
||||
p = pos;
|
||||
l = limit;
|
||||
start = p;
|
||||
|
||||
} else if (c == '\n') {
|
||||
hashCode = (hashCode * 31) + c;
|
||||
lineNumber++;
|
||||
lineStart = p;
|
||||
} else {
|
||||
hashCode = (hashCode * 31) + c;
|
||||
}
|
||||
}
|
||||
|
||||
if (builder == null) {
|
||||
builder = new StringBuilder();
|
||||
}
|
||||
builder.append(buffer, start, p - start);
|
||||
pos = p;
|
||||
if (!fillBuffer(1)) {
|
||||
|
@ -1,65 +0,0 @@
|
||||
/*
|
||||
* 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.
|
||||
*
|
||||
* <p>This class is safe for concurrent use.
|
||||
*/
|
||||
final class StringPool {
|
||||
/**
|
||||
* The maximum length of strings to add to the pool. Strings longer than this
|
||||
* don't benefit from pooling because we spend more time on pooling than we
|
||||
* save on garbage collection.
|
||||
*/
|
||||
private static final int MAX_LENGTH = 20;
|
||||
private final String[] pool = new String[1024];
|
||||
|
||||
/**
|
||||
* Returns a string equal to {@code new String(array, start, length)}.
|
||||
*/
|
||||
public String get(char[] array, int start, int length, int hashCode) {
|
||||
if (length > StringPool.MAX_LENGTH) {
|
||||
return new String(array, start, length);
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user