Skip to content

Commit 119f977

Browse files
author
Dominic Chen
committed
[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
1 parent 80760e9 commit 119f977

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

compiler-rt/lib/scudo/standalone/string_utils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class ScopedString {
2828
void append(const char *Format, va_list Args);
2929
void append(const char *Format, ...) FORMAT(2, 3);
3030
void output() const { outputRaw(String.data()); }
31+
void reserve(size_t Size) { String.reserve(Size + 1); }
3132

3233
private:
3334
Vector<char> String;

compiler-rt/lib/scudo/standalone/tests/strings_test.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,11 @@ TEST(ScudoStringsTest, Clear) {
4343
}
4444

4545
TEST(ScudoStringsTest, ClearLarge) {
46+
constexpr char appendString[] = "123";
4647
scudo::ScopedString Str;
48+
Str.reserve(sizeof(appendString) * 10000);
4749
for (int i = 0; i < 10000; ++i)
48-
Str.append("123");
50+
Str.append(appendString);
4951
Str.clear();
5052
EXPECT_EQ(0ul, Str.length());
5153
EXPECT_EQ('\0', *Str.data());
@@ -76,6 +78,7 @@ TEST(ScudoStringTest, PotentialOverflows) {
7678
// of it with variations of append. The expectation is for nothing to crash.
7779
const scudo::uptr PageSize = scudo::getPageSizeCached();
7880
scudo::ScopedString Str;
81+
Str.reserve(2 * PageSize);
7982
Str.clear();
8083
fillString(Str, 2 * PageSize);
8184
Str.clear();

0 commit comments

Comments
 (0)