-
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?
Changes from all commits
a5d5e0e
750aa37
321f5d1
a838908
820df8c
78aff9c
2b51368
989e3bc
7007d20
20319bb
58b9af5
503a76a
a1da6fa
bd75fca
347823d
cb30d60
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1684,7 +1684,7 @@ ValueFlow::Value ValueFlow::getLifetimeObjValue(const Token *tok, bool inconclus | |
// There should only be one lifetime | ||
if (values.size() != 1) | ||
return ValueFlow::Value{}; | ||
return values.front(); | ||
return std::move(values.front()); | ||
} | ||
|
||
template<class Predicate> | ||
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more. This should be written as |
||
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 commentThe reason will be displayed to describe this comment to others. Learn more. This should be written as There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more.
Not its not.
Thats not related. That does call two move constructors. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
If you want to make a copy. But it seems like the check should require using |
||
errorPath.emplace_back(tok, "Address of variable taken here."); | ||
|
||
ValueFlow::Value value; | ||
|
@@ -3801,7 +3801,7 @@ static void valueFlowSymbolicOperators(const SymbolDatabase& symboldatabase, con | |
continue; | ||
|
||
ValueFlow::Value v = makeSymbolic(arg); | ||
v.errorPath = c.errorPath; | ||
v.errorPath = std::move(c.errorPath); | ||
v.errorPath.emplace_back(tok, "Passed to " + tok->str()); | ||
if (c.intvalue == 0) | ||
v.setImpossible(); | ||
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more. This should be There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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 commentThe reason will be displayed to describe this comment to others. Learn more.
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 commentThe reason will be displayed to describe this comment to others. Learn more.
Actually, |
||
if (!value.isSymbolicValue()) | ||
continue; | ||
const Token* expr = value.tokvalue; | ||
|
@@ -5198,7 +5198,7 @@ static void valueFlowInferCondition(TokenList& tokenlist, const Settings& settin | |
for (const ValuePtr<InferModel>& model : iteratorModels) { | ||
std::vector<ValueFlow::Value> result = | ||
infer(model, tok->str(), tok->astOperand1()->values(), tok->astOperand2()->values()); | ||
for (ValueFlow::Value value : result) { | ||
for (ValueFlow::Value& value : result) { | ||
value.valueType = ValueFlow::Value::ValueType::INT; | ||
setTokenValue(tok, std::move(value), settings); | ||
} | ||
|
@@ -5216,8 +5216,7 @@ static void valueFlowInferCondition(TokenList& tokenlist, const Settings& settin | |
std::vector<ValueFlow::Value> result = infer(makeIntegralInferModel(), "!=", tok->values(), 0); | ||
if (result.size() != 1) | ||
continue; | ||
ValueFlow::Value value = result.front(); | ||
setTokenValue(tok, std::move(value), settings); | ||
setTokenValue(tok, std::move(result.front()), settings); | ||
} | ||
} | ||
} | ||
|
@@ -5636,7 +5635,7 @@ static void valueFlowInjectParameter(const TokenList& tokenlist, | |
const Settings& settings, | ||
const Variable* arg, | ||
const Scope* functionScope, | ||
const std::list<ValueFlow::Value>& argvalues) | ||
std::list<ValueFlow::Value> argvalues) | ||
{ | ||
// Is argument passed by value or const reference, and is it a known non-class type? | ||
if (arg->isReference() && !arg->isConst() && !arg->isClass()) | ||
|
@@ -5650,7 +5649,7 @@ static void valueFlowInjectParameter(const TokenList& tokenlist, | |
valueFlowForward(const_cast<Token*>(functionScope->bodyStart->next()), | ||
functionScope->bodyEnd, | ||
arg->nameToken(), | ||
argvalues, | ||
std::move(argvalues), | ||
tokenlist, | ||
errorLogger, | ||
settings); | ||
|
@@ -5876,7 +5875,7 @@ static void valueFlowFunctionDefaultParameter(const TokenList& tokenlist, const | |
argvalues.push_back(std::move(v)); | ||
} | ||
if (!argvalues.empty()) | ||
valueFlowInjectParameter(tokenlist, errorLogger, settings, var, scope, argvalues); | ||
valueFlowInjectParameter(tokenlist, errorLogger, settings, var, scope, std::move(argvalues)); | ||
} | ||
} | ||
} | ||
|
@@ -5961,10 +5960,10 @@ static void valueFlowFunctionReturn(TokenList& tokenlist, ErrorLogger& errorLogg | |
|
||
bool hasKnownValue = false; | ||
|
||
for (const ValueFlow::Value& v : getCommonValuesFromTokens(returns)) { | ||
setFunctionReturnValue(function, tok, v, settings, false); | ||
for (ValueFlow::Value& v : getCommonValuesFromTokens(returns)) { | ||
if (v.isKnown()) | ||
hasKnownValue = true; | ||
setFunctionReturnValue(function, tok, std::move(v), settings, false); | ||
} | ||
|
||
if (hasKnownValue) | ||
|
@@ -5990,10 +5989,10 @@ static void valueFlowFunctionReturn(TokenList& tokenlist, ErrorLogger& errorLogg | |
if (programMemory.empty() && !arguments.empty()) | ||
continue; | ||
std::vector<ValueFlow::Value> values = execute(function->functionScope, programMemory, settings); | ||
for (const ValueFlow::Value& v : values) { | ||
for (ValueFlow::Value& v : values) { | ||
if (v.isUninitValue()) | ||
continue; | ||
setFunctionReturnValue(function, tok, v, settings); | ||
setFunctionReturnValue(function, tok, std::move(v), settings); | ||
} | ||
} | ||
} | ||
|
@@ -6799,8 +6798,8 @@ static void valueFlowContainerSize(const TokenList& tokenlist, | |
continue; | ||
} | ||
|
||
for (const ValueFlow::Value& value : values) { | ||
valueFlowForward(nameToken->next(), var->nameToken(), value, tokenlist, errorLogger, settings); | ||
for (ValueFlow::Value& value : values) { | ||
valueFlowForward(nameToken->next(), var->nameToken(), std::move(value), tokenlist, errorLogger, settings); | ||
} | ||
} | ||
|
||
|
@@ -6846,8 +6845,8 @@ static void valueFlowContainerSize(const TokenList& tokenlist, | |
const Token* constructorArgs = tok; | ||
values = getContainerSizeFromConstructor(constructorArgs, tok->valueType(), settings, true); | ||
} | ||
for (const ValueFlow::Value& value : values) | ||
setTokenValue(tok, value, settings); | ||
for (ValueFlow::Value& value : values) | ||
setTokenValue(tok, std::move(value), settings); | ||
} else if (Token::Match(tok, ";|{|} %var% =") && Token::Match(tok->tokAt(2)->astOperand2(), "[({]") && | ||
// init list | ||
((tok->tokAt(2) == tok->tokAt(2)->astOperand2()->astParent() && !tok->tokAt(2)->astOperand2()->astOperand2() && tok->tokAt(2)->astOperand2()->str() == "{") || | ||
|
@@ -6860,8 +6859,8 @@ static void valueFlowContainerSize(const TokenList& tokenlist, | |
Token* rhs = tok->tokAt(2)->astOperand2(); | ||
std::vector<ValueFlow::Value> values = getInitListSize(rhs, containerTok->valueType(), settings); | ||
valueFlowContainerSetTokValue(tokenlist, errorLogger, settings, containerTok, rhs); | ||
for (const ValueFlow::Value& value : values) | ||
valueFlowForward(containerTok->next(), containerTok, value, tokenlist, errorLogger, settings); | ||
for (ValueFlow::Value& value : values) | ||
valueFlowForward(containerTok->next(), containerTok, std::move(value), tokenlist, errorLogger, settings); | ||
} | ||
} else if (Token::Match(tok, ". %name% (") && tok->astOperand1() && tok->astOperand1()->valueType() && | ||
tok->astOperand1()->valueType()->container) { | ||
|
@@ -7125,8 +7124,8 @@ static void valueFlowSafeFunctions(const TokenList& tokenlist, const SymbolDatab | |
argValues.back().valueType = ValueFlow::Value::ValueType::CONTAINER_SIZE; | ||
argValues.back().errorPath.emplace_back(arg.nameToken(), "Assuming " + arg.name() + " size is 1000000"); | ||
argValues.back().safe = true; | ||
for (const ValueFlow::Value &value : argValues) | ||
valueFlowForward(const_cast<Token*>(functionScope->bodyStart), arg.nameToken(), value, tokenlist, errorLogger, settings); | ||
for (ValueFlow::Value &value : argValues) | ||
valueFlowForward(const_cast<Token*>(functionScope->bodyStart), arg.nameToken(), std::move(value), tokenlist, errorLogger, settings); | ||
continue; | ||
} | ||
|
||
|
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)
.