Skip to content

Commit 27066a0

Browse files
committed
ProgramMemory: avoid unnecessary copy in erase_if() [skip ci]
1 parent f4c9f3a commit 27066a0

1 file changed

Lines changed: 32 additions & 3 deletions

File tree

lib/programmemory.cpp

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,39 @@ void ProgramMemory::erase_if(const std::function<bool(const ExprIdToken&)>& pred
173173
if (mValues->empty())
174174
return;
175175

176-
// TODO: how to delay until we actuallly modify?
177-
copyOnWrite();
176+
// TODO: the std::find_if() call is causing ValueFlow::Value::Value(ValueFlow::Value const&) invocations
177+
/*auto it = std::find_if(mValues->cbegin(), mValues->cend(), [&pred](const std::pair<ExprIdToken, ValueFlow::Value>& entry) {
178+
return pred(entry.first);
179+
});*/
180+
181+
auto it = mValues->cbegin();
182+
for (; it != mValues->cend(); ++it) {
183+
if (pred(it->first))
184+
break;
185+
}
186+
if (it == mValues->cend())
187+
return;
188+
189+
if (mValues.use_count() == 1)
190+
{
191+
it = mValues->erase(it);
192+
}
193+
else
194+
{
195+
const auto& exprIdTok = it->first;
196+
197+
copyOnWrite();
198+
199+
it = mValues->cbegin();
200+
for (; it != mValues->cend(); ++it) {
201+
if (it->first == exprIdTok) {
202+
it = mValues->erase(it);
203+
break;
204+
}
205+
}
206+
}
178207

179-
for (auto it = mValues->begin(); it != mValues->end();) {
208+
for (; it != mValues->cend();) {
180209
if (pred(it->first))
181210
it = mValues->erase(it);
182211
else

0 commit comments

Comments
 (0)