[compiler-rt] 119f977 - [scudo] Optimize scudo test string allocation

Dominic Chen via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 12 16:14:00 PST 2022


Author: Dominic Chen
Date: 2022-12-12T15:50:00-08:00
New Revision: 119f977d9e3f8af9f5ac54270e828e354452f903

URL: https://github.com/llvm/llvm-project/commit/119f977d9e3f8af9f5ac54270e828e354452f903
DIFF: https://github.com/llvm/llvm-project/commit/119f977d9e3f8af9f5ac54270e828e354452f903.diff

LOG: [scudo] Optimize scudo test string allocation

When the underlying vector becomes full, it resizes, remaps, and then copies over the old data. To avoid thes excess allocations, allow reservation from the backing vector.

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

Added: 
    

Modified: 
    compiler-rt/lib/scudo/standalone/string_utils.h
    compiler-rt/lib/scudo/standalone/tests/strings_test.cpp

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/scudo/standalone/string_utils.h b/compiler-rt/lib/scudo/standalone/string_utils.h
index dd6ff7893b839..41901194dfdc3 100644
--- a/compiler-rt/lib/scudo/standalone/string_utils.h
+++ b/compiler-rt/lib/scudo/standalone/string_utils.h
@@ -28,6 +28,7 @@ class ScopedString {
   void append(const char *Format, va_list Args);
   void append(const char *Format, ...) FORMAT(2, 3);
   void output() const { outputRaw(String.data()); }
+  void reserve(size_t Size) { String.reserve(Size + 1); }
 
 private:
   Vector<char> String;

diff  --git a/compiler-rt/lib/scudo/standalone/tests/strings_test.cpp b/compiler-rt/lib/scudo/standalone/tests/strings_test.cpp
index 6d7e78a816acd..7a69ffd9762c5 100644
--- a/compiler-rt/lib/scudo/standalone/tests/strings_test.cpp
+++ b/compiler-rt/lib/scudo/standalone/tests/strings_test.cpp
@@ -43,9 +43,11 @@ TEST(ScudoStringsTest, Clear) {
 }
 
 TEST(ScudoStringsTest, ClearLarge) {
+  constexpr char appendString[] = "123";
   scudo::ScopedString Str;
+  Str.reserve(sizeof(appendString) * 10000);
   for (int i = 0; i < 10000; ++i)
-    Str.append("123");
+    Str.append(appendString);
   Str.clear();
   EXPECT_EQ(0ul, Str.length());
   EXPECT_EQ('\0', *Str.data());
@@ -76,6 +78,7 @@ TEST(ScudoStringTest, PotentialOverflows) {
   // of it with variations of append. The expectation is for nothing to crash.
   const scudo::uptr PageSize = scudo::getPageSizeCached();
   scudo::ScopedString Str;
+  Str.reserve(2 * PageSize);
   Str.clear();
   fillString(Str, 2 * PageSize);
   Str.clear();


        


More information about the llvm-commits mailing list