Skip to content

Commit

Permalink
Change LineBuffer to grow exponentially
Browse files Browse the repository at this point in the history
  • Loading branch information
Lexikos committed Oct 17, 2021
1 parent 29fcbf8 commit 5470f96
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
22 changes: 15 additions & 7 deletions source/script.cpp
Expand Up @@ -2773,24 +2773,32 @@ bool Script::EndsWithOperator(LPTSTR aBuf, LPTSTR aBuf_marker)
ResultType Script::LineBuffer::EnsureCapacity(size_t aLength)
{
aLength += RESERVED_SPACE;
return size < aLength ? Realloc(aLength) : OK;
if (size >= aLength)
return OK;
// See comments in Expand() regarding buffer growth.
size_t newsize = size ? size : INITIAL_SIZE;
while (newsize < aLength)
newsize *= 2;
return Realloc(aLength);
}

ResultType Script::LineBuffer::Expand()
{
return Realloc(size + EXPANSION_INTERVAL);
// The buffer typically needs to grow incrementally while joining lines in a continuation section.
// Expanding in large increments avoids multiple reallocs in most files. Expanding exponentially
// scales better in theory, though some testing showed it to have virtually no impact on load time
// even with very long lines. Memory usage isn't a concern since the buffer will be freed after
// the script file is read.
return Realloc(size ? size * 2 : INITIAL_SIZE);
}

ResultType Script::LineBuffer::Realloc(size_t aNewSize)
{
// The buffer typically needs to grow incrementally while joining lines in a continuation section.
// Expanding in large increments avoids multiple reallocs in most files.
size_t newsize = (aNewSize + EXPANSION_INTERVAL - 1) / EXPANSION_INTERVAL * EXPANSION_INTERVAL;
LPTSTR newp = (LPTSTR)realloc(p, sizeof(TCHAR) * newsize);
LPTSTR newp = (LPTSTR)realloc(p, sizeof(TCHAR) * aNewSize);
if (!newp)
return FAIL;
p = newp;
size = newsize;
size = aNewSize;
return OK;
}

Expand Down
2 changes: 1 addition & 1 deletion source/script.h
Expand Up @@ -2893,7 +2893,7 @@ class Script
TCHAR *p = nullptr;
size_t length = 0;
size_t size = 0;
const size_t EXPANSION_INTERVAL = 0x1000;
const size_t INITIAL_SIZE = 0x1000; // # characters for first allocation. Subsequent Reallocs will grow exponentially.
const size_t RESERVED_SPACE = 3; // Allow for a null-terminator and appending "()" for call statements.
size_t Capacity() { ASSERT(size); return size - RESERVED_SPACE; }
ResultType Expand();
Expand Down

0 comments on commit 5470f96

Please sign in to comment.