Bug 489820 part 2 - Grow buffers to the worst-case size before tokenizing; fix comments. r=wchen.

This commit is contained in:
Henri Sivonen
2015-08-25 18:05:45 +03:00
parent ee6c94fc31
commit bee2d42d10
16 changed files with 231 additions and 52 deletions

View File

@@ -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]