Skip to content

Commit b9b9a23

Browse files
mxms0mxmsvitalybuka
authored
[ProfData] Improve efficiency of reader (#169730)
Pre-reserve space in the map before inserting. In release builds, 9.4% of all CPU time is spent in llvm::sampleprof::ProfileSymbolList::add. Of that 9.4%, roughly half is in llvm::DenseMapBase::grow. --------- Co-authored-by: mxms <[email protected]> Co-authored-by: Vitaly Buka <[email protected]>
1 parent 1d3384e commit b9b9a23

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

llvm/include/llvm/ProfileData/SampleProf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1658,6 +1658,7 @@ class ProfileSymbolList {
16581658
}
16591659

16601660
unsigned size() { return Syms.size(); }
1661+
void reserve(size_t Size) { Syms.reserve(Size); }
16611662

16621663
void setToCompress(bool TC) { ToCompress = TC; }
16631664
bool toCompress() { return ToCompress; }

llvm/lib/ProfileData/SampleProf.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#include "llvm/Support/ErrorHandling.h"
2323
#include "llvm/Support/LEB128.h"
2424
#include "llvm/Support/raw_ostream.h"
25+
#include <algorithm>
26+
#include <cstdint>
2527
#include <string>
2628
#include <system_error>
2729

@@ -398,6 +400,10 @@ LLVM_DUMP_METHOD void FunctionSamples::dump() const { print(dbgs(), 0); }
398400

399401
std::error_code ProfileSymbolList::read(const uint8_t *Data,
400402
uint64_t ListSize) {
403+
// Scan forward to see how many elements we expect.
404+
reserve(std::min<uint64_t>(ProfileSymbolListCutOff,
405+
std::count(Data, Data + ListSize, 0)));
406+
401407
const char *ListStart = reinterpret_cast<const char *>(Data);
402408
uint64_t Size = 0;
403409
uint64_t StrNum = 0;

0 commit comments

Comments
 (0)