fix(c++): use std::move to reduce a copy#843
Conversation
There was a problem hiding this comment.
Pull request overview
This PR targets small C++ performance cleanups in Status construction in GraphAr’s C++ runtime, aligning with issue #842’s goal of reducing unnecessary string copies in status.h.
Changes:
- Change
Status(StatusCode, const std::string&)to takestd::stringby value and move into internal storage. - Simplify
Status::OK()to return{}. - Adjust
Status::FromArgs(...)to move theStringBuilder(...)result into the constructor.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
cpp/src/graphar/status.h
Outdated
| template <typename... Args> | ||
| static Status FromArgs(StatusCode code, Args... args) { | ||
| return Status(code, util::StringBuilder(std::forward<Args>(args)...)); | ||
| return Status(code, std::move(util::StringBuilder(std::forward<Args>(args)...))); |
There was a problem hiding this comment.
std::move on the temporary returned by util::StringBuilder(...) is counterproductive here: it forces materialization of the temporary and can inhibit copy elision into the by-value Status constructor parameter, potentially adding an extra move (and for SSO strings, possibly a copy). Passing the return value directly should be at least as efficient and keeps the intent clearer.
| return Status(code, std::move(util::StringBuilder(std::forward<Args>(args)...))); | |
| return Status(code, util::StringBuilder(std::forward<Args>(args)...)); |
| inline static Status OK() { return {}; } | ||
|
|
||
| template <typename... Args> | ||
| static Status FromArgs(StatusCode code, Args... args) { |
There was a problem hiding this comment.
FromArgs takes its parameter pack by value (Args... args), which defeats the perfect-forwarding used by IOError/KeyError/... and can introduce extra copies of the formatting arguments. Consider changing it to Args&&... args and forwarding those into util::StringBuilder to preserve the callers’ value categories.
| static Status FromArgs(StatusCode code, Args... args) { | |
| static Status FromArgs(StatusCode code, Args&&... args) { |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #843 +/- ##
=========================================
Coverage 80.21% 80.21%
Complexity 615 615
=========================================
Files 93 93
Lines 10265 10265
Branches 1047 1049 +2
=========================================
Hits 8234 8234
Misses 1791 1791
Partials 240 240
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
70f60e4 to
770d4d2
Compare
|
@yangxk1y could you please review my code |
Reason for this PR
I found a potential improvemnt spot in status.h. issue: #842
What changes are included in this PR?
Are these changes tested?
Are there any user-facing changes?