Skip to content

[llvm-readobj] Use heterogenous lookups with std::map (NFC) #114929

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

Merged

Conversation

kazutakahirata
Copy link
Contributor

@kazutakahirata kazutakahirata commented Nov 5, 2024

Heterogenous lookups allow us to call find with StringRef, avoiding a
temporary heap allocation of std::string.

@llvmbot
Copy link
Member

llvmbot commented Nov 5, 2024

@llvm/pr-subscribers-llvm-binary-utilities

Author: Kazu Hirata (kazutakahirata)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/114929.diff

1 Files Affected:

  • (modified) llvm/tools/llvm-readobj/ObjDumper.cpp (+2-2)
diff --git a/llvm/tools/llvm-readobj/ObjDumper.cpp b/llvm/tools/llvm-readobj/ObjDumper.cpp
index d02d1e543d31dc..d3c613ee823bae 100644
--- a/llvm/tools/llvm-readobj/ObjDumper.cpp
+++ b/llvm/tools/llvm-readobj/ObjDumper.cpp
@@ -106,7 +106,7 @@ static std::vector<object::SectionRef>
 getSectionRefsByNameOrIndex(const object::ObjectFile &Obj,
                             ArrayRef<std::string> Sections) {
   std::vector<object::SectionRef> Ret;
-  std::map<std::string, bool> SecNames;
+  std::map<std::string, bool, std::less<>> SecNames;
   std::map<unsigned, bool> SecIndices;
   unsigned SecIndex;
   for (StringRef Section : Sections) {
@@ -119,7 +119,7 @@ getSectionRefsByNameOrIndex(const object::ObjectFile &Obj,
   SecIndex = Obj.isELF() ? 0 : 1;
   for (object::SectionRef SecRef : Obj.sections()) {
     StringRef SecName = unwrapOrError(Obj.getFileName(), SecRef.getName());
-    auto NameIt = SecNames.find(std::string(SecName));
+    auto NameIt = SecNames.find(SecName);
     if (NameIt != SecNames.end())
       NameIt->second = true;
     auto IndexIt = SecIndices.find(SecIndex);

Copy link
Collaborator

@jh7370 jh7370 left a comment

Choose a reason for hiding this comment

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

Functionally LGTM, but the commit title is unhelpful, in my opinion. I'd suggest adding a description of "Support lookups in a section name std::map with both std::string and StringRef" or something to that effect (I'd suggest this be the title, but it's too long probably, so the description will have to do).

@kazutakahirata
Copy link
Contributor Author

Functionally LGTM, but the commit title is unhelpful, in my opinion. I'd suggest adding a description of "Support lookups in a section name std::map with both std::string and StringRef" or something to that effect (I'd suggest this be the title, but it's too long probably, so the description will have to do).

Thanks for the review! I've left "heterogenous lookup" in the title because it is a concise term used often in the C++ community, but I realize some people may not be familiar with the concept. I expanded a bit in the PR description.

@kazutakahirata kazutakahirata merged commit 3297858 into llvm:main Nov 5, 2024
10 checks passed
@kazutakahirata kazutakahirata deleted the cleanup_001_heterogenous_readobj branch November 5, 2024 17:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants