[llvm] [Offload][NFCI] Avoid temporary string copies in InfoTreeNode (PR #159372)

Alexey Sachkov via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 17 07:27:36 PDT 2025


https://github.com/AlexeySachkov created https://github.com/llvm/llvm-project/pull/159372

It looks like all all sites of `InfoTreeNode::add` pass string literals as `Key` and `Units`, so we could mandate those to be rvalue-references to move them directly into the final location, avoiding temporary copies.

>From 7183afa08dd09b81c15942cef2fae49711e7393f Mon Sep 17 00:00:00 2001
From: Alexey Sachkov <alexey.sachkov at intel.com>
Date: Wed, 17 Sep 2025 16:17:06 +0200
Subject: [PATCH] [Offload][NFCI] Avoid temporary string copies in InfoTreeNode

It looks like all all sites of `InfoTreeNode::add` pass string literals
as `Key` and `Units`, so we could mandate those to be rvalue-references
to move them directly into the final location, avoiding temporary
copies.
---
 .../plugins-nextgen/common/include/PluginInterface.h   | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/offload/plugins-nextgen/common/include/PluginInterface.h b/offload/plugins-nextgen/common/include/PluginInterface.h
index ce66d277d6187..799747e22e6d0 100644
--- a/offload/plugins-nextgen/common/include/PluginInterface.h
+++ b/offload/plugins-nextgen/common/include/PluginInterface.h
@@ -192,8 +192,8 @@ struct InfoTreeNode {
   llvm::DenseMap<DeviceInfo, size_t> DeviceInfoMap;
 
   InfoTreeNode() : InfoTreeNode("", std::monostate{}, "") {}
-  InfoTreeNode(std::string Key, VariantType Value, std::string Units)
-      : Key(Key), Value(Value), Units(Units) {}
+  InfoTreeNode(std::string &&Key, VariantType Value, std::string &&Units)
+      : Key(std::move(Key)), Value(Value), Units(std::move(Units)) {}
 
   /// Add a new info entry as a child of this node. The entry requires at least
   /// a key string in \p Key. The value in \p Value is optional and can be any
@@ -201,8 +201,7 @@ struct InfoTreeNode {
   /// and must be a string. Providing a device info key allows liboffload to
   /// use that value for an appropriate olGetDeviceInfo query
   template <typename T = std::monostate>
-  InfoTreeNode *add(std::string Key, T Value = T(),
-                    const std::string &Units = std::string(),
+  InfoTreeNode *add(std::string &&Key, T Value = T(), std::string &&Units = "",
                     std::optional<DeviceInfo> DeviceInfoKey = std::nullopt) {
     assert(!Key.empty() && "Invalid info key");
 
@@ -217,7 +216,8 @@ struct InfoTreeNode {
     else
       ValueVariant = std::string{Value};
 
-    auto Ptr = &Children->emplace_back(Key, ValueVariant, Units);
+    auto Ptr =
+        &Children->emplace_back(std::move(Key), ValueVariant, std::move(Units));
 
     if (DeviceInfoKey)
       DeviceInfoMap[*DeviceInfoKey] = Children->size() - 1;



More information about the llvm-commits mailing list