-
Notifications
You must be signed in to change notification settings - Fork 1.5k
ValueFlow: avoid various unnecessary copies #7583
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
base: main
Are you sure you want to change the base?
Conversation
Mostly taken from #6756 and discovered by reviewing the code. Still needs to be profiled. |
42d3603
to
762ebed
Compare
Not much in terms of performance which is not surprising since these were found by review and not by profiling. |
lib/valueflow.cpp
Outdated
{ | ||
if (!precedes(start, end)) | ||
throw InternalError(var->nameToken(), "valueFlowForwardConst: start token does not precede the end token."); | ||
for (Token* tok = start; tok != end; tok = tok->next()) { | ||
if (tok->varId() == var->declarationId()) { | ||
for (const ValueFlow::Value& value : values) | ||
setTokenValue(tok, value, settings); | ||
for (ValueFlow::Value& value : values) |
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 more then one token has matching varid and this inner loop is executed twice then you will move values that are already moved.
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.
Good catch. I was relying on the selfcheck/clang-tidy to catch these things.
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.
Dropped . And I filed https://trac.cppcheck.net/ticket/13974 about detecting this.
@@ -3155,10 +3155,10 @@ static void valueFlowLifetime(TokenList &tokenlist, ErrorLogger &errorLogger, co | |||
else if (tok->isUnaryOp("&")) { | |||
if (Token::simpleMatch(tok->astParent(), "*")) | |||
continue; | |||
for (const ValueFlow::LifetimeToken& lt : ValueFlow::getLifetimeTokens(tok->astOperand1(), settings)) { | |||
for (ValueFlow::LifetimeToken& lt : ValueFlow::getLifetimeTokens(tok->astOperand1(), settings)) { |
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.
This should be written as for (ValueFlow::LifetimeToken&& lt : ValueFlow::getLifetimeTokens(tok->astOperand1(), settings))
, although I think you might need to use move_iterators.
if (!settings.certainty.isEnabled(Certainty::inconclusive) && lt.inconclusive) | ||
continue; | ||
ErrorPath errorPath = lt.errorPath; | ||
ErrorPath& errorPath = lt.errorPath; |
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.
This should be written as ErrorPath&& errorPath = std::move(lt.errorPath)
.
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.
That would require two moves in the code so wouldn't that pessimize the code? Need to look into this. See also a somewhat related discussion I recently stumbled on: llvm/llvm-project#94798.
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.
That would require two moves in the code so wouldn't that pessimize the code?
Not its not. std::move
is just a cast. A move will happen when its passed to a value parameter. Another std::move
will still be needed since an rvalue reference variable will be an lvalue reference instead. So you will need to keep the std::move(errorPath)
below.
See also a somewhat related discussion I recently stumbled on: llvm/llvm-project#94798.
Thats not related. That does call two move constructors.
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.
Not its not.
std::move
is just a cast. A move will happen when its passed to a value parameter. Anotherstd::move
will still be needed since an rvalue reference variable will be an lvalue reference instead.
Thanks. As usual I have been stupid here. But the latter sentence answered a question while looking at the code.
What would be a case where a second std::move()
would be unnecessary? I am wondering if the existing Clang/clang-tidy check would detect those.
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 would be a case where a second std::move() would be unnecessary?
If you want to make a copy. But it seems like the check should require using std::move
or std::as_const
to make moves and copies explicit.
@@ -4355,7 +4355,7 @@ static void valueFlowAfterAssign(TokenList &tokenlist, | |||
continue; | |||
ids.insert(value.tokvalue->exprId()); | |||
} | |||
for (ValueFlow::Value value : values) { | |||
for (ValueFlow::Value& value : values) { |
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.
This should be for (ValueFlow::Value&& value : std::move(values))
. This will make clear that the elements in the values
vector has been moved.
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.
So we can't just move, we need to use move iterators. Something like this would do it:
template <class Container>
struct move_range_adaptor {
move_range_adaptor(Container&& c) : container(std::forward<Container>(c)) {}
auto begin() { return std::make_move_iterator(container.begin()); }
auto end() { return std::make_move_iterator(container.end()); }
private:
Container container;
};
template<class Container>
move_range_adaptor<Container> move_range(Container&& c) {
return {std::forward<Container>(c)};
}
We just need to update our analysis to treat move_range
as a move.
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.
This will make clear that the elements in the
values
vector has been moved.
That is a good point. It could even be a readability check.
I have been avoiding using rvalues explicitly ever since they were causing copies instead of moves in a project I worked on in the past and we had to nuke them all over the place to get rid of performance issues (I no longer have access to the code base but I think there might be affected parts in a public repo and will try to reproduce that).
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.
I have been avoiding using rvalues explicitly ever since they were causing copies instead of moves in a project I worked on in the past and we had to nuke them all over the place to get rid of performance issues
I would assume this issue came from not moving rvalue references since rvalue reference variables become lvalues so they need to be moved again to be an rvalue.
Ideally there should be a check for this. Every implicit copy on an rvalue reference should be made as an explicit move or copy(althougth I am not sure how an explicit copy should be expressed).
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.
Every implicit copy on an rvalue reference should be made as an explicit move or copy(althougth I am not sure how an explicit copy should be expressed).
Actually, std::as_const
could be used to explicity copy.
@@ -327,11 +327,13 @@ static void programMemoryParseCondition(ProgramMemory& pm, const Token* tok, con | |||
if (endTok && findExpressionChanged(vartok, tok->next(), endTok, settings)) | |||
return; | |||
const bool impossible = (tok->str() == "==" && !then) || (tok->str() == "!=" && then); | |||
const ValueFlow::Value& v = then ? truevalue : falsevalue; | |||
pm.setValue(vartok, impossible ? asImpossible(v) : v); | |||
ValueFlow::Value& v = then ? truevalue : falsevalue; |
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.
Should be ValueFlow::Value&& v = then ? std::move(truevalue) : std::move(falsevalue)
.
No description provided.