Optimize rule-matching by reordering the OutSection-Rule-Sections loop#820
Draft
parth-07 wants to merge 1 commit intoqualcomm:mainfrom
Draft
Optimize rule-matching by reordering the OutSection-Rule-Sections loop#820parth-07 wants to merge 1 commit intoqualcomm:mainfrom
parth-07 wants to merge 1 commit intoqualcomm:mainfrom
Conversation
93158a0 to
81a7bc3
Compare
This commit improves the performance of linker script rule-matching by
reordering the nested OutputSection-LinkerScriptRule-InputSections loop.
Currently, the nested loop is as follows:
```
for (auto O : OutSections) {
for (auto R : O.rules()) {
for (auto S : InputSections) {
// ...
}
}
}
```
This patch changes the nested loop to:
```
for (auto S : InputSections) {
for (auto O : OutSections) {
for (auto R : O.rules()) {
// ...
}
}
}
```
The new loop structure has the following benefits:
- Once an input section is matched to a rule, we waste no iterations
over it. On the contrary, in the old design, all the input sections
are always iterated over for EACH rule.
- We do a lot of computation for a section. In the old design, all the
computations are repeated for EACH rule, whereas in the new design all
the computations happen once-per-section in the outermost loop.
On a contrived test case that make good use of linker script
rule-matching, this patch results in ~15% performance improvement
for the rule-matching phase.
Signed-off-by: Parth Arora <partaror@qti.qualcomm.com>
81a7bc3 to
60b7158
Compare
Contributor
|
I like this approach, and potential savings. |
| for (Section *Section : Sections) { | ||
| ELFSection *ELFSect = llvm::dyn_cast<eld::ELFSection>(Section); | ||
| // For each input section. | ||
| for (Section *Section : Sections) { |
Contributor
There was a problem hiding this comment.
Change the variable Section to Sec or something like that.
| if (In->isSpecial()) | ||
| if (In->isSpecial()) { | ||
| IsRetrySect = true; | ||
| RetrySections.insert(std::make_pair(Section, true)); |
Contributor
There was a problem hiding this comment.
we dont need this map.
| InputSectionHash = llvm::hash_combine(SectName); | ||
| } | ||
| if (Section->getOutputSection() && !IsRetrySect) { | ||
| shouldBreak = false; |
Contributor
There was a problem hiding this comment.
shouldSkipMatch instead of shouldBreak ?
quic-areg
reviewed
Feb 13, 2026
| break; | ||
| } | ||
| } | ||
| bool shouldBreak = false; |
Contributor
There was a problem hiding this comment.
Suggested change
| bool shouldBreak = false; |
This shouldBreak is currently a no-op
| } | ||
| // For all output sections. | ||
| for (auto *Out : SectionMap) { | ||
| if (ELFSect) { |
Contributor
There was a problem hiding this comment.
Suggested change
| if (ELFSect) { | |
| if (Section->getOutputSection() && !IsRetrySect) | |
| break; | |
| if (ELFSect) { |
| // RuleMatchTimes[In] += std::chrono::system_clock::now() - Start; | ||
| } // end each rule | ||
| if (shouldBreak) | ||
| break; |
| } // end each output section | ||
| // RuleMatchTimes[In] += std::chrono::system_clock::now() - Start; | ||
| } // end each rule | ||
| if (shouldBreak) |
Contributor
There was a problem hiding this comment.
Suggested change
| if (shouldBreak) |
| } | ||
|
|
||
| void ObjectBuilder::assignInputFromOutput(eld::InputFile *Obj) { | ||
| std::unordered_map<Section *, bool> RetrySections; |
Contributor
There was a problem hiding this comment.
Is this still used anymore? We write to it but do we read it anywhere?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This commit improves the performance of linker script rule-matching by reordering the nested OutputSection-LinkerScriptRule-InputSections loop.
Currently, the nested loop is as follows:
This patch changes the nested loop to:
The new loop structure has the following benefits:
On a contrived test case that make good use of linker script rule-matching, this patch results in ~15% performance improvement for the rule-matching phase.