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 45cf5ce
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions lib/programmemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,32 @@ void ProgramMemory::erase_if(const std::function<bool(const ExprIdToken&)>& pred
if (mValues->empty())
return;

// TODO: how to delay until we actuallly modify?
copyOnWrite();
auto it = std::find_if(mValues->cbegin(), mValues->cend(), [&pred](const std::pair<const ExprIdToken, ValueFlow::Value>& entry) {
return pred(entry.first);
});
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 45cf5ce

Please sign in to comment.