Skip to content

Commit

Permalink
ProgramMemory: avoid several unnecessary copies (#5943)
Browse files Browse the repository at this point in the history
  • Loading branch information
firewave committed Feb 4, 2024
1 parent a55ef10 commit db3ce5e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
25 changes: 13 additions & 12 deletions lib/programmemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void ProgramMemory::setValue(const Token* expr, const ValueFlow::Value& value) {
},
subvalue);
if (subexpr)
mValues[subexpr] = subvalue;
mValues[subexpr] = std::move(subvalue);
}
const ValueFlow::Value* ProgramMemory::getValue(nonneg int exprid, bool impossible) const
{
Expand Down Expand Up @@ -183,10 +183,10 @@ bool ProgramMemory::empty() const
return mValues.empty();
}

void ProgramMemory::replace(const ProgramMemory &pm)
void ProgramMemory::replace(ProgramMemory pm)
{
for (auto&& p : pm.mValues) {
mValues[p.first] = p.second;
mValues[p.first] = std::move(p.second);
}
}

Expand Down Expand Up @@ -448,12 +448,12 @@ void ProgramMemoryState::insert(const ProgramMemory &pm, const Token* origin)
state.insert(pm);
}

void ProgramMemoryState::replace(const ProgramMemory &pm, const Token* origin)
void ProgramMemoryState::replace(ProgramMemory pm, const Token* origin)
{
if (origin)
for (auto&& p : pm)
for (const auto& p : pm)
origins[p.first.getExpressionId()] = origin;
state.replace(pm);
state.replace(std::move(pm));
}

static void addVars(ProgramMemory& pm, const ProgramMemory::Map& vars)
Expand All @@ -472,7 +472,7 @@ void ProgramMemoryState::addState(const Token* tok, const ProgramMemory::Map& va
ProgramMemory local = pm;
fillProgramMemoryFromAssignments(pm, tok, settings, local, vars);
addVars(pm, vars);
replace(pm, tok);
replace(std::move(pm), tok);
}

void ProgramMemoryState::assume(const Token* tok, bool b, bool isEmpty)
Expand All @@ -495,7 +495,7 @@ void ProgramMemoryState::assume(const Token* tok, bool b, bool isEmpty)

void ProgramMemoryState::removeModifiedVars(const Token* tok)
{
ProgramMemory pm = state;
const ProgramMemory& pm = state;
auto eval = [&](const Token* cond) -> std::vector<MathLib::bigint> {
if (conditionIsTrue(cond, pm, settings))
return {1};
Expand Down Expand Up @@ -1270,9 +1270,10 @@ namespace {
ValueFlow::Value r = state.execute(tok);
if (r.isUninitValue())
continue;
result.insert(std::make_pair(tok->exprId(), r));
const bool brk = b && isTrueOrFalse(r, *b);
result.emplace(tok->exprId(), std::move(r));
// Short-circuit evaluation
if (b && isTrueOrFalse(r, *b))
if (brk)
break;
}
return result;
Expand Down Expand Up @@ -1496,13 +1497,13 @@ namespace {
if (expr->isComparisonOp() && (r.isUninitValue() || r.isImpossible())) {
if (rhs.isIntValue()) {
std::vector<ValueFlow::Value> result =
infer(ValueFlow::makeIntegralInferModel(), expr->str(), expr->astOperand1()->values(), {rhs});
infer(ValueFlow::makeIntegralInferModel(), expr->str(), expr->astOperand1()->values(), {std::move(rhs)});
if (!result.empty() && result.front().isKnown())
return result.front();
}
if (lhs.isIntValue()) {
std::vector<ValueFlow::Value> result =
infer(ValueFlow::makeIntegralInferModel(), expr->str(), {lhs}, expr->astOperand2()->values());
infer(ValueFlow::makeIntegralInferModel(), expr->str(), {std::move(lhs)}, expr->astOperand2()->values());
if (!result.empty() && result.front().isKnown())
return result.front();
}
Expand Down
4 changes: 2 additions & 2 deletions lib/programmemory.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ struct ProgramMemory {

bool empty() const;

void replace(const ProgramMemory &pm);
void replace(ProgramMemory pm);

void insert(const ProgramMemory &pm);

Expand Down Expand Up @@ -171,7 +171,7 @@ struct ProgramMemoryState {
explicit ProgramMemoryState(const Settings* s);

void insert(const ProgramMemory &pm, const Token* origin = nullptr);
void replace(const ProgramMemory &pm, const Token* origin = nullptr);
void replace(ProgramMemory pm, const Token* origin = nullptr);

void addState(const Token* tok, const ProgramMemory::Map& vars);

Expand Down

0 comments on commit db3ce5e

Please sign in to comment.