Skip to content

[ObjectYAML] Avoid repeated hash lookups (NFC) #127958

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

Conversation

kazutakahirata
Copy link
Contributor

No description provided.

@llvmbot
Copy link
Member

llvmbot commented Feb 20, 2025

@llvm/pr-subscribers-objectyaml

Author: Kazu Hirata (kazutakahirata)

Changes

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

1 Files Affected:

  • (modified) llvm/lib/ObjectYAML/XCOFFEmitter.cpp (+14-12)
diff --git a/llvm/lib/ObjectYAML/XCOFFEmitter.cpp b/llvm/lib/ObjectYAML/XCOFFEmitter.cpp
index f3a9fb188f51d..5d7d6a1141ba0 100644
--- a/llvm/lib/ObjectYAML/XCOFFEmitter.cpp
+++ b/llvm/lib/ObjectYAML/XCOFFEmitter.cpp
@@ -145,14 +145,16 @@ bool XCOFFWriter::initSectionHeaders(uint64_t &CurrentOffset) {
   uint64_t CurrentEndTDataAddr = 0;
   for (uint16_t I = 0, E = InitSections.size(); I < E; ++I) {
     // Assign indices for sections.
-    if (InitSections[I].SectionName.size() &&
-        !SectionIndexMap[InitSections[I].SectionName]) {
-      // The section index starts from 1.
-      SectionIndexMap[InitSections[I].SectionName] = I + 1;
-      if ((I + 1) > MaxSectionIndex) {
-        ErrHandler("exceeded the maximum permitted section index of " +
-                   Twine(MaxSectionIndex));
-        return false;
+    if (InitSections[I].SectionName.size()) {
+      int16_t &SectionIndex = SectionIndexMap[InitSections[I].SectionName];
+      if (!SectionIndex) {
+        // The section index starts from 1.
+        SectionIndex = I + 1;
+        if ((I + 1) > MaxSectionIndex) {
+          ErrHandler("exceeded the maximum permitted section index of " +
+                     Twine(MaxSectionIndex));
+          return false;
+        }
       }
     }
 
@@ -779,19 +781,19 @@ bool XCOFFWriter::writeSymbols() {
       W.write<uint32_t>(YamlSym.Value);
     }
     if (YamlSym.SectionName) {
-      if (!SectionIndexMap.count(*YamlSym.SectionName)) {
+      auto It = SectionIndexMap.find(*YamlSym.SectionName);
+      if (It == SectionIndexMap.end()) {
         ErrHandler("the SectionName " + *YamlSym.SectionName +
                    " specified in the symbol does not exist");
         return false;
       }
-      if (YamlSym.SectionIndex &&
-          SectionIndexMap[*YamlSym.SectionName] != *YamlSym.SectionIndex) {
+      if (YamlSym.SectionIndex && It->second != *YamlSym.SectionIndex) {
         ErrHandler("the SectionName " + *YamlSym.SectionName +
                    " and the SectionIndex (" + Twine(*YamlSym.SectionIndex) +
                    ") refer to different sections");
         return false;
       }
-      W.write<int16_t>(SectionIndexMap[*YamlSym.SectionName]);
+      W.write<int16_t>(It->second);
     } else {
       W.write<int16_t>(YamlSym.SectionIndex.value_or(0));
     }

@kazutakahirata kazutakahirata merged commit c612f79 into llvm:main Feb 20, 2025
10 checks passed
@kazutakahirata kazutakahirata deleted the cleanup_001_repeated_hash_lookups_llvm_ObjectYAML branch February 20, 2025 16:56
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