Skip to content

Commit db3ce5e

Browse files
authored
ProgramMemory: avoid several unnecessary copies (#5943)
1 parent a55ef10 commit db3ce5e

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

lib/programmemory.cpp

+13-12
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ void ProgramMemory::setValue(const Token* expr, const ValueFlow::Value& value) {
6767
},
6868
subvalue);
6969
if (subexpr)
70-
mValues[subexpr] = subvalue;
70+
mValues[subexpr] = std::move(subvalue);
7171
}
7272
const ValueFlow::Value* ProgramMemory::getValue(nonneg int exprid, bool impossible) const
7373
{
@@ -183,10 +183,10 @@ bool ProgramMemory::empty() const
183183
return mValues.empty();
184184
}
185185

186-
void ProgramMemory::replace(const ProgramMemory &pm)
186+
void ProgramMemory::replace(ProgramMemory pm)
187187
{
188188
for (auto&& p : pm.mValues) {
189-
mValues[p.first] = p.second;
189+
mValues[p.first] = std::move(p.second);
190190
}
191191
}
192192

@@ -448,12 +448,12 @@ void ProgramMemoryState::insert(const ProgramMemory &pm, const Token* origin)
448448
state.insert(pm);
449449
}
450450

451-
void ProgramMemoryState::replace(const ProgramMemory &pm, const Token* origin)
451+
void ProgramMemoryState::replace(ProgramMemory pm, const Token* origin)
452452
{
453453
if (origin)
454-
for (auto&& p : pm)
454+
for (const auto& p : pm)
455455
origins[p.first.getExpressionId()] = origin;
456-
state.replace(pm);
456+
state.replace(std::move(pm));
457457
}
458458

459459
static void addVars(ProgramMemory& pm, const ProgramMemory::Map& vars)
@@ -472,7 +472,7 @@ void ProgramMemoryState::addState(const Token* tok, const ProgramMemory::Map& va
472472
ProgramMemory local = pm;
473473
fillProgramMemoryFromAssignments(pm, tok, settings, local, vars);
474474
addVars(pm, vars);
475-
replace(pm, tok);
475+
replace(std::move(pm), tok);
476476
}
477477

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

496496
void ProgramMemoryState::removeModifiedVars(const Token* tok)
497497
{
498-
ProgramMemory pm = state;
498+
const ProgramMemory& pm = state;
499499
auto eval = [&](const Token* cond) -> std::vector<MathLib::bigint> {
500500
if (conditionIsTrue(cond, pm, settings))
501501
return {1};
@@ -1270,9 +1270,10 @@ namespace {
12701270
ValueFlow::Value r = state.execute(tok);
12711271
if (r.isUninitValue())
12721272
continue;
1273-
result.insert(std::make_pair(tok->exprId(), r));
1273+
const bool brk = b && isTrueOrFalse(r, *b);
1274+
result.emplace(tok->exprId(), std::move(r));
12741275
// Short-circuit evaluation
1275-
if (b && isTrueOrFalse(r, *b))
1276+
if (brk)
12761277
break;
12771278
}
12781279
return result;
@@ -1496,13 +1497,13 @@ namespace {
14961497
if (expr->isComparisonOp() && (r.isUninitValue() || r.isImpossible())) {
14971498
if (rhs.isIntValue()) {
14981499
std::vector<ValueFlow::Value> result =
1499-
infer(ValueFlow::makeIntegralInferModel(), expr->str(), expr->astOperand1()->values(), {rhs});
1500+
infer(ValueFlow::makeIntegralInferModel(), expr->str(), expr->astOperand1()->values(), {std::move(rhs)});
15001501
if (!result.empty() && result.front().isKnown())
15011502
return result.front();
15021503
}
15031504
if (lhs.isIntValue()) {
15041505
std::vector<ValueFlow::Value> result =
1505-
infer(ValueFlow::makeIntegralInferModel(), expr->str(), {lhs}, expr->astOperand2()->values());
1506+
infer(ValueFlow::makeIntegralInferModel(), expr->str(), {std::move(lhs)}, expr->astOperand2()->values());
15061507
if (!result.empty() && result.front().isKnown())
15071508
return result.front();
15081509
}

lib/programmemory.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ struct ProgramMemory {
129129

130130
bool empty() const;
131131

132-
void replace(const ProgramMemory &pm);
132+
void replace(ProgramMemory pm);
133133

134134
void insert(const ProgramMemory &pm);
135135

@@ -171,7 +171,7 @@ struct ProgramMemoryState {
171171
explicit ProgramMemoryState(const Settings* s);
172172

173173
void insert(const ProgramMemory &pm, const Token* origin = nullptr);
174-
void replace(const ProgramMemory &pm, const Token* origin = nullptr);
174+
void replace(ProgramMemory pm, const Token* origin = nullptr);
175175

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

0 commit comments

Comments
 (0)