Skip to content

Add StringMap::lookup() overload to take a default value and return a reference to the value to avoid a copy #115469

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions llvm/include/llvm/ADT/StringMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,15 @@ class LLVM_ALLOCATORHOLDER_EMPTYBASE StringMap
return ValueTy();
}

/// lookup - Return the entry for the specified key, or a default
Copy link
Member

@MaskRay MaskRay Nov 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove lookup - when adding new methods

https://llvm.org/docs/CodingStandards.html#doxygen-use-in-documentation-comments
Don’t duplicate function or class name at the beginning of the comment.

/// provided value if no such entry exists.
const ValueTy &lookup(StringRef Key, const ValueTy &Value) const {
const_iterator Iter = find(Key);
if (Iter != end())
return Iter->second;
return Value;
}

/// at - Return the entry for the specified key, or abort if no such
/// entry exists.
const ValueTy &at(StringRef Val) const {
Expand Down
12 changes: 3 additions & 9 deletions llvm/lib/CodeGen/MachineOutliner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1289,15 +1289,9 @@ void MachineOutliner::emitInstrCountChangedRemark(

std::string Fname = std::string(F.getName());
unsigned FnCountAfter = MF->getInstructionCount();
unsigned FnCountBefore = 0;

// Check if the function was recorded before.
auto It = FunctionToInstrCount.find(Fname);

// Did we have a previously-recorded size? If yes, then set FnCountBefore
// to that.
if (It != FunctionToInstrCount.end())
FnCountBefore = It->second;
// Check if the function was recorded before and if yes, then set
// FnCountBefore to that.
unsigned FnCountBefore = FunctionToInstrCount.lookup(Fname, 0);

// Compute the delta and emit a remark if there was a change.
int64_t FnDelta = static_cast<int64_t>(FnCountAfter) -
Expand Down
5 changes: 1 addition & 4 deletions llvm/tools/llvm-rc/ResourceScriptStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,7 @@ const StringMap<VersionInfoFixedType> VersionInfoFixed::FixedFieldsInfoMap = {

VersionInfoFixedType VersionInfoFixed::getFixedType(StringRef Type) {
auto UpperType = Type.upper();
auto Iter = FixedFieldsInfoMap.find(UpperType);
if (Iter != FixedFieldsInfoMap.end())
return Iter->getValue();
return FtUnknown;
return FixedFieldsInfoMap.lookup(UpperType, FtUnknown);
}

bool VersionInfoFixed::isTypeSupported(VersionInfoFixedType Type) {
Expand Down
5 changes: 5 additions & 0 deletions llvm/unittests/ADT/StringMapTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,15 @@ TEST_F(StringMapTest, SmallFullMapTest) {

EXPECT_EQ(3u, Map.size());
EXPECT_EQ(0, Map.lookup("eins"));
EXPECT_EQ(7, Map.lookup("eins", 7));
EXPECT_EQ(2, Map.lookup("zwei"));
EXPECT_EQ(2, Map.lookup("zwei", 7));
EXPECT_EQ(0, Map.lookup("drei"));
EXPECT_EQ(7, Map.lookup("drei", 7));
EXPECT_EQ(4, Map.lookup("veir"));
EXPECT_EQ(4, Map.lookup("veir", 7));
EXPECT_EQ(5, Map.lookup("funf"));
EXPECT_EQ(5, Map.lookup("funf", 7));
}

TEST_F(StringMapTest, CopyCtorTest) {
Expand Down
Loading