Skip to content

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

Draft
wants to merge 16 commits into
base: main
Choose a base branch
from

Conversation

firewave
Copy link
Collaborator

No description provided.

@firewave
Copy link
Collaborator Author

firewave commented Jun 10, 2025

Mostly taken from #6756 and discovered by reviewing the code. Still needs to be profiled.

@firewave firewave marked this pull request as draft June 10, 2025 10:10
@firewave firewave force-pushed the vf-move branch 3 times, most recently from 42d3603 to 762ebed Compare June 10, 2025 10:59
@firewave firewave marked this pull request as ready for review June 10, 2025 11:17
@firewave
Copy link
Collaborator Author

Not much in terms of performance which is not surprising since these were found by review and not by profiling.

{
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)
Copy link
Owner

@danmar danmar Jun 12, 2025

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.

Copy link
Collaborator Author

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.

Copy link
Collaborator Author

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.

@firewave firewave marked this pull request as draft June 12, 2025 13:19
@firewave firewave marked this pull request as ready for review June 29, 2025 16:03
@@ -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)) {
Copy link
Contributor

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

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).

Copy link
Collaborator Author

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.

Copy link
Contributor

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.

Copy link
Collaborator Author

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. Another std::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.

Copy link
Contributor

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

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.

Copy link
Contributor

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.

Copy link
Collaborator Author

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).

Copy link
Contributor

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).

Copy link
Contributor

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

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).

@firewave firewave marked this pull request as draft July 3, 2025 09:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants