-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[Offload][NFC] Avoid temporary string copies in InfoTreeNode #159372
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Offload][NFC] Avoid temporary string copies in InfoTreeNode #159372
Conversation
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.
|
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 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. |
|
@llvm/pr-subscribers-offload Author: Alexey Sachkov (AlexeySachkov) ChangesIt looks like all all sites of Full diff: https://github.com/llvm/llvm-project/pull/159372.diff 1 Files Affected:
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
|
@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). |
|
@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! |
No description provided.