[PATCH] D119951: [demangler] Fix build breakage

Nathan Sidwell via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 16 08:10:04 PST 2022


urnathan created this revision.
urnathan added reviewers: thakis, ChuanqiXu, bruno.
urnathan requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

D119177 <https://reviews.llvm.org/D119177> broke some builds.
std::max was being found on those that worked by some unknown path, not via <algorithm>. As that's a large header, this just open-codes the comparisons.

Also I took the chance to improve the 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.


https://reviews.llvm.org/D119951

Files:
  libcxxabi/src/demangle/Utility.h
  llvm/include/llvm/Demangle/Utility.h


Index: llvm/include/llvm/Demangle/Utility.h
===================================================================
--- llvm/include/llvm/Demangle/Utility.h
+++ llvm/include/llvm/Demangle/Utility.h
@@ -17,6 +17,7 @@
 #define DEMANGLE_UTILITY_H
 
 #include "StringView.h"
+#include <algorithm>
 #include <array>
 #include <cstdint>
 #include <cstdlib>
@@ -37,10 +38,13 @@
   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;
-      Need = std::max(Need, MinInitAlloc);
-      BufferCapacity = std::max(Need, BufferCapacity * 2);
+      // 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;
       Buffer = static_cast<char *>(std::realloc(Buffer, BufferCapacity));
       if (Buffer == nullptr)
         std::terminate();
Index: libcxxabi/src/demangle/Utility.h
===================================================================
--- libcxxabi/src/demangle/Utility.h
+++ libcxxabi/src/demangle/Utility.h
@@ -38,10 +38,13 @@
   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;
-      Need = std::max(Need, MinInitAlloc);
-      BufferCapacity = std::max(Need, BufferCapacity * 2);
+      // 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;
       Buffer = static_cast<char *>(std::realloc(Buffer, BufferCapacity));
       if (Buffer == nullptr)
         std::terminate();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D119951.409274.patch
Type: text/x-patch
Size: 2105 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220216/b053af25/attachment.bin>


More information about the llvm-commits mailing list