Skip to content

Commit 6320972

Browse files
committed
ProgramMemory: avoid unnecessary copy in erase_if()
1 parent 53e55be commit 6320972

1 file changed

Lines changed: 8 additions & 7 deletions

File tree

lib/programmemory.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -173,14 +173,15 @@ 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: messes with the use count
177+
const auto values = mValues;
178+
179+
for (auto it = values->begin(); it != values->end(); ++it) {
180+
if (!pred(it->first))
181+
continue;
178182

179-
for (auto it = mValues->begin(); it != mValues->end();) {
180-
if (pred(it->first))
181-
it = mValues->erase(it);
182-
else
183-
++it;
183+
copyOnWrite();
184+
mValues->erase(it->first);
184185
}
185186
}
186187

0 commit comments

Comments
 (0)