Skip to content

Conversation

@AlexeySachkov
Copy link
Contributor

@AlexeySachkov AlexeySachkov commented Sep 17, 2025

No description provided.

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.
@github-actions
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Member

llvmbot commented Sep 17, 2025

@llvm/pr-subscribers-offload

Author: Alexey Sachkov (AlexeySachkov)

Changes

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

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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
InfoTreeNode(std::string &&Key, VariantType Value, std::string &&Units)
InfoTreeNode(std::string Key, VariantType Value, std::string Units)

I'm pretty sure the typical idiom is to pass-by-value and then move. This should move an r-value into Key and then move it into the member. What you have is technically correct for moving, but makes the interface 'move only' which isn't as great.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What you have is technically correct for moving, but makes the interface 'move only' which isn't as great.

Ok, I see. I postponed the std::string construction till the very last moment, but what we could do is to construct it as early as possible and then just move it "internally" since we already operate on a copy and that would make the interface more flexible (i.e. non-'move-only'), right?

Does this apply to the constructor as well, or only to the add method?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understood it correctly, the concern should be resolved in 663d2e1

@RossBrunton RossBrunton self-requested a review September 17, 2025 15:27
@AlexeySachkov AlexeySachkov changed the title [Offload][NFCI] Avoid temporary string copies in InfoTreeNode [Offload][NFC] Avoid temporary string copies in InfoTreeNode Sep 18, 2025
@AlexeySachkov
Copy link
Contributor Author

@jhuber6, @RossBrunton, I don't have merge permissions, so could you please help with the merge?

I can do local rebase, or "squash and merge" could be used in the GH UI. I think that the PR title is self-explanatory and the final commit message can be left empty (unless there are objections to that).

@jhuber6 jhuber6 enabled auto-merge (squash) September 23, 2025 17:21
@jhuber6 jhuber6 merged commit bb58464 into llvm:main Sep 23, 2025
9 checks passed
@github-actions
Copy link

@AlexeySachkov Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

@AlexeySachkov AlexeySachkov deleted the private/asachkov/optimize-infotreenode-add branch September 23, 2025 18:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants