Skip to content

Commit d55e235

Browse files
[memprof] Use std::unique_ptr instead of std::optional (#94655)
Changing the type of Frame::SymbolName from std::optional<std::string> to std::unique<std::string> reduces sizeof(Frame) from 64 to 32. The smaller type reduces the cycle and instruction counts by 23% and 4.4%, respectively, with "llvm-profdata show" modified to deserialize all MemProfRecords in a MemProf V2 profile. The peak memory usage is cut down nearly by half.
1 parent f078548 commit d55e235

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

llvm/include/llvm/ProfileData/MemProf.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ struct Frame {
199199
GlobalValue::GUID Function;
200200
// The symbol name for the function. Only populated in the Frame by the reader
201201
// if requested during initialization. This field should not be serialized.
202-
std::optional<std::string> SymbolName;
202+
std::unique_ptr<std::string> SymbolName;
203203
// The source line offset of the call from the beginning of parent function.
204204
uint32_t LineOffset;
205205
// The source column number of the call to help distinguish multiple calls
@@ -210,7 +210,9 @@ struct Frame {
210210

211211
Frame(const Frame &Other) {
212212
Function = Other.Function;
213-
SymbolName = Other.SymbolName;
213+
SymbolName = Other.SymbolName
214+
? std::make_unique<std::string>(*Other.SymbolName)
215+
: nullptr;
214216
LineOffset = Other.LineOffset;
215217
Column = Other.Column;
216218
IsInlineFrame = Other.IsInlineFrame;
@@ -228,7 +230,9 @@ struct Frame {
228230

229231
Frame &operator=(const Frame &Other) {
230232
Function = Other.Function;
231-
SymbolName = Other.SymbolName;
233+
SymbolName = Other.SymbolName
234+
? std::make_unique<std::string>(*Other.SymbolName)
235+
: nullptr;
232236
LineOffset = Other.LineOffset;
233237
Column = Other.Column;
234238
IsInlineFrame = Other.IsInlineFrame;
@@ -237,10 +241,10 @@ struct Frame {
237241

238242
bool operator!=(const Frame &Other) const { return !operator==(Other); }
239243

240-
bool hasSymbolName() const { return SymbolName.has_value(); }
244+
bool hasSymbolName() const { return !!SymbolName; }
241245

242246
StringRef getSymbolName() const {
243-
assert(SymbolName.has_value());
247+
assert(hasSymbolName());
244248
return *SymbolName;
245249
}
246250

llvm/lib/ProfileData/MemProfReader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,7 @@ Error RawMemProfReader::readNextRecord(
690690
return F;
691691
auto Iter = this->GuidToSymbolName.find(F.Function);
692692
assert(Iter != this->GuidToSymbolName.end());
693-
F.SymbolName = Iter->getSecond();
693+
F.SymbolName = std::make_unique<std::string>(Iter->getSecond());
694694
return F;
695695
};
696696
return MemProfReader::readNextRecord(GuidRecord, IdToFrameCallback);

0 commit comments

Comments
 (0)