DescriptionAllocate space in StringBuilder for m_string and the new bytes.
StringBuilder has an optimization to just store the first String object
you append to avoid a copy in cases where you only append one String
and then call toString(), for example if there's onlyone Text child
and you do element.textContent.
When appending the second string we then allocate a Vector buffer and
copy the first string (m_string) into it. We weren't allocating extra
space for the bytes we were about to append in the second string
though, which meant we always did an extra copy immediately following
the the buffer creation:
String first;
String second;
StringBuilder builder;
// No copy, just stores a pointer to first.
builder.append(first);
// 1. Creates a Vector
// 2. Copies first into it.
// 3. Appends second to the Vector which causes a Vector resize.
builder.append(second);
We solve this by calling reserveInitialCapacity with m_length + the
number of bytes we're about to append when we created the buffer. We
also inflate the number of bytes to be at least the inline capacity
size so that doing:
builder.append(first);
builder.append('1');
builder.append('2');
doesn't require an immediate resize for appending '2' by ensuring we
always overallocate the Vector by at least the inline capacity size so
even when appending a String you get 16 chars of extra space to insert
something else.
BUG=624642
Committed: https://crrev.com/464918ccc62d3a70a976b3c38b5ef6ce6e552f31
Cr-Commit-Position: refs/heads/master@{#403407}
Patch Set 1 #
Total comments: 2
Messages
Total messages: 16 (6 generated)
|