Bug 1693541 - Improve uses of nsBaseHashtable and descendants and avoid multiple subsequent lookups in dom/localstorage. r=dom-storage-reviewers,janv

Differential Revision: https://phabricator.services.mozilla.com/D106089
This commit is contained in:
Simon Giesecke 2021-03-01 09:59:31 +00:00
parent 41be2e9861
commit 0b97035410
5 changed files with 43 additions and 45 deletions

View File

@ -4221,7 +4221,7 @@ already_AddRefed<Connection> ConnectionThread::CreateConnection(
bool aDatabaseWasNotAvailable) {
AssertIsOnOwningThread();
MOZ_ASSERT(!aOriginMetadata.mOrigin.IsEmpty());
MOZ_ASSERT(!mConnections.GetWeak(aOriginMetadata.mOrigin));
MOZ_ASSERT(!mConnections.Contains(aOriginMetadata.mOrigin));
RefPtr<Connection> connection =
new Connection(this, aOriginMetadata, std::move(aArchivedOriginScope),
@ -7242,7 +7242,7 @@ void PrepareDatastoreOp::GetResponse(LSRequestResponse& aResponse) {
gDatastores = new DatastoreHashtable();
}
MOZ_ASSERT(!gDatastores->MaybeGet(Origin()));
MOZ_ASSERT(!gDatastores->Contains(Origin()));
gDatastores->InsertOrUpdate(Origin(),
WrapMovingNotNullUnchecked(mDatastore));
}
@ -7252,14 +7252,14 @@ void PrepareDatastoreOp::GetResponse(LSRequestResponse& aResponse) {
gPrivateDatastores = MakeUnique<PrivateDatastoreHashtable>();
}
if (!gPrivateDatastores->Get(Origin())) {
gPrivateDatastores->LookupOrInsertWith(Origin(), [&] {
auto privateDatastore =
MakeUnique<PrivateDatastore>(WrapMovingNotNull(mDatastore));
gPrivateDatastores->InsertOrUpdate(Origin(), std::move(privateDatastore));
mPrivateDatastoreRegistered.Flip();
}
return privateDatastore;
});
}
mDatastoreId = ++gLastDatastoreId;

View File

@ -85,7 +85,7 @@ LSDatabase::LSDatabase(const nsACString& aOrigin)
obsSvc->AddObserver(sObserver, XPCOM_SHUTDOWN_OBSERVER_TOPIC, false));
}
MOZ_ASSERT(!gLSDatabases->Get(mOrigin));
MOZ_ASSERT(!gLSDatabases->Contains(mOrigin));
gLSDatabases->InsertOrUpdate(mOrigin, this);
}

View File

@ -33,7 +33,7 @@ LSObserver::LSObserver(const nsACString& aOrigin)
gLSObservers = new LSObserverHashtable();
}
MOZ_ASSERT(!gLSObservers->Get(mOrigin));
MOZ_ASSERT(!gLSObservers->Contains(mOrigin));
gLSObservers->InsertOrUpdate(mOrigin, this);
}

View File

@ -28,14 +28,14 @@ class LSWriteOptimizerBase::WriteInfoComparator {
void LSWriteOptimizerBase::DeleteItem(const nsAString& aKey, int64_t aDelta) {
AssertIsOnOwningThread();
WriteInfo* existingWriteInfo;
if (mWriteInfos.Get(aKey, &existingWriteInfo) &&
existingWriteInfo->GetType() == WriteInfo::InsertItem) {
mWriteInfos.Remove(aKey);
} else {
mWriteInfos.InsertOrUpdate(
aKey, MakeUnique<DeleteItemInfo>(NextSerialNumber(), aKey));
}
mWriteInfos.WithEntryHandle(aKey, [&](auto&& entry) {
if (entry && entry.Data()->GetType() == WriteInfo::InsertItem) {
entry.Remove();
} else {
entry.InsertOrUpdate(
MakeUnique<DeleteItemInfo>(NextSerialNumber(), aKey));
}
});
mTotalDelta += aDelta;
}

View File

@ -17,26 +17,24 @@ void LSWriteOptimizer<T, U>::InsertItem(const nsAString& aKey, const T& aValue,
int64_t aDelta) {
AssertIsOnOwningThread();
WriteInfo* existingWriteInfo;
UniquePtr<WriteInfo> newWriteInfo;
if (mWriteInfos.Get(aKey, &existingWriteInfo) &&
existingWriteInfo->GetType() == WriteInfo::DeleteItem) {
// We could just simply replace the deletion with ordinary update, but
// that
// would preserve item's original position/index. Imagine a case when we
// have only one existing key k1. Now let's create a new optimizer and
// remove k1, add k2 and add k1 back. The final order should be k2, k1
// (ordinary update would produce k1, k2). So we need to differentiate
// between normal update and "optimized" update which resulted from a
// deletion followed by an insertion. We use the UpdateWithMove flag for
// this.
mWriteInfos.WithEntryHandle(aKey, [&](auto&& entry) {
if (entry && entry.Data()->GetType() == WriteInfo::DeleteItem) {
// We could just simply replace the deletion with ordinary update, but
// that would preserve item's original position/index. Imagine a case when
// we have only one existing key k1. Now let's create a new optimizer and
// remove k1, add k2 and add k1 back. The final order should be k2, k1
// (ordinary update would produce k1, k2). So we need to differentiate
// between normal update and "optimized" update which resulted from a
// deletion followed by an insertion. We use the UpdateWithMove flag for
// this.
newWriteInfo = MakeUnique<UpdateItemInfo>(NextSerialNumber(), aKey, aValue,
/* aUpdateWithMove */ true);
} else {
newWriteInfo = MakeUnique<InsertItemInfo>(NextSerialNumber(), aKey, aValue);
}
mWriteInfos.InsertOrUpdate(aKey, std::move(newWriteInfo));
entry.Update(MakeUnique<UpdateItemInfo>(NextSerialNumber(), aKey, aValue,
/* aUpdateWithMove */ true));
} else {
entry.InsertOrUpdate(
MakeUnique<InsertItemInfo>(NextSerialNumber(), aKey, aValue));
}
});
mTotalDelta += aDelta;
}
@ -46,16 +44,16 @@ void LSWriteOptimizer<T, U>::UpdateItem(const nsAString& aKey, const T& aValue,
int64_t aDelta) {
AssertIsOnOwningThread();
WriteInfo* existingWriteInfo;
UniquePtr<WriteInfo> newWriteInfo;
if (mWriteInfos.Get(aKey, &existingWriteInfo) &&
existingWriteInfo->GetType() == WriteInfo::InsertItem) {
newWriteInfo = MakeUnique<InsertItemInfo>(NextSerialNumber(), aKey, aValue);
} else {
newWriteInfo = MakeUnique<UpdateItemInfo>(NextSerialNumber(), aKey, aValue,
/* aUpdateWithMove */ false);
}
mWriteInfos.InsertOrUpdate(aKey, std::move(newWriteInfo));
mWriteInfos.WithEntryHandle(aKey, [&](auto&& entry) {
if (entry && entry.Data()->GetType() == WriteInfo::InsertItem) {
entry.Update(
MakeUnique<InsertItemInfo>(NextSerialNumber(), aKey, aValue));
} else {
entry.InsertOrUpdate(
MakeUnique<UpdateItemInfo>(NextSerialNumber(), aKey, aValue,
/* aUpdateWithMove */ false));
}
});
mTotalDelta += aDelta;
}