[lld] [lld] Add explicit std::move(...) to avoid a few vector copies (PR #180474)

via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 9 03:34:55 PST 2026


https://github.com/serge-sans-paille updated https://github.com/llvm/llvm-project/pull/180474

>From 6baa408fb5a191a33d29ab88d6cc2323e9e461da Mon Sep 17 00:00:00 2001
From: serge-sans-paille <sguelton at mozilla.com>
Date: Mon, 9 Feb 2026 08:38:39 +0100
Subject: [PATCH 1/2] [lld] Add explicit std::move(...) to avoid a few vector
 copies

In corner cases, it is profitable to move an llvm::SmallString instead of
copying it.

It is almost always profitable to move an std::vector

Changes suggested by performance-use-std-move from https://github.com/llvm/llvm-project/pull/179467
---
 lld/COFF/Driver.cpp         | 2 +-
 lld/wasm/OutputSections.cpp | 2 +-
 lld/wasm/Writer.cpp         | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/lld/COFF/Driver.cpp b/lld/COFF/Driver.cpp
index 3bc9c98bcdbc3..699d53ca6d2e3 100644
--- a/lld/COFF/Driver.cpp
+++ b/lld/COFF/Driver.cpp
@@ -1339,7 +1339,7 @@ void LinkerDriver::parsePDBAltPath() {
     cursor = secondMark + 1;
   }
 
-  ctx.config.pdbAltPath = buf;
+  ctx.config.pdbAltPath = std::move(buf);
 }
 
 /// Convert resource files and potentially merge input resource object
diff --git a/lld/wasm/OutputSections.cpp b/lld/wasm/OutputSections.cpp
index 8ccd38f7895cb..d6348e459d31e 100644
--- a/lld/wasm/OutputSections.cpp
+++ b/lld/wasm/OutputSections.cpp
@@ -232,7 +232,7 @@ void CustomSection::finalizeInputSections() {
     return;
 
   mergedSection->finalizeContents();
-  inputSections = newSections;
+  inputSections = std::move(newSections);
 }
 
 void CustomSection::finalizeContents() {
diff --git a/lld/wasm/Writer.cpp b/lld/wasm/Writer.cpp
index a184cdbdfefa8..40a5832056087 100644
--- a/lld/wasm/Writer.cpp
+++ b/lld/wasm/Writer.cpp
@@ -1109,7 +1109,7 @@ void Writer::combineOutputSegments() {
     }
   }
 
-  segments = newSegments;
+  segments = std::move(newSegments);
 }
 
 static void createFunction(DefinedFunction *func, StringRef bodyContent) {

>From 42a46784b56288e9b71462fec3ec80678a189b44 Mon Sep 17 00:00:00 2001
From: serge-sans-paille <sguelton at mozilla.com>
Date: Mon, 9 Feb 2026 12:28:57 +0100
Subject: [PATCH 2/2] fixup! [lld] Add explicit std::move(...) to avoid a few
 vector copies

---
 lld/COFF/Driver.cpp | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/lld/COFF/Driver.cpp b/lld/COFF/Driver.cpp
index 699d53ca6d2e3..5eb6e20398e5f 100644
--- a/lld/COFF/Driver.cpp
+++ b/lld/COFF/Driver.cpp
@@ -1295,7 +1295,9 @@ static void findKeepUniqueSections(COFFLinkerContext &ctx) {
 // lld only supports %_PDB% and %_EXT% and warns on references to all other env
 // vars.
 void LinkerDriver::parsePDBAltPath() {
-  SmallString<128> buf;
+  auto &pdbAltPath = ctx.config.pdbAltPath;
+  pdbAltPath.clear();
+
   StringRef pdbBasename =
       sys::path::filename(ctx.config.pdbPath, sys::path::Style::windows);
   StringRef binaryExtension =
@@ -1317,29 +1319,27 @@ void LinkerDriver::parsePDBAltPath() {
         (secondMark = ctx.config.pdbAltPath.find('%', firstMark + 1)) ==
             StringRef::npos) {
       // Didn't find another full fragment, treat rest of string as literal.
-      buf.append(ctx.config.pdbAltPath.substr(cursor));
+      pdbAltPath.append(ctx.config.pdbAltPath.substr(cursor));
       break;
     }
 
     // Found a full fragment. Append text in front of first %, and interpret
     // text between first and second % as variable name.
-    buf.append(ctx.config.pdbAltPath.substr(cursor, firstMark - cursor));
+    pdbAltPath.append(ctx.config.pdbAltPath.substr(cursor, firstMark - cursor));
     StringRef var =
         ctx.config.pdbAltPath.substr(firstMark, secondMark - firstMark + 1);
     if (var.equals_insensitive("%_pdb%"))
-      buf.append(pdbBasename);
+      pdbAltPath.append(pdbBasename);
     else if (var.equals_insensitive("%_ext%"))
-      buf.append(binaryExtension);
+      pdbAltPath.append(binaryExtension);
     else {
       Warn(ctx) << "only %_PDB% and %_EXT% supported in /pdbaltpath:, keeping "
                 << var << " as literal";
-      buf.append(var);
+      pdbAltPath.append(var);
     }
 
     cursor = secondMark + 1;
   }
-
-  ctx.config.pdbAltPath = std::move(buf);
 }
 
 /// Convert resource files and potentially merge input resource object



More information about the llvm-commits mailing list