[PATCH] D135119: [scudo] Optimize scudo test string allocation

Dominic Chen via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 3 17:12:20 PDT 2022


ddcc created this revision.
ddcc added reviewers: eugenis, vitalybuka.
Herald added subscribers: Enna1, cryptoad.
Herald added a project: All.
ddcc requested review of this revision.
Herald added a project: Sanitizers.
Herald added a subscriber: Sanitizers.

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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D135119

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


Index: compiler-rt/lib/scudo/standalone/tests/strings_test.cpp
===================================================================
--- compiler-rt/lib/scudo/standalone/tests/strings_test.cpp
+++ compiler-rt/lib/scudo/standalone/tests/strings_test.cpp
@@ -43,9 +43,10 @@
 }
 
 TEST(ScudoStringsTest, ClearLarge) {
-  scudo::ScopedString Str;
+  const char appendString[] = "123";
+  scudo::ScopedString Str(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());
@@ -75,7 +76,7 @@
   // Use a ScopedString that spans a page, and attempt to write past the end
   // of it with variations of append. The expectation is for nothing to crash.
   const scudo::uptr PageSize = scudo::getPageSizeCached();
-  scudo::ScopedString Str;
+  scudo::ScopedString Str(2 * PageSize);
   Str.clear();
   fillString(Str, 2 * PageSize);
   Str.clear();
Index: compiler-rt/lib/scudo/standalone/string_utils.h
===================================================================
--- compiler-rt/lib/scudo/standalone/string_utils.h
+++ compiler-rt/lib/scudo/standalone/string_utils.h
@@ -18,7 +18,11 @@
 
 class ScopedString {
 public:
-  explicit ScopedString() { String.push_back('\0'); }
+  explicit ScopedString() { init(); }
+  explicit ScopedString(uptr Size) {
+    String.reserve(Size + 1);
+    init();
+  }
   uptr length() { return String.size() - 1; }
   const char *data() { return String.data(); }
   void clear() {
@@ -30,6 +34,7 @@
   void output() const { outputRaw(String.data()); }
 
 private:
+  void init() { String.push_back('\0'); }
   Vector<char> String;
 };
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D135119.464857.patch
Type: text/x-patch
Size: 1710 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221004/1b5cca48/attachment.bin>


More information about the llvm-commits mailing list