Skip to content

Commit 4709bac

Browse files
committed
[ELF] Avoid std::stable_partition which may allocate memory. NFC
1 parent 99a2d94 commit 4709bac

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

lld/ELF/Writer.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1836,18 +1836,22 @@ static void removeUnusedSyntheticSections() {
18361836
})
18371837
.base();
18381838

1839-
// Mark unused synthetic sections for deletion
1840-
auto end = std::stable_partition(start, inputSections.end(),
1841-
[&](InputSectionBase *s) {
1842-
auto *sec = cast<SyntheticSection>(s);
1843-
return sec->getParent() && sec->isNeeded();
1844-
});
1839+
// Remove unused synthetic sections from inputSections;
1840+
DenseSet<InputSectionBase *> unused;
1841+
auto end =
1842+
std::remove_if(start, inputSections.end(), [&](InputSectionBase *s) {
1843+
auto *sec = cast<SyntheticSection>(s);
1844+
if (sec->getParent() && sec->isNeeded())
1845+
return false;
1846+
unused.insert(sec);
1847+
return true;
1848+
});
1849+
inputSections.erase(end, inputSections.end());
18451850

18461851
// Remove unused synthetic sections from the corresponding input section
18471852
// description and orphanSections.
1848-
DenseSet<InputSectionBase *> unused(end, inputSections.end());
1849-
for (auto it = end; it != inputSections.end(); ++it)
1850-
if (OutputSection *osec = cast<SyntheticSection>(*it)->getParent())
1853+
for (auto *sec : unused)
1854+
if (OutputSection *osec = cast<SyntheticSection>(sec)->getParent())
18511855
for (SectionCommand *cmd : osec->commands)
18521856
if (auto *isd = dyn_cast<InputSectionDescription>(cmd))
18531857
llvm::erase_if(isd->sections, [&](InputSection *isec) {
@@ -1856,9 +1860,6 @@ static void removeUnusedSyntheticSections() {
18561860
llvm::erase_if(script->orphanSections, [&](const InputSectionBase *sec) {
18571861
return unused.count(sec);
18581862
});
1859-
1860-
// Erase unused synthetic sections.
1861-
inputSections.erase(end, inputSections.end());
18621863
}
18631864

18641865
// Create output section objects and add them to OutputSections.

0 commit comments

Comments
 (0)