Bug 1372007 - Replace calls to Contains+Put with LookupForAdd and Contains+Get+Put with LookupRemoveIf to avoid unnecessary hashtable lookups. r=froydnj

MozReview-Commit-ID: 5Y1rVCglpzu
This commit is contained in:
Mats Palmgren 2017-06-14 01:54:26 +02:00
Родитель bddaa611f0
Коммит fb94159369
1 изменённых файлов: 17 добавлений и 26 удалений

Просмотреть файл

@ -4084,42 +4084,33 @@ enum nsPreviousIntersectionThreshold {
void
Element::RegisterIntersectionObserver(DOMIntersectionObserver* aObserver)
{
nsDataHashtable<nsRefPtrHashKey<DOMIntersectionObserver>, int32_t>* observers =
RegisteredIntersectionObservers();
if (observers->Contains(aObserver)) {
return;
}
// Value can be:
// -2: Makes sure next calculated threshold always differs, leading to a
// notification task being scheduled.
// -1: Non-intersecting.
// >= 0: Intersecting, valid index of aObserver->mThresholds.
RegisteredIntersectionObservers()->Put(aObserver, eUninitialized);
RegisteredIntersectionObservers()->LookupForAdd(aObserver).OrInsert([]() {
// Value can be:
// -2: Makes sure next calculated threshold always differs, leading to a
// notification task being scheduled.
// -1: Non-intersecting.
// >= 0: Intersecting, valid index of aObserver->mThresholds.
return eUninitialized;
});
}
void
Element::UnregisterIntersectionObserver(DOMIntersectionObserver* aObserver)
{
nsDataHashtable<nsRefPtrHashKey<DOMIntersectionObserver>, int32_t>* observers =
RegisteredIntersectionObservers();
observers->Remove(aObserver);
RegisteredIntersectionObservers()->Remove(aObserver);
}
bool
Element::UpdateIntersectionObservation(DOMIntersectionObserver* aObserver, int32_t aThreshold)
{
nsDataHashtable<nsRefPtrHashKey<DOMIntersectionObserver>, int32_t>* observers =
RegisteredIntersectionObservers();
if (!observers->Contains(aObserver)) {
return false;
}
int32_t previousThreshold = observers->Get(aObserver);
if (previousThreshold != aThreshold) {
observers->Put(aObserver, aThreshold);
return true;
}
return false;
bool updated = false;
RegisteredIntersectionObservers()->LookupRemoveIf(aObserver,
[&updated, aThreshold] (int32_t& aValue) {
updated = aValue != aThreshold;
aValue = aThreshold;
return false; // don't remove the entry
});
return updated;
}
void