Skip to content

Commit d936924

Browse files
authored
[BOLT][NFC] Make YamlProfileToFunction a DenseMap (#108712)
YAML function profiles have sparse function IDs, assigned from sequential function IDs from profiled binary. For example, for one large binary, YAML profile has 15K functions, but the highest ID is ~600K, close to number of functions in the profiled binary. In `matchProfileToFunction`, `YamlProfileToFunction` vector was resized to match function ID, which entails a 40X overcommit. Change the type of `YamlProfileToFunction` to DenseMap to reduce memory utilization. #99891 makes use of it for profile lookup associated with a given binary function.
1 parent 7382509 commit d936924

File tree

2 files changed

+4
-13
lines changed

2 files changed

+4
-13
lines changed

bolt/include/bolt/Profile/YAMLProfileReader.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ class YAMLProfileReader : public ProfileReaderBase {
105105
yaml::bolt::BinaryProfile YamlBP;
106106

107107
/// Map a function ID from a YAML profile to a BinaryFunction object.
108-
std::vector<BinaryFunction *> YamlProfileToFunction;
108+
DenseMap<uint32_t, BinaryFunction *> YamlProfileToFunction;
109109

110110
using FunctionSet = std::unordered_set<const BinaryFunction *>;
111111
/// To keep track of functions that have a matched profile before the profile
@@ -162,8 +162,6 @@ class YAMLProfileReader : public ProfileReaderBase {
162162
/// Update matched YAML -> BinaryFunction pair.
163163
void matchProfileToFunction(yaml::bolt::BinaryFunctionProfile &YamlBF,
164164
BinaryFunction &BF) {
165-
if (YamlBF.Id >= YamlProfileToFunction.size())
166-
YamlProfileToFunction.resize(YamlBF.Id + 1);
167165
YamlProfileToFunction[YamlBF.Id] = &BF;
168166
YamlBF.Used = true;
169167

bolt/lib/Profile/YAMLProfileReader.cpp

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,7 @@ bool YAMLProfileReader::parseFunctionProfile(
238238
BB.setExecutionCount(YamlBB.ExecCount);
239239

240240
for (const yaml::bolt::CallSiteInfo &YamlCSI : YamlBB.CallSites) {
241-
BinaryFunction *Callee = YamlCSI.DestId < YamlProfileToFunction.size()
242-
? YamlProfileToFunction[YamlCSI.DestId]
243-
: nullptr;
241+
BinaryFunction *Callee = YamlProfileToFunction.lookup(YamlCSI.DestId);
244242
bool IsFunction = Callee ? true : false;
245243
MCSymbol *CalleeSymbol = nullptr;
246244
if (IsFunction)
@@ -703,7 +701,7 @@ Error YAMLProfileReader::readProfile(BinaryContext &BC) {
703701
break;
704702
}
705703
}
706-
YamlProfileToFunction.resize(YamlBP.Functions.size() + 1);
704+
YamlProfileToFunction.reserve(YamlBP.Functions.size());
707705

708706
// Computes hash for binary functions.
709707
if (opts::MatchProfileWithFunctionHash) {
@@ -756,12 +754,7 @@ Error YAMLProfileReader::readProfile(BinaryContext &BC) {
756754
NormalizeByCalls = usesEvent("branches");
757755
uint64_t NumUnused = 0;
758756
for (yaml::bolt::BinaryFunctionProfile &YamlBF : YamlBP.Functions) {
759-
if (YamlBF.Id >= YamlProfileToFunction.size()) {
760-
// Such profile was ignored.
761-
++NumUnused;
762-
continue;
763-
}
764-
if (BinaryFunction *BF = YamlProfileToFunction[YamlBF.Id])
757+
if (BinaryFunction *BF = YamlProfileToFunction.lookup(YamlBF.Id))
765758
parseFunctionProfile(*BF, YamlBF);
766759
else
767760
++NumUnused;

0 commit comments

Comments
 (0)