Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ProgramMemory: avoid unnecessary copy in erase_if() #6652

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont think this extra branch is needed. Especially if you can move the copyOnWrite out of the loop.

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