[lld] [lld-macho] Reduce memory usage of printing thunks in map file (PR #122785)

Ellis Hoag via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 14 10:30:01 PST 2025


================
@@ -217,15 +203,32 @@ void macho::writeMapFile() {
                    seg->name.str().c_str(), osec->name.str().c_str());
     }
 
-  // Shared function to print an array of symbols.
-  auto printIsecArrSyms = [&](const std::vector<ConcatInputSection *> &arr) {
-    for (const ConcatInputSection *isec : arr) {
-      for (Defined *sym : isec->symbols) {
-        if (!(isPrivateLabel(sym->getName()) && getSymSizeForMap(sym) == 0))
-          os << format("0x%08llX\t0x%08llX\t[%3u] %s\n", sym->getVA(),
-                       getSymSizeForMap(sym),
-                       readerToFileOrdinal[sym->getFile()],
-                       sym->getName().str().data());
+  // Helper lambda that prints all symbols from one ConcatInputSection.
+  auto printOne = [&](const ConcatInputSection *isec) {
+    for (Defined *sym : isec->symbols) {
+      if (!(isPrivateLabel(sym->getName()) && getSymSizeForMap(sym) == 0)) {
+        os << format("0x%08llX\t0x%08llX\t[%3u] %s\n", sym->getVA(),
+                     getSymSizeForMap(sym),
+                     readerToFileOrdinal.lookup(sym->getFile()),
+                     sym->getName().str().data());
+      }
+    }
+  };
+  // Shared function to print one or two arrays of ConcatInputSection in
+  // ascending outSecOff order. The second array is optional; if provided, we
+  // interleave the printing in sorted order without allocating a merged temp
+  // array.
+  auto printIsecArrSyms = [&](ArrayRef<ConcatInputSection *> arr1,
+                              ArrayRef<ConcatInputSection *> arr2 = {}) {
+    size_t i = 0, j = 0;
+    size_t size1 = arr1.size();
+    size_t size2 = arr2.size();
+    while (i < size1 || j < size2) {
+      if (i < size1 &&
+          (j >= size2 || arr1[i]->outSecOff <= arr2[j]->outSecOff)) {
+        printOne(arr1[i++]);
+      } else if (j < size2) {
+        printOne(arr2[j++]);
----------------
ellishg wrote:

```suggestion
    while (arr1.size() || arr2.size()) {
      if (arr1.size() &&
          (arr2.empty() || arr1.front()->outSecOff <= arr2.front()->outSecOff)) {
        printOne(arr1.front());
        arr1 = arr1.drop_front();
      } else { // arr2.size() should always be positive here
        printOne(arr2.front());
        arr2 = arr2.drop_front();
```

https://github.com/llvm/llvm-project/pull/122785


More information about the llvm-commits mailing list