Merge pull request #1015 from yasirmhd/master
Memory optimisation in JsonReader
This commit is contained in:
commit
2d072bae3a
@ -985,7 +985,7 @@ public class JsonReader implements Closeable {
|
|||||||
private String nextQuotedValue(char quote) throws IOException {
|
private String nextQuotedValue(char quote) throws IOException {
|
||||||
// Like nextNonWhitespace, this uses locals 'p' and 'l' to save inner-loop field access.
|
// Like nextNonWhitespace, this uses locals 'p' and 'l' to save inner-loop field access.
|
||||||
char[] buffer = this.buffer;
|
char[] buffer = this.buffer;
|
||||||
StringBuilder builder = new StringBuilder();
|
StringBuilder builder = null;
|
||||||
while (true) {
|
while (true) {
|
||||||
int p = pos;
|
int p = pos;
|
||||||
int l = limit;
|
int l = limit;
|
||||||
@ -996,12 +996,23 @@ public class JsonReader implements Closeable {
|
|||||||
|
|
||||||
if (c == quote) {
|
if (c == quote) {
|
||||||
pos = p;
|
pos = p;
|
||||||
builder.append(buffer, start, p - start - 1);
|
int len = p - start - 1;
|
||||||
return builder.toString();
|
if (builder == null) {
|
||||||
|
return new String(buffer, start, len);
|
||||||
|
} else {
|
||||||
|
builder.append(buffer, start, len);
|
||||||
|
return builder.toString();
|
||||||
|
}
|
||||||
} else if (c == '\\') {
|
} else if (c == '\\') {
|
||||||
pos = p;
|
pos = p;
|
||||||
builder.append(buffer, start, p - start - 1);
|
int len = p - start - 1;
|
||||||
builder.append(readEscapeCharacter());
|
char escapeChar = readEscapeCharacter();
|
||||||
|
if (builder == null) {
|
||||||
|
int estimatedLength = (len + pos - p) * 2;
|
||||||
|
builder = new StringBuilder(Math.max(estimatedLength, 16));
|
||||||
|
}
|
||||||
|
builder.append(buffer, start, len);
|
||||||
|
builder.append(escapeChar);
|
||||||
p = pos;
|
p = pos;
|
||||||
l = limit;
|
l = limit;
|
||||||
start = p;
|
start = p;
|
||||||
@ -1011,6 +1022,10 @@ public class JsonReader implements Closeable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (builder == null) {
|
||||||
|
int estimatedLength = (p - start) * 2;
|
||||||
|
builder = new StringBuilder(Math.max(estimatedLength, 16));
|
||||||
|
}
|
||||||
builder.append(buffer, start, p - start);
|
builder.append(buffer, start, p - start);
|
||||||
pos = p;
|
pos = p;
|
||||||
if (!fillBuffer(1)) {
|
if (!fillBuffer(1)) {
|
||||||
|
Loading…
Reference in New Issue
Block a user