Bug 489820 part 2 - Grow buffers to the worst-case size before tokenizing; fix comments. r=wchen.
This commit is contained in:
@@ -602,7 +602,7 @@ public abstract class TreeBuilder<T> implements TokenHandler,
|
||||
// ]NOCPP]
|
||||
start(fragment);
|
||||
charBufferLen = 0;
|
||||
charBuffer = new char[1024];
|
||||
charBuffer = null;
|
||||
framesetOk = true;
|
||||
if (fragment) {
|
||||
T elt;
|
||||
@@ -5594,14 +5594,30 @@ public abstract class TreeBuilder<T> implements TokenHandler,
|
||||
|
||||
private final void accumulateCharactersForced(@Const @NoLength char[] buf,
|
||||
int start, int length) throws SAXException {
|
||||
int newLen = charBufferLen + length;
|
||||
if (newLen > charBuffer.length) {
|
||||
char[] newBuf = new char[newLen];
|
||||
System.arraycopy(buf, start, charBuffer, charBufferLen, length);
|
||||
charBufferLen += length;
|
||||
}
|
||||
|
||||
@Override public void ensureBufferSpace(int inputLength)
|
||||
throws SAXException {
|
||||
// TODO: Unify Tokenizer.strBuf and TreeBuilder.charBuffer so that
|
||||
// this method becomes unnecessary.
|
||||
int worstCase = charBufferLen + inputLength;
|
||||
if (charBuffer == null) {
|
||||
// Add an arbitrary small value to avoid immediate reallocation
|
||||
// once there are a few characters in the buffer.
|
||||
charBuffer = new char[worstCase + 128];
|
||||
} else if (worstCase > charBuffer.length) {
|
||||
// HotSpot reportedly allocates memory with 8-byte accuracy, so
|
||||
// there's no point in trying to do math here to avoid slop.
|
||||
// Maybe we should add some small constant to worstCase here
|
||||
// but not doing that without profiling. In C++ with jemalloc,
|
||||
// the corresponding method should do math to round up here
|
||||
// to avoid slop.
|
||||
char[] newBuf = new char[worstCase];
|
||||
System.arraycopy(charBuffer, 0, newBuf, 0, charBufferLen);
|
||||
charBuffer = newBuf;
|
||||
}
|
||||
System.arraycopy(buf, start, charBuffer, charBufferLen, length);
|
||||
charBufferLen = newLen;
|
||||
}
|
||||
|
||||
// ]NOCPP]
|
||||
|
||||
Reference in New Issue
Block a user