Bug 1370674 part 1 - Use mUpdateTimers.LookupRemoveIf() to avoid a second hashtable lookup for Remove(). Use mUpdateTimers.GetOrInsert() to avoid a second hashtable lookup for Put(). r=froydnj

MozReview-Commit-ID: 1lp2s4NQfvR
This commit is contained in:
Mats Palmgren 2017-06-14 01:03:38 +02:00
Родитель 24a9e3129b
Коммит 10986b2016
1 изменённых файлов: 18 добавлений и 19 удалений

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

@ -2344,11 +2344,11 @@ ServiceWorkerManager::RemoveScopeAndRegistration(ServiceWorkerRegistrationInfo*
return; return;
} }
nsCOMPtr<nsITimer> timer = data->mUpdateTimers.Get(aRegistration->mScope); data->mUpdateTimers.LookupRemoveIf(aRegistration->mScope,
if (timer) { [] (nsCOMPtr<nsITimer>& aTimer) {
timer->Cancel(); aTimer->Cancel();
data->mUpdateTimers.Remove(aRegistration->mScope); return true; // remove it
} });
// The registration should generally only be removed if there are no controlled // The registration should generally only be removed if there are no controlled
// documents, but mControlledDocuments can contain references to potentially // documents, but mControlledDocuments can contain references to potentially
@ -3679,12 +3679,11 @@ ServiceWorkerManager::ForceUnregister(RegistrationDataPerPrincipal* aRegistratio
queue->CancelAll(); queue->CancelAll();
} }
nsCOMPtr<nsITimer> timer = aRegistrationData->mUpdateTimers.LookupRemoveIf(aRegistration->mScope,
aRegistrationData->mUpdateTimers.Get(aRegistration->mScope); [] (nsCOMPtr<nsITimer>& aTimer) {
if (timer) { aTimer->Cancel();
timer->Cancel(); return true; // remove it
aRegistrationData->mUpdateTimers.Remove(aRegistration->mScope); });
}
// Since Unregister is async, it is ok to call it in an enumeration. // Since Unregister is async, it is ok to call it in an enumeration.
Unregister(aRegistration->mPrincipal, nullptr, NS_ConvertUTF8toUTF16(aRegistration->mScope)); Unregister(aRegistration->mPrincipal, nullptr, NS_ConvertUTF8toUTF16(aRegistration->mScope));
@ -4250,7 +4249,7 @@ ServiceWorkerManager::ScheduleUpdateTimer(nsIPrincipal* aPrincipal,
return; return;
} }
nsCOMPtr<nsITimer> timer = data->mUpdateTimers.Get(aScope); nsCOMPtr<nsITimer>& timer = data->mUpdateTimers.GetOrInsert(aScope);
if (timer) { if (timer) {
// There is already a timer scheduled. In this case just use the original // There is already a timer scheduled. In this case just use the original
// schedule time. We don't want to push it out to a later time since that // schedule time. We don't want to push it out to a later time since that
@ -4261,6 +4260,7 @@ ServiceWorkerManager::ScheduleUpdateTimer(nsIPrincipal* aPrincipal,
timer = do_CreateInstance("@mozilla.org/timer;1", &rv); timer = do_CreateInstance("@mozilla.org/timer;1", &rv);
if (NS_WARN_IF(NS_FAILED(rv))) { if (NS_WARN_IF(NS_FAILED(rv))) {
data->mUpdateTimers.Remove(aScope); // another lookup, but very rare
return; return;
} }
@ -4272,10 +4272,9 @@ ServiceWorkerManager::ScheduleUpdateTimer(nsIPrincipal* aPrincipal,
rv = timer->InitWithCallback(callback, UPDATE_DELAY_MS, rv = timer->InitWithCallback(callback, UPDATE_DELAY_MS,
nsITimer::TYPE_ONE_SHOT); nsITimer::TYPE_ONE_SHOT);
if (NS_WARN_IF(NS_FAILED(rv))) { if (NS_WARN_IF(NS_FAILED(rv))) {
data->mUpdateTimers.Remove(aScope); // another lookup, but very rare
return; return;
} }
data->mUpdateTimers.Put(aScope, timer);
} }
void void
@ -4302,11 +4301,11 @@ ServiceWorkerManager::UpdateTimerFired(nsIPrincipal* aPrincipal,
return; return;
} }
nsCOMPtr<nsITimer> timer = data->mUpdateTimers.Get(aScope); data->mUpdateTimers.LookupRemoveIf(aScope,
if (timer) { [] (nsCOMPtr<nsITimer>& aTimer) {
timer->Cancel(); aTimer->Cancel();
data->mUpdateTimers.Remove(aScope); return true; // remove it
} });
RefPtr<ServiceWorkerRegistrationInfo> registration; RefPtr<ServiceWorkerRegistrationInfo> registration;
data->mInfos.Get(aScope, getter_AddRefs(registration)); data->mInfos.Get(aScope, getter_AddRefs(registration));