diff --git a/libcxxabi/src/demangle/Utility.h b/libcxxabi/src/demangle/Utility.h --- a/libcxxabi/src/demangle/Utility.h +++ b/libcxxabi/src/demangle/Utility.h @@ -37,10 +37,10 @@ void grow(size_t N) { size_t Need = N + CurrentPosition; if (Need > BufferCapacity) { - // Avoid many reallocations during startup, with a bit of hysteresis. - constexpr size_t MinInitAlloc = 1024; - if (Need < MinInitAlloc) - Need = MinInitAlloc; + // Reduce the number of reallocations, with a bit of hysteresis. The + // number here is chosen so the first allocation will more-than-likely not + // allocate more than 1K. + Need += 1024 - 32; BufferCapacity *= 2; if (BufferCapacity < Need) BufferCapacity = Need; diff --git a/llvm/include/llvm/Demangle/Utility.h b/llvm/include/llvm/Demangle/Utility.h --- a/llvm/include/llvm/Demangle/Utility.h +++ b/llvm/include/llvm/Demangle/Utility.h @@ -37,10 +37,10 @@ void grow(size_t N) { size_t Need = N + CurrentPosition; if (Need > BufferCapacity) { - // Avoid many reallocations during startup, with a bit of hysteresis. - constexpr size_t MinInitAlloc = 1024; - if (Need < MinInitAlloc) - Need = MinInitAlloc; + // Reduce the number of reallocations, with a bit of hysteresis. The + // number here is chosen so the first allocation will more-than-likely not + // allocate more than 1K. + Need += 1024 - 32; BufferCapacity *= 2; if (BufferCapacity < Need) BufferCapacity = Need; diff --git a/llvm/unittests/Demangle/OutputBufferTest.cpp b/llvm/unittests/Demangle/OutputBufferTest.cpp --- a/llvm/unittests/Demangle/OutputBufferTest.cpp +++ b/llvm/unittests/Demangle/OutputBufferTest.cpp @@ -78,3 +78,16 @@ std::free(OB.getBuffer()); } + +// Test when initial needed size is larger than the default. +TEST(OutputBufferTest, Extend) { + OutputBuffer OB; + + char Massive[2000]; + std::memset(Massive, 'a', sizeof(Massive)); + Massive[sizeof(Massive) - 1] = 0; + OB << Massive; + EXPECT_EQ(Massive, toString(OB)); + + std::free(OB.getBuffer()); +}