[ELF] --gdb-index: fix memory usage regression after D74773
authorFangrui Song <maskray@google.com>
Thu, 12 Mar 2020 23:44:47 +0000 (16:44 -0700)
committerFangrui Song <maskray@google.com>
Thu, 12 Mar 2020 23:55:30 +0000 (16:55 -0700)
On an internal target,

* Before D74773: time -f '%M' => 18275680
* After D74773:  time -f '%M' => 22088964

This patch restores to the status before D74773.

lld/ELF/SyntheticSections.cpp

index c69becb..ed2cdb7 100644 (file)
@@ -2822,14 +2822,16 @@ template <class ELFT> GdbIndexSection *GdbIndexSection::create() {
   std::vector<std::vector<NameAttrEntry>> nameAttrs(sections.size());
 
   parallelForEachN(0, sections.size(), [&](size_t i) {
-    DWARFContext *dwarf =
-        sections[i]->getFile<ELFT>()->getDwarf()->getContext();
+    // To keep memory usage low, we don't want to keep cached DWARFContext, so
+    // avoid getDwarf() here.
+    ObjFile<ELFT> *file = sections[i]->getFile<ELFT>();
+    DWARFContext dwarf(std::make_unique<LLDDwarfObj<ELFT>>(file));
 
     chunks[i].sec = sections[i];
-    chunks[i].compilationUnits = readCuList(*dwarf);
-    chunks[i].addressAreas = readAddressAreas(*dwarf, sections[i]);
+    chunks[i].compilationUnits = readCuList(dwarf);
+    chunks[i].addressAreas = readAddressAreas(dwarf, sections[i]);
     nameAttrs[i] = readPubNamesAndTypes<ELFT>(
-        static_cast<const LLDDwarfObj<ELFT> &>(dwarf->getDWARFObj()),
+        static_cast<const LLDDwarfObj<ELFT> &>(dwarf.getDWARFObj()),
         chunks[i].compilationUnits);
   });