[libcxx-commits] [libcxxabi] 024495e - [demangler] Improve buffer hysteresis

Nathan Sidwell via libcxx-commits libcxx-commits at lists.llvm.org
Tue Mar 1 04:38:28 PST 2022


Author: Nathan Sidwell
Date: 2022-03-01T04:37:24-08:00
New Revision: 024495e62660a51edd060b57a95ca68d584d3b43

URL: https://github.com/llvm/llvm-project/commit/024495e62660a51edd060b57a95ca68d584d3b43
DIFF: https://github.com/llvm/llvm-project/commit/024495e62660a51edd060b57a95ca68d584d3b43.diff

LOG: [demangler] Improve buffer hysteresis

Improve demangler buffer hysteresis.  If we needed more than double
the buffer, the original code would allocate exactly the amount
needed, and thus consequently the next request would also realloc.
We're very unlikely to get into wanting more than double, after the
first allocation, as it would require the user to have used an
identifier larger than the hysteresis.  With machine generated code
that's possible, but unlikely.

Reviewed By: ChuanqiXu

Differential Revision: https://reviews.llvm.org/D119972

Added: 
    

Modified: 
    libcxxabi/src/demangle/Utility.h
    llvm/include/llvm/Demangle/Utility.h
    llvm/unittests/Demangle/OutputBufferTest.cpp

Removed: 
    


################################################################################
diff  --git a/libcxxabi/src/demangle/Utility.h b/libcxxabi/src/demangle/Utility.h
index f8190b81332ac..0cf70bdde403d 100644
--- a/libcxxabi/src/demangle/Utility.h
+++ b/libcxxabi/src/demangle/Utility.h
@@ -37,10 +37,10 @@ class OutputBuffer {
   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
index c7cf4566e1a39..f3537c12d4d7d 100644
--- a/llvm/include/llvm/Demangle/Utility.h
+++ b/llvm/include/llvm/Demangle/Utility.h
@@ -37,10 +37,10 @@ class OutputBuffer {
   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
index 1d37f567e1d42..9fc572b34ba7a 100644
--- a/llvm/unittests/Demangle/OutputBufferTest.cpp
+++ b/llvm/unittests/Demangle/OutputBufferTest.cpp
@@ -78,3 +78,16 @@ TEST(OutputBufferTest, Prepend) {
 
   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());
+}


        


More information about the libcxx-commits mailing list