Bug 1592992 - Part 3: Replace js::EraseIf with Vector::eraseIf where possible. r=jonco

mozilla::Vector already provides an `eraseIf` method, so we should prefer to use
the existing method. Except that Vector::eraseIf doesn't return the number of
elements removed, but that's easy to determine manually. More annoyingly is the
use of a modifying predicate function in Zone.cpp, which prevents using
Vector::eraseIf. It looks like for that specific use case a custome `EraseIf`
function is still necessary.

Differential Revision: https://phabricator.services.mozilla.com/D51362

--HG--
extra : moz-landing-system : lando
This commit is contained in:
André Bargull 2019-11-08 11:03:05 +00:00
parent 8a116f84e5
commit 9010f66124
3 changed files with 24 additions and 21 deletions

View File

@ -162,8 +162,14 @@ class Fifo {
// the number of elements removed.
template <class Pred>
size_t eraseIf(Pred pred) {
size_t erased = EraseIf(front_, pred);
erased += EraseIf(rear_, pred);
size_t frontLength = front_.length();
front_.eraseIf(pred);
size_t erased = frontLength - front_.length();
size_t rearLength = rear_.length();
rear_.eraseIf(pred);
erased += rearLength - rear_.length();
return erased;
}

View File

@ -155,6 +155,22 @@ void Zone::setNeedsIncrementalBarrier(bool needs) {
void Zone::beginSweepTypes() { types.beginSweep(); }
template <class Pred>
static void EraseIf(js::gc::WeakEntryVector& entries, Pred pred) {
auto* begin = entries.begin();
auto* const end = entries.end();
auto* newEnd = begin;
for (auto* p = begin; p != end; p++) {
if (!pred(*p)) {
*newEnd++ = *p;
}
}
size_t removed = end - newEnd;
entries.shrinkBy(removed);
}
static void SweepWeakEntryVectorWhileMinorSweeping(
js::gc::WeakEntryVector& entries) {
EraseIf(entries, [](js::gc::WeakMarkable& markable) -> bool {

View File

@ -77,25 +77,6 @@ static inline void Reverse(T* beg, T* end) {
}
}
template <class T, class Pred>
static inline T* RemoveIf(T* begin, T* end, Pred pred) {
T* result = begin;
for (T* p = begin; p != end; p++) {
if (!pred(*p)) {
*result++ = *p;
}
}
return result;
}
template <class Container, class Pred>
static inline size_t EraseIf(Container& c, Pred pred) {
auto newEnd = RemoveIf(c.begin(), c.end(), pred);
size_t removed = c.end() - newEnd;
c.shrinkBy(removed);
return removed;
}
template <class Container1, class Container2>
static inline bool EqualContainers(const Container1& lhs,
const Container2& rhs) {