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

Kazu Hirata via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 19 21:20:44 PST 2025


https://github.com/kazutakahirata created https://github.com/llvm/llvm-project/pull/127958

None

>From f53dc00a383de9d6aa911d5d593284055f85bb89 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Wed, 19 Feb 2025 08:26:01 -0800
Subject: [PATCH] [ObjectYAML] Avoid repeated hash lookups (NFC)

---
 llvm/lib/ObjectYAML/XCOFFEmitter.cpp | 26 ++++++++++++++------------
 1 file changed, 14 insertions(+), 12 deletions(-)

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));
     }



More information about the llvm-commits mailing list