Skip to content

Commit

Permalink
ProgramMemory: avoid unnecessary copy in erase_if() [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
firewave committed Aug 15, 2024
1 parent f4c9f3a commit 27066a0
Showing 1 changed file with 32 additions and 3 deletions.
35 changes: 32 additions & 3 deletions lib/programmemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,39 @@ void ProgramMemory::erase_if(const std::function<bool(const ExprIdToken&)>& pred
if (mValues->empty())
return;

// TODO: how to delay until we actuallly modify?
copyOnWrite();
// TODO: the std::find_if() call is causing ValueFlow::Value::Value(ValueFlow::Value const&) invocations
/*auto it = std::find_if(mValues->cbegin(), mValues->cend(), [&pred](const std::pair<ExprIdToken, ValueFlow::Value>& entry) {
return pred(entry.first);
});*/

auto it = mValues->cbegin();
for (; it != mValues->cend(); ++it) {
if (pred(it->first))
break;
}
if (it == mValues->cend())
return;

if (mValues.use_count() == 1)
{
it = mValues->erase(it);
}
else
{
const auto& exprIdTok = it->first;

copyOnWrite();

it = mValues->cbegin();
for (; it != mValues->cend(); ++it) {
if (it->first == exprIdTok) {
it = mValues->erase(it);
break;
}
}
}

for (auto it = mValues->begin(); it != mValues->end();) {
for (; it != mValues->cend();) {
if (pred(it->first))
it = mValues->erase(it);
else
Expand Down

0 comments on commit 27066a0

Please sign in to comment.