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

via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 17 07:28:35 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-offload

Author: Alexey Sachkov (AlexeySachkov)

<details>
<summary>Changes</summary>

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.

---
Full diff: https://github.com/llvm/llvm-project/pull/159372.diff


1 Files Affected:

- (modified) offload/plugins-nextgen/common/include/PluginInterface.h (+5-5) 


``````````diff
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;

``````````

</details>


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


More information about the llvm-commits mailing list