[NFC] Optimize vector usage in lld
By using emplace_back, as well as converting some loops to for-each, we can do more efficient vectorization.
Make copy constructor for TemporaryFile noexcept.
Reviewed By: #lld-macho, int3
Differential Revision: https://reviews.llvm.org/D139552
diff --git a/lld/COFF/Chunks.cpp b/lld/COFF/Chunks.cpp
index 65de868..7ec4829 100644
--- a/lld/COFF/Chunks.cpp
+++ b/lld/COFF/Chunks.cpp
@@ -658,8 +658,7 @@
}
// sizeInBits is used to initialize the Flags field; currently no
// other flags are defined.
- res.emplace_back(
- RuntimePseudoReloc(target, this, rel.VirtualAddress, sizeInBits));
+ res.emplace_back(target, this, rel.VirtualAddress, sizeInBits);
}
}
diff --git a/lld/COFF/Driver.cpp b/lld/COFF/Driver.cpp
index edfa7f6..a927d41 100644
--- a/lld/COFF/Driver.cpp
+++ b/lld/COFF/Driver.cpp
@@ -1506,7 +1506,7 @@
}
// Construct search path list.
- searchPaths.push_back("");
+ searchPaths.emplace_back("");
for (auto *arg : args.filtered(OPT_libpath))
searchPaths.push_back(arg->getValue());
detectWinSysRoot(args);
diff --git a/lld/COFF/DriverUtils.cpp b/lld/COFF/DriverUtils.cpp
index d70f53a..c663a85 100644
--- a/lld/COFF/DriverUtils.cpp
+++ b/lld/COFF/DriverUtils.cpp
@@ -347,9 +347,7 @@
}
}
- TemporaryFile(TemporaryFile &&obj) {
- std::swap(path, obj.path);
- }
+ TemporaryFile(TemporaryFile &&obj) noexcept { std::swap(path, obj.path); }
~TemporaryFile() {
if (path.empty())
@@ -869,7 +867,7 @@
ctx.config.argv = {argv[0]};
for (opt::Arg *arg : args) {
if (arg->getOption().getKind() != opt::Option::InputClass) {
- ctx.config.argv.push_back(args.getArgString(arg->getIndex()));
+ ctx.config.argv.emplace_back(args.getArgString(arg->getIndex()));
}
}
diff --git a/lld/COFF/MinGW.cpp b/lld/COFF/MinGW.cpp
index 01372cb..1212569 100644
--- a/lld/COFF/MinGW.cpp
+++ b/lld/COFF/MinGW.cpp
@@ -267,8 +267,8 @@
// Update pointers in input files.
parallelForEach(ctx.objFileInstances, [&](ObjFile *file) {
MutableArrayRef<Symbol *> syms = file->getMutableSymbols();
- for (size_t i = 0, e = syms.size(); i != e; ++i)
- if (Symbol *s = map.lookup(syms[i]))
- syms[i] = s;
+ for (auto &sym : syms)
+ if (Symbol *s = map.lookup(sym))
+ sym = s;
});
}
diff --git a/lld/COFF/Writer.cpp b/lld/COFF/Writer.cpp
index a8fc38c..0909b14 100644
--- a/lld/COFF/Writer.cpp
+++ b/lld/COFF/Writer.cpp
@@ -488,7 +488,7 @@
uint32_t &thunkSymbolIndex = insertion.first->second;
if (insertion.second)
thunkSymbolIndex = file->addRangeThunkSymbol(thunk);
- relocReplacements.push_back({j, thunkSymbolIndex});
+ relocReplacements.emplace_back(j, thunkSymbolIndex);
}
// Get a writable copy of this section's relocations so they can be
@@ -531,8 +531,7 @@
continue;
ArrayRef<coff_relocation> relocs = sc->getRelocs();
- for (size_t j = 0, e = relocs.size(); j < e; ++j) {
- const coff_relocation &rel = relocs[j];
+ for (const coff_relocation &rel : relocs) {
Symbol *relocTarget = sc->file->getSymbol(rel.SymbolTableIndex);
Defined *sym = dyn_cast_or_null<Defined>(relocTarget);
@@ -1034,13 +1033,13 @@
// allowing a debugger to match a PDB and an executable. So we need it even
// if we're ultimately not going to write CodeView data to the PDB.
buildId = make<CVDebugRecordChunk>(ctx);
- debugRecords.push_back({COFF::IMAGE_DEBUG_TYPE_CODEVIEW, buildId});
+ debugRecords.emplace_back(COFF::IMAGE_DEBUG_TYPE_CODEVIEW, buildId);
}
if (config->cetCompat) {
- debugRecords.push_back({COFF::IMAGE_DEBUG_TYPE_EX_DLLCHARACTERISTICS,
- make<ExtendedDllCharacteristicsChunk>(
- IMAGE_DLL_CHARACTERISTICS_EX_CET_COMPAT)});
+ debugRecords.emplace_back(COFF::IMAGE_DEBUG_TYPE_EX_DLLCHARACTERISTICS,
+ make<ExtendedDllCharacteristicsChunk>(
+ IMAGE_DLL_CHARACTERISTICS_EX_CET_COMPAT));
}
// Align and add each chunk referenced by the debug data directory.
diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp
index 7dacdeb..4278bf6 100644
--- a/lld/ELF/InputFiles.cpp
+++ b/lld/ELF/InputFiles.cpp
@@ -1706,9 +1706,9 @@
// user programs can access blobs by name. Non-alphanumeric
// characters in a filename are replaced with underscore.
std::string s = "_binary_" + mb.getBufferIdentifier().str();
- for (size_t i = 0; i < s.size(); ++i)
- if (!isAlnum(s[i]))
- s[i] = '_';
+ for (char &c : s)
+ if (!isAlnum(c))
+ c = '_';
llvm::StringSaver &saver = lld::saver();
diff --git a/lld/ELF/MapFile.cpp b/lld/ELF/MapFile.cpp
index e28656c..9bda8f1 100644
--- a/lld/ELF/MapFile.cpp
+++ b/lld/ELF/MapFile.cpp
@@ -157,7 +157,7 @@
os << right_justify("VMA", w) << ' ' << right_justify("LMA", w)
<< " Size Align Out In Symbol\n";
- OutputSection* osec = nullptr;
+ OutputSection *osec = nullptr;
for (SectionCommand *cmd : script->sectionCommands) {
if (auto *assign = dyn_cast<SymbolAssignment>(cmd)) {
if (assign->provide && !assign->sym)
diff --git a/lld/ELF/OutputSections.cpp b/lld/ELF/OutputSections.cpp
index dc40f07..2251ecc 100644
--- a/lld/ELF/OutputSections.cpp
+++ b/lld/ELF/OutputSections.cpp
@@ -242,7 +242,7 @@
llvm::function_ref<int(InputSectionBase *s)> order) {
std::vector<std::pair<int, InputSection *>> v;
for (InputSection *s : in)
- v.push_back({order(s), s});
+ v.emplace_back(order(s), s);
llvm::stable_sort(v, less_first());
for (size_t i = 0; i < v.size(); ++i)
diff --git a/lld/MachO/Arch/ARM64.cpp b/lld/MachO/Arch/ARM64.cpp
index 6627d41..e28e82a 100644
--- a/lld/MachO/Arch/ARM64.cpp
+++ b/lld/MachO/Arch/ARM64.cpp
@@ -139,14 +139,14 @@
thunk->align = 4;
thunk->data = {reinterpret_cast<const uint8_t *>(thunkCode),
sizeof(thunkCode)};
- thunk->relocs.push_back({/*type=*/ARM64_RELOC_PAGEOFF12,
- /*pcrel=*/false, /*length=*/2,
- /*offset=*/4, /*addend=*/0,
- /*referent=*/funcSym});
- thunk->relocs.push_back({/*type=*/ARM64_RELOC_PAGE21,
- /*pcrel=*/true, /*length=*/2,
- /*offset=*/0, /*addend=*/0,
- /*referent=*/funcSym});
+ thunk->relocs.emplace_back(/*type=*/ARM64_RELOC_PAGEOFF12,
+ /*pcrel=*/false, /*length=*/2,
+ /*offset=*/4, /*addend=*/0,
+ /*referent=*/funcSym);
+ thunk->relocs.emplace_back(/*type=*/ARM64_RELOC_PAGE21,
+ /*pcrel=*/true, /*length=*/2,
+ /*offset=*/0, /*addend=*/0,
+ /*referent=*/funcSym);
}
ARM64::ARM64() : ARM64Common(LP64()) {
diff --git a/lld/MachO/SyntheticSections.h b/lld/MachO/SyntheticSections.h
index b17e991..8332188 100644
--- a/lld/MachO/SyntheticSections.h
+++ b/lld/MachO/SyntheticSections.h
@@ -162,7 +162,7 @@
void addEntry(const InputSection *isec, uint64_t offset) {
if (config->isPic)
- locations.push_back({isec, offset});
+ locations.emplace_back(isec, offset);
}
private:
@@ -174,7 +174,7 @@
int64_t addend;
Location target;
BindingEntry(int64_t addend, Location target)
- : addend(addend), target(std::move(target)) {}
+ : addend(addend), target(target) {}
};
template <class Sym>