[llvm] [Offload][NFCI] Avoid temporary string copies in InfoTreeNode (PR #159372)
Alexey Sachkov via llvm-commits
llvm-commits at lists.llvm.org
Thu Sep 18 03:03:10 PDT 2025
https://github.com/AlexeySachkov updated https://github.com/llvm/llvm-project/pull/159372
>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 1/2] [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;
>From 663d2e129a3cf681c554a311ac03d7b816fe0db4 Mon Sep 17 00:00:00 2001
From: Alexey Sachkov <alexey.sachkov at intel.com>
Date: Thu, 18 Sep 2025 12:02:57 +0200
Subject: [PATCH 2/2] Apply comments: made the API non-move-only
---
offload/plugins-nextgen/common/include/PluginInterface.h | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/offload/plugins-nextgen/common/include/PluginInterface.h b/offload/plugins-nextgen/common/include/PluginInterface.h
index 799747e22e6d0..2242c6c26371e 100644
--- a/offload/plugins-nextgen/common/include/PluginInterface.h
+++ b/offload/plugins-nextgen/common/include/PluginInterface.h
@@ -192,7 +192,7 @@ struct InfoTreeNode {
llvm::DenseMap<DeviceInfo, size_t> DeviceInfoMap;
InfoTreeNode() : InfoTreeNode("", std::monostate{}, "") {}
- InfoTreeNode(std::string &&Key, VariantType Value, std::string &&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
@@ -201,7 +201,8 @@ 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(), std::string &&Units = "",
+ InfoTreeNode *add(std::string Key, T Value = T(),
+ std::string Units = std::string(),
std::optional<DeviceInfo> DeviceInfoKey = std::nullopt) {
assert(!Key.empty() && "Invalid info key");
More information about the llvm-commits
mailing list