From fb9415936999f824f45255037e8a8c52a3c0c1a8 Mon Sep 17 00:00:00 2001 From: Mats Palmgren Date: Wed, 14 Jun 2017 01:54:26 +0200 Subject: [PATCH] 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 --- dom/base/Element.cpp | 43 +++++++++++++++++-------------------------- 1 file changed, 17 insertions(+), 26 deletions(-) diff --git a/dom/base/Element.cpp b/dom/base/Element.cpp index a2bcd91121ed..6042e69d3c8b 100644 --- a/dom/base/Element.cpp +++ b/dom/base/Element.cpp @@ -4084,42 +4084,33 @@ enum nsPreviousIntersectionThreshold { void Element::RegisterIntersectionObserver(DOMIntersectionObserver* aObserver) { - nsDataHashtable, 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, int32_t>* observers = - RegisteredIntersectionObservers(); - observers->Remove(aObserver); + RegisteredIntersectionObservers()->Remove(aObserver); } bool Element::UpdateIntersectionObservation(DOMIntersectionObserver* aObserver, int32_t aThreshold) { - nsDataHashtable, 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