Skip to content

Commit aaf1697

Browse files
SC llvm teamSC llvm team
authored andcommitted
Merged main:a7f0195b4404 into amd-gfx:2b44fbb977a6
Local branch amd-gfx 2b44fbb Merged main:ea72a4e6547f into amd-gfx:7199f6eb6d6f Remote branch main a7f0195 [scudo] secondary allocator cache optimal-fit retrieval
2 parents 2b44fbb + a7f0195 commit aaf1697

File tree

20 files changed

+471
-182
lines changed

20 files changed

+471
-182
lines changed

clang-tools-extra/clang-tidy/performance/FasterStringFindCheck.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,20 @@ std::optional<std::string> makeCharacterLiteral(const StringLiteral *Literal) {
2626
Literal->outputString(OS);
2727
}
2828
// Now replace the " with '.
29-
auto Pos = Result.find_first_of('"');
30-
if (Pos == Result.npos)
29+
auto OpenPos = Result.find_first_of('"');
30+
if (OpenPos == Result.npos)
3131
return std::nullopt;
32-
Result[Pos] = '\'';
33-
Pos = Result.find_last_of('"');
34-
if (Pos == Result.npos)
32+
Result[OpenPos] = '\'';
33+
34+
auto ClosePos = Result.find_last_of('"');
35+
if (ClosePos == Result.npos)
3536
return std::nullopt;
36-
Result[Pos] = '\'';
37+
Result[ClosePos] = '\'';
38+
39+
// "'" is OK, but ''' is not, so add a backslash
40+
if ((ClosePos - OpenPos) == 2 && Result[OpenPos + 1] == '\'')
41+
Result.replace(OpenPos + 1, 1, "\\'");
42+
3743
return Result;
3844
}
3945

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,10 @@ Changes in existing checks
188188
<clang-tidy/checks/modernize/loop-convert>` to support for-loops with
189189
iterators initialized by free functions like ``begin``, ``end``, or ``size``.
190190

191+
- Improved :doc:`performance-faster-string-find
192+
<clang-tidy/checks/performance/faster-string-find>` check to properly escape
193+
single quotes.
194+
191195
- Improved :doc:`performanc-noexcept-swap
192196
<clang-tidy/checks/performance/noexcept-swap>` check to enforce a stricter
193197
match with the swap function signature, eliminating false-positives.

clang-tools-extra/test/clang-tidy/checkers/performance/faster-string-find.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,21 @@ void StringFind() {
5656
// CHECK-MESSAGES: [[@LINE-1]]:12: warning: 'find' called with a string literal
5757
// CHECK-FIXES: Str.find('a', 1);
5858

59-
// Doens't work with strings smaller or larger than 1 char.
59+
// Doesn't work with strings smaller or larger than 1 char.
6060
Str.find("");
6161
Str.find("ab");
6262

6363
// Doesn't do anything with the 3 argument overload.
6464
Str.find("a", 1, 1);
6565

66+
// Single quotes are escaped properly
67+
Str.find("'");
68+
// CHECK-MESSAGES: [[@LINE-1]]:12: warning: 'find' called with a string literal
69+
// CHECK-FIXES: Str.find('\'');
70+
Str.find("\'");
71+
// CHECK-MESSAGES: [[@LINE-1]]:12: warning: 'find' called with a string literal
72+
// CHECK-FIXES: Str.find('\'');
73+
6674
// Other methods that can also be replaced
6775
Str.rfind("a");
6876
// CHECK-MESSAGES: [[@LINE-1]]:13: warning: 'rfind' called with a string literal

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

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -279,41 +279,64 @@ template <typename Config> class MapAllocatorCache {
279279
LargeBlock::Header **H, bool *Zeroed) EXCLUDES(Mutex) {
280280
const uptr PageSize = getPageSizeCached();
281281
const u32 MaxCount = atomic_load_relaxed(&MaxEntriesCount);
282+
// 10% of the requested size proved to be the optimal choice for
283+
// retrieving cached blocks after testing several options.
284+
constexpr u32 FragmentedBytesDivisor = 10;
282285
bool Found = false;
283286
CachedBlock Entry;
284-
uptr HeaderPos = 0;
287+
uptr EntryHeaderPos = 0;
285288
{
286289
ScopedLock L(Mutex);
287290
CallsToRetrieve++;
288291
if (EntriesCount == 0)
289292
return false;
293+
u32 OptimalFitIndex = 0;
294+
uptr MinDiff = UINTPTR_MAX;
290295
for (u32 I = 0; I < MaxCount; I++) {
291296
if (!Entries[I].isValid())
292297
continue;
293298
const uptr CommitBase = Entries[I].CommitBase;
294299
const uptr CommitSize = Entries[I].CommitSize;
295300
const uptr AllocPos =
296301
roundDown(CommitBase + CommitSize - Size, Alignment);
297-
HeaderPos = AllocPos - HeadersSize;
302+
const uptr HeaderPos = AllocPos - HeadersSize;
298303
if (HeaderPos > CommitBase + CommitSize)
299304
continue;
300305
if (HeaderPos < CommitBase ||
301306
AllocPos > CommitBase + PageSize * MaxUnusedCachePages) {
302307
continue;
303308
}
304309
Found = true;
305-
Entry = Entries[I];
306-
Entries[I].invalidate();
310+
const uptr Diff = HeaderPos - CommitBase;
311+
// immediately use a cached block if it's size is close enough to the
312+
// requested size.
313+
const uptr MaxAllowedFragmentedBytes =
314+
(CommitBase + CommitSize - HeaderPos) / FragmentedBytesDivisor;
315+
if (Diff <= MaxAllowedFragmentedBytes) {
316+
OptimalFitIndex = I;
317+
EntryHeaderPos = HeaderPos;
318+
break;
319+
}
320+
// keep track of the smallest cached block
321+
// that is greater than (AllocSize + HeaderSize)
322+
if (Diff > MinDiff)
323+
continue;
324+
OptimalFitIndex = I;
325+
MinDiff = Diff;
326+
EntryHeaderPos = HeaderPos;
327+
}
328+
if (Found) {
329+
Entry = Entries[OptimalFitIndex];
330+
Entries[OptimalFitIndex].invalidate();
307331
EntriesCount--;
308332
SuccessfulRetrieves++;
309-
break;
310333
}
311334
}
312335
if (!Found)
313336
return false;
314337

315338
*H = reinterpret_cast<LargeBlock::Header *>(
316-
LargeBlock::addHeaderTag<Config>(HeaderPos));
339+
LargeBlock::addHeaderTag<Config>(EntryHeaderPos));
317340
*Zeroed = Entry.Time == 0;
318341
if (useMemoryTagging<Config>(Options))
319342
Entry.MemMap.setMemoryPermission(Entry.CommitBase, Entry.CommitSize, 0);

0 commit comments

Comments
 (0)