Skip to content

Commit 2f9a80b

Browse files
authored
[MC][NFC] Make ELFUniquingMap a StringMap (#95006)
This avoid std::map, which is slow, and uses a StringMap. Section name, group name, linked-to name and unique id are encoded into the key for fast lookup. This gives a measurable performance boost (>3%) for applications that compile many small object files (e.g., functions in JIT compilers).
1 parent 7722082 commit 2f9a80b

File tree

2 files changed

+38
-36
lines changed

2 files changed

+38
-36
lines changed

llvm/include/llvm/MC/MCContext.h

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -252,31 +252,6 @@ class MCContext {
252252
/// A collection of MCPseudoProbe in the current module
253253
MCPseudoProbeTable PseudoProbeTable;
254254

255-
// Sections are differentiated by the quadruple (section_name, group_name,
256-
// unique_id, link_to_symbol_name). Sections sharing the same quadruple are
257-
// combined into one section.
258-
struct ELFSectionKey {
259-
std::string SectionName;
260-
StringRef GroupName;
261-
StringRef LinkedToName;
262-
unsigned UniqueID;
263-
264-
ELFSectionKey(StringRef SectionName, StringRef GroupName,
265-
StringRef LinkedToName, unsigned UniqueID)
266-
: SectionName(SectionName), GroupName(GroupName),
267-
LinkedToName(LinkedToName), UniqueID(UniqueID) {}
268-
269-
bool operator<(const ELFSectionKey &Other) const {
270-
if (SectionName != Other.SectionName)
271-
return SectionName < Other.SectionName;
272-
if (GroupName != Other.GroupName)
273-
return GroupName < Other.GroupName;
274-
if (int O = LinkedToName.compare(Other.LinkedToName))
275-
return O < 0;
276-
return UniqueID < Other.UniqueID;
277-
}
278-
};
279-
280255
struct COFFSectionKey {
281256
std::string SectionName;
282257
StringRef GroupName;
@@ -350,8 +325,8 @@ class MCContext {
350325
};
351326

352327
StringMap<MCSectionMachO *> MachOUniquingMap;
353-
std::map<ELFSectionKey, MCSectionELF *> ELFUniquingMap;
354328
std::map<COFFSectionKey, MCSectionCOFF *> COFFUniquingMap;
329+
StringMap<MCSectionELF *> ELFUniquingMap;
355330
std::map<std::string, MCSectionGOFF *> GOFFUniquingMap;
356331
std::map<WasmSectionKey, MCSectionWasm *> WasmUniquingMap;
357332
std::map<XCOFFSectionKey, MCSectionXCOFF *> XCOFFUniquingMap;

llvm/lib/MC/MCContext.cpp

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#include "llvm/MC/SectionKind.h"
4545
#include "llvm/Support/Casting.h"
4646
#include "llvm/Support/CommandLine.h"
47+
#include "llvm/Support/EndianStream.h"
4748
#include "llvm/Support/ErrorHandling.h"
4849
#include "llvm/Support/MemoryBuffer.h"
4950
#include "llvm/Support/Path.h"
@@ -548,16 +549,42 @@ MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type,
548549
if (GroupSym)
549550
Group = GroupSym->getName();
550551
assert(!(LinkedToSym && LinkedToSym->getName().empty()));
551-
// Do the lookup, if we have a hit, return it.
552-
auto IterBool = ELFUniquingMap.insert(std::make_pair(
553-
ELFSectionKey{Section.str(), Group,
554-
LinkedToSym ? LinkedToSym->getName() : "", UniqueID},
555-
nullptr));
556-
auto &Entry = *IterBool.first;
557-
if (!IterBool.second)
558-
return Entry.second;
559552

560-
StringRef CachedName = Entry.first.SectionName;
553+
// Sections are differentiated by the quadruple (section_name, group_name,
554+
// unique_id, link_to_symbol_name). Sections sharing the same quadruple are
555+
// combined into one section. As an optimization, non-unique sections without
556+
// group or linked-to symbol have a shorter unique-ing key.
557+
std::pair<StringMap<MCSectionELF *>::iterator, bool> EntryNewPair;
558+
// Length of the section name, which are the first SectionLen bytes of the key
559+
unsigned SectionLen;
560+
if (GroupSym || LinkedToSym || UniqueID != MCSection::NonUniqueID) {
561+
SmallString<128> Buffer;
562+
Section.toVector(Buffer);
563+
SectionLen = Buffer.size();
564+
Buffer.push_back(0); // separator which cannot occur in the name
565+
if (GroupSym)
566+
Buffer.append(GroupSym->getName());
567+
Buffer.push_back(0); // separator which cannot occur in the name
568+
if (LinkedToSym)
569+
Buffer.append(LinkedToSym->getName());
570+
support::endian::write(Buffer, UniqueID, endianness::native);
571+
StringRef UniqueMapKey = StringRef(Buffer);
572+
EntryNewPair = ELFUniquingMap.insert(std::make_pair(UniqueMapKey, nullptr));
573+
} else if (!Section.isSingleStringRef()) {
574+
SmallString<128> Buffer;
575+
SectionLen = Buffer.size();
576+
StringRef UniqueMapKey = Section.toStringRef(Buffer);
577+
EntryNewPair = ELFUniquingMap.insert(std::make_pair(UniqueMapKey, nullptr));
578+
} else {
579+
SectionLen = Section.getSingleStringRef().size();
580+
StringRef UniqueMapKey = Section.getSingleStringRef();
581+
EntryNewPair = ELFUniquingMap.insert(std::make_pair(UniqueMapKey, nullptr));
582+
}
583+
584+
if (!EntryNewPair.second)
585+
return EntryNewPair.first->second;
586+
587+
StringRef CachedName = EntryNewPair.first->getKey().take_front(SectionLen);
561588

562589
SectionKind Kind;
563590
if (Flags & ELF::SHF_ARM_PURECODE)
@@ -601,7 +628,7 @@ MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type,
601628
MCSectionELF *Result =
602629
createELFSectionImpl(CachedName, Type, Flags, Kind, EntrySize, GroupSym,
603630
IsComdat, UniqueID, LinkedToSym);
604-
Entry.second = Result;
631+
EntryNewPair.first->second = Result;
605632

606633
recordELFMergeableSectionInfo(Result->getName(), Result->getFlags(),
607634
Result->getUniqueID(), Result->getEntrySize());

0 commit comments

Comments
 (0)