Skip to content

Commit c612f79

Browse files
[ObjectYAML] Avoid repeated hash lookups (NFC) (#127958)
1 parent 506b31e commit c612f79

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

llvm/lib/ObjectYAML/XCOFFEmitter.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,16 @@ bool XCOFFWriter::initSectionHeaders(uint64_t &CurrentOffset) {
145145
uint64_t CurrentEndTDataAddr = 0;
146146
for (uint16_t I = 0, E = InitSections.size(); I < E; ++I) {
147147
// Assign indices for sections.
148-
if (InitSections[I].SectionName.size() &&
149-
!SectionIndexMap[InitSections[I].SectionName]) {
150-
// The section index starts from 1.
151-
SectionIndexMap[InitSections[I].SectionName] = I + 1;
152-
if ((I + 1) > MaxSectionIndex) {
153-
ErrHandler("exceeded the maximum permitted section index of " +
154-
Twine(MaxSectionIndex));
155-
return false;
148+
if (InitSections[I].SectionName.size()) {
149+
int16_t &SectionIndex = SectionIndexMap[InitSections[I].SectionName];
150+
if (!SectionIndex) {
151+
// The section index starts from 1.
152+
SectionIndex = I + 1;
153+
if ((I + 1) > MaxSectionIndex) {
154+
ErrHandler("exceeded the maximum permitted section index of " +
155+
Twine(MaxSectionIndex));
156+
return false;
157+
}
156158
}
157159
}
158160

@@ -779,19 +781,19 @@ bool XCOFFWriter::writeSymbols() {
779781
W.write<uint32_t>(YamlSym.Value);
780782
}
781783
if (YamlSym.SectionName) {
782-
if (!SectionIndexMap.count(*YamlSym.SectionName)) {
784+
auto It = SectionIndexMap.find(*YamlSym.SectionName);
785+
if (It == SectionIndexMap.end()) {
783786
ErrHandler("the SectionName " + *YamlSym.SectionName +
784787
" specified in the symbol does not exist");
785788
return false;
786789
}
787-
if (YamlSym.SectionIndex &&
788-
SectionIndexMap[*YamlSym.SectionName] != *YamlSym.SectionIndex) {
790+
if (YamlSym.SectionIndex && It->second != *YamlSym.SectionIndex) {
789791
ErrHandler("the SectionName " + *YamlSym.SectionName +
790792
" and the SectionIndex (" + Twine(*YamlSym.SectionIndex) +
791793
") refer to different sections");
792794
return false;
793795
}
794-
W.write<int16_t>(SectionIndexMap[*YamlSym.SectionName]);
796+
W.write<int16_t>(It->second);
795797
} else {
796798
W.write<int16_t>(YamlSym.SectionIndex.value_or(0));
797799
}

0 commit comments

Comments
 (0)