[LLD][COFF] Avoid repeated vector insertion during thunk creation (up to 10x improvement for large links) #241059
<!--LLVM NEW CONTRIBUTOR COMMENT-->
Hello @kfarnung :wave:
Thank you for submitting a Pull Request (PR) to the LLVM Project. Since this is your first PR, here are a few useful links covering our main contribution policies and review practices.
All contributions to LLVM must follow our LLVM AI Tool Use Policy. In particular, if you used AI while working on this PR, remember to add a note to the PR description.
The LLVM Code-Review Policy and Practices document contains practical information about the PR process, including how patches are reviewed and accepted, and who can review a PR.
Please reply to this message to confirm that you have read these policies, especially the LLVM AI Tool Use Policy, and that any AI tool usage has been noted in the PR description.
---
Frequently asked questions
How do I add reviewers?
This PR will be automatically labeled, and the relevant teams will be notified. For some parts of the project, reviewers may also be added automatically.
You can also add reviewers manually using the Reviewers section on this page. If you cannot use that section, it is probably because you do not have write permissions for the repository. In that case, you can request a review by tagging reviewers in a comment using @ followed by their GitHub username.
What if there are no comments?
If you have not received any comments on your PR after a week, you can request a review by pinging the PR with a comment such as “Ping”. The common courtesy ping rate is once a week. Please remember that you are asking for volunteer time from other developers.
Are any special GitHub settings required to contribute to LLVM?
We only require contributors to have a public email address associated with their GitHub commits, see this section of LLVM Developer Policy for details.
---
If you have questions, feel free to leave a comment on this PR, or ask on LLVM Discord or LLVM Discourse.
Thank you,
The LLVM Community
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-lld-coff
@llvm/pr-subscribers-lld
Author: Kyle Farnung (kfarnung)
<details>
<summary>Changes</summary>
Inserting each range-extension thunk into OutputSection::chunks shifts
the remaining chunk pointers. When many chunks need thunks, this makes
chunk-list construction quadratic.
Append the original chunks and their thunks to a new vector and swap it
into the output section, preserving their order and the existing thunk
placement, reuse, relocation rewriting, and retry policy. Newly created
range-extension thunks need no additional visit because they inherit the
no-op extendRanges implementation.
Tested with ninja -C build check-lld-coff: 559 passed, 6 unsupported.
Assisted-by: GitHub Copilot CLI (GPT-6.1 Sol)
---
Full diff: https://github.com/llvm/llvm-project/pull/241059.diff
1 Files Affected:
````````````diff --git a/lld/COFF/Writer.cpp b/lld/COFF/Writer.cpp
index 3cab426df55cf..c66b44c3597d0 100644
--- a/lld/COFF/Writer.cpp
+++ b/lld/COFF/Writer.cpp
@@ -491,12 +491,14 @@ bool Writer::createThunks(OutputSection *os, int margin) {
DenseMap<uint64_t, Defined *> lastThunks;
DenseMap<std::pair<ObjFile *, Defined *>, uint32_t> thunkSymtabIndices;
size_t thunksSize = 0;
- // Recheck Chunks.size() each iteration, since we can insert more
- // elements into it.
- for (size_t i = 0; i != os->chunks.size(); ++i) {
- SectionChunk *sc = dyn_cast<SectionChunk>(os->chunks[i]);
+ // Append thunks without repeatedly shifting the remaining chunks.
+ std::vector<Chunk *> newChunks;
+ newChunks.reserve(os->chunks.size());
+ for (Chunk *c : os->chunks) {
+ newChunks.push_back(c);
+ SectionChunk *sc = dyn_cast<SectionChunk>(c);
if (!sc) {
- auto chunk = cast<NonSectionChunk>(os->chunks[i]);
+ auto chunk = cast<NonSectionChunk>(c);
if (uint32_t size = chunk->extendRanges()) {
thunksSize += size;
addressesChanged = true;
@@ -504,7 +506,6 @@ bool Writer::createThunks(OutputSection *os, int margin) {
continue;
}
MachineTypes machine = sc->getMachine();
- size_t thunkInsertionSpot = i + 1;
// Try to get a good enough estimate of where new thunks will be placed.
// Offset this by the size of the new thunks added so far, to make the
@@ -540,8 +541,7 @@ bool Writer::createThunks(OutputSection *os, int margin) {
Chunk *thunkChunk = thunk->getChunk();
thunkChunk->setRVA(
thunkInsertionRVA); // Estimate of where it will be located.
- os->chunks.insert(os->chunks.begin() + thunkInsertionSpot, thunkChunk);
- thunkInsertionSpot++;
+ newChunks.push_back(thunkChunk);
thunksSize += thunkChunk->getSize();
thunkInsertionRVA += thunkChunk->getSize();
addressesChanged = true;
@@ -586,6 +586,7 @@ bool Writer::createThunks(OutputSection *os, int margin) {
sc->setRelocs(newRelocs);
}
+ os->chunks.swap(newChunks);
return addressesChanged;
}
</details>
LGTM, thanks!
<!--LLVM BUILDBOT INFORMATION COMMENT-->
@kfarnung Congratulations on having your first Pull Request (PR) merged into the LLVM Project!
Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.
Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.
How to do this, and the rest of the post-merge process, is covered in detail here.
If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.
If you don't get any reports, no action is required from you. Your changes are working as expected, well done!
mstorsjo
kfarnung
Inserting each range-extension thunk into OutputSection::chunks shifts
the remaining chunk pointers. When many chunks need thunks, this makes
chunk-list construction quadratic.
Append the original chunks and their thunks to a new vector and swap it
into the output section, preserving their order and the existing thunk
placement, reuse, relocation rewriting, and retry policy. Newly created
range-extension thunks need no additional visit because they inherit the
no-op extendRanges implementation.
On my x64 Windows machine it reduces a large ARM64 debug link from 43
minutes down to 2 minutes.
Tested with ninja -C build check-lld-coff: 559 passed, 6 unsupported.
Assisted-by: GitHub Copilot CLI (GPT-6.1 Sol)