Bug 708901 - Migrate to nsTHashSet in dom/localstorage. r=dom-storage-reviewers,janv

Depends on D108598

Differential Revision: https://phabricator.services.mozilla.com/D108599
This commit is contained in:
Simon Giesecke 2021-03-23 10:36:34 +00:00
parent 45420d4e39
commit 11949c7259
3 changed files with 79 additions and 81 deletions

View File

@ -125,7 +125,7 @@
#include "nsStringFlags.h"
#include "nsStringFwd.h"
#include "nsTArray.h"
#include "nsTHashtable.h"
#include "nsTHashSet.h"
#include "nsTLiteralString.h"
#include "nsTStringRepr.h"
#include "nsThreadUtils.h"
@ -1420,7 +1420,7 @@ class Datastore final
* PrepareDatastoreOps register themselves with the Datastore at
* and unregister in PrepareDatastoreOp::Cleanup.
*/
nsTHashtable<nsPtrHashKey<PrepareDatastoreOp>> mPrepareDatastoreOps;
nsTHashSet<PrepareDatastoreOp*> mPrepareDatastoreOps;
/**
* PreparedDatastore instances register themselves with their associated
* Datastore at construction time and unregister at destruction time. They
@ -1428,19 +1428,19 @@ class Datastore final
* from closing itself via MaybeClose(), thereby giving the document enough
* time to load and access LocalStorage.
*/
nsTHashtable<nsPtrHashKey<PreparedDatastore>> mPreparedDatastores;
nsTHashSet<PreparedDatastore*> mPreparedDatastores;
/**
* A database is live (and in this hashtable) if it has a live LSDatabase
* actor. There is at most one Database per origin per content process. Each
* Database corresponds to an LSDatabase in its associated content process.
*/
nsTHashtable<nsPtrHashKey<Database>> mDatabases;
nsTHashSet<Database*> mDatabases;
/**
* A database is active if it has a non-null `mSnapshot`. As long as there
* are any active databases final deltas can't be calculated and
* `UpdateUsage()` can't be invoked.
*/
nsTHashtable<nsPtrHashKey<Database>> mActiveDatabases;
nsTHashSet<Database*> mActiveDatabases;
/**
* Non-authoritative hashtable representation of mOrderedItems for efficient
* lookup.
@ -1834,7 +1834,7 @@ class Snapshot final : public PBackgroundLSSnapshotParent {
* other values must be null. (Note: this could also be done when
* mLoadKeysReceived is true as a further optimization, but is not.)
*/
nsTHashtable<nsStringHashKey> mUnknownItems;
nsTHashSet<nsString> mUnknownItems;
/**
* Values that have changed in mDatastore as reported by SaveItem
* notifications that are not yet known to the child LSSnapshot.
@ -1904,9 +1904,8 @@ class Snapshot final : public PBackgroundLSSnapshotParent {
Snapshot(Database* aDatabase, const nsAString& aDocumentURI);
void Init(nsTHashtable<nsStringHashKey>& aLoadedItems,
nsTHashtable<nsStringHashKey>& aUnknownItems,
uint32_t aNextLoadIndex, uint32_t aTotalLength,
int64_t aInitialUsage, int64_t aPeakUsage,
nsTHashSet<nsString>&& aUnknownItems, uint32_t aNextLoadIndex,
uint32_t aTotalLength, int64_t aInitialUsage, int64_t aPeakUsage,
LSSnapshot::LoadState aLoadState, bool aHasOtherProcessObservers) {
AssertIsOnBackgroundThread();
MOZ_ASSERT(aInitialUsage >= 0);
@ -1918,7 +1917,7 @@ class Snapshot final : public PBackgroundLSSnapshotParent {
MOZ_ASSERT(mPeakUsage == -1);
mLoadedItems.SwapElements(aLoadedItems);
mUnknownItems.SwapElements(aUnknownItems);
mUnknownItems = std::move(aUnknownItems);
mNextLoadIndex = aNextLoadIndex;
mTotalLength = aTotalLength;
mUsage = aInitialUsage;
@ -4322,22 +4321,22 @@ void Datastore::NoteLivePrepareDatastoreOp(
PrepareDatastoreOp* aPrepareDatastoreOp) {
AssertIsOnBackgroundThread();
MOZ_ASSERT(aPrepareDatastoreOp);
MOZ_ASSERT(!mPrepareDatastoreOps.GetEntry(aPrepareDatastoreOp));
MOZ_ASSERT(!mPrepareDatastoreOps.Contains(aPrepareDatastoreOp));
MOZ_ASSERT(mDirectoryLock);
MOZ_ASSERT(!mClosed);
mPrepareDatastoreOps.PutEntry(aPrepareDatastoreOp);
mPrepareDatastoreOps.Insert(aPrepareDatastoreOp);
}
void Datastore::NoteFinishedPrepareDatastoreOp(
PrepareDatastoreOp* aPrepareDatastoreOp) {
AssertIsOnBackgroundThread();
MOZ_ASSERT(aPrepareDatastoreOp);
MOZ_ASSERT(mPrepareDatastoreOps.GetEntry(aPrepareDatastoreOp));
MOZ_ASSERT(mPrepareDatastoreOps.Contains(aPrepareDatastoreOp));
MOZ_ASSERT(mDirectoryLock);
MOZ_ASSERT(!mClosed);
mPrepareDatastoreOps.RemoveEntry(aPrepareDatastoreOp);
mPrepareDatastoreOps.Remove(aPrepareDatastoreOp);
QuotaManager::GetRef().MaybeRecordShutdownStep(
quota::Client::LS, "PrepareDatastoreOp finished"_ns);
@ -4372,22 +4371,22 @@ void Datastore::NoteLivePreparedDatastore(
PreparedDatastore* aPreparedDatastore) {
AssertIsOnBackgroundThread();
MOZ_ASSERT(aPreparedDatastore);
MOZ_ASSERT(!mPreparedDatastores.GetEntry(aPreparedDatastore));
MOZ_ASSERT(!mPreparedDatastores.Contains(aPreparedDatastore));
MOZ_ASSERT(mDirectoryLock);
MOZ_ASSERT(!mClosed);
mPreparedDatastores.PutEntry(aPreparedDatastore);
mPreparedDatastores.Insert(aPreparedDatastore);
}
void Datastore::NoteFinishedPreparedDatastore(
PreparedDatastore* aPreparedDatastore) {
AssertIsOnBackgroundThread();
MOZ_ASSERT(aPreparedDatastore);
MOZ_ASSERT(mPreparedDatastores.GetEntry(aPreparedDatastore));
MOZ_ASSERT(mPreparedDatastores.Contains(aPreparedDatastore));
MOZ_ASSERT(mDirectoryLock);
MOZ_ASSERT(!mClosed);
mPreparedDatastores.RemoveEntry(aPreparedDatastore);
mPreparedDatastores.Remove(aPreparedDatastore);
QuotaManager::GetRef().MaybeRecordShutdownStep(
quota::Client::LS, "PreparedDatastore finished"_ns);
@ -4398,22 +4397,22 @@ void Datastore::NoteFinishedPreparedDatastore(
void Datastore::NoteLiveDatabase(Database* aDatabase) {
AssertIsOnBackgroundThread();
MOZ_ASSERT(aDatabase);
MOZ_ASSERT(!mDatabases.GetEntry(aDatabase));
MOZ_ASSERT(!mDatabases.Contains(aDatabase));
MOZ_ASSERT(mDirectoryLock);
MOZ_ASSERT(!mClosed);
mDatabases.PutEntry(aDatabase);
mDatabases.Insert(aDatabase);
}
void Datastore::NoteFinishedDatabase(Database* aDatabase) {
AssertIsOnBackgroundThread();
MOZ_ASSERT(aDatabase);
MOZ_ASSERT(mDatabases.GetEntry(aDatabase));
MOZ_ASSERT(!mActiveDatabases.GetEntry(aDatabase));
MOZ_ASSERT(mDatabases.Contains(aDatabase));
MOZ_ASSERT(!mActiveDatabases.Contains(aDatabase));
MOZ_ASSERT(mDirectoryLock);
MOZ_ASSERT(!mClosed);
mDatabases.RemoveEntry(aDatabase);
mDatabases.Remove(aDatabase);
QuotaManager::GetRef().MaybeRecordShutdownStep(quota::Client::LS,
"Database finished"_ns);
@ -4424,21 +4423,21 @@ void Datastore::NoteFinishedDatabase(Database* aDatabase) {
void Datastore::NoteActiveDatabase(Database* aDatabase) {
AssertIsOnBackgroundThread();
MOZ_ASSERT(aDatabase);
MOZ_ASSERT(mDatabases.GetEntry(aDatabase));
MOZ_ASSERT(!mActiveDatabases.GetEntry(aDatabase));
MOZ_ASSERT(mDatabases.Contains(aDatabase));
MOZ_ASSERT(!mActiveDatabases.Contains(aDatabase));
MOZ_ASSERT(!mClosed);
mActiveDatabases.PutEntry(aDatabase);
mActiveDatabases.Insert(aDatabase);
}
void Datastore::NoteInactiveDatabase(Database* aDatabase) {
AssertIsOnBackgroundThread();
MOZ_ASSERT(aDatabase);
MOZ_ASSERT(mDatabases.GetEntry(aDatabase));
MOZ_ASSERT(mActiveDatabases.GetEntry(aDatabase));
MOZ_ASSERT(mDatabases.Contains(aDatabase));
MOZ_ASSERT(mActiveDatabases.Contains(aDatabase));
MOZ_ASSERT(!mClosed);
mActiveDatabases.RemoveEntry(aDatabase);
mActiveDatabases.Remove(aDatabase);
if (!mActiveDatabases.Count() && mPendingUsageDeltas.Length()) {
int64_t finalDelta = 0;
@ -4926,9 +4925,7 @@ void Datastore::NoteChangedObserverArray(
const nsTArray<NotNull<Observer*>>& aObservers) {
AssertIsOnBackgroundThread();
for (auto iter = mActiveDatabases.ConstIter(); !iter.Done(); iter.Next()) {
Database* database = iter.Get()->GetKey();
for (Database* database : mActiveDatabases) {
Snapshot* snapshot = database->GetSnapshot();
MOZ_ASSERT(snapshot);
@ -5089,9 +5086,7 @@ void Datastore::NotifySnapshots(Database* aDatabase, const nsAString& aKey,
const LSValue& aOldValue, bool aAffectsOrder) {
AssertIsOnBackgroundThread();
for (auto iter = mDatabases.ConstIter(); !iter.Done(); iter.Next()) {
Database* database = iter.Get()->GetKey();
for (Database* database : mDatabases) {
MOZ_ASSERT(database);
if (database == aDatabase) {
@ -5368,9 +5363,9 @@ mozilla::ipc::IPCResult Database::RecvPBackgroundLSSnapshotConstructor(
mDatastore->GetSnapshotLoadInfo(aKey, addKeyToUnknownItems, loadedItems,
itemInfos, nextLoadIndex, loadState);
nsTHashtable<nsStringHashKey> unknownItems;
nsTHashSet<nsString> unknownItems;
if (addKeyToUnknownItems) {
unknownItems.PutEntry(aKey);
unknownItems.Insert(aKey);
}
uint32_t totalLength = mDatastore->GetLength();
@ -5386,8 +5381,9 @@ mozilla::ipc::IPCResult Database::RecvPBackgroundLSSnapshotConstructor(
bool hasOtherProcessObservers = mDatastore->HasOtherProcessObservers(this);
snapshot->Init(loadedItems, unknownItems, nextLoadIndex, totalLength,
initialUsage, peakUsage, loadState, hasOtherProcessObservers);
snapshot->Init(loadedItems, std::move(unknownItems), nextLoadIndex,
totalLength, initialUsage, peakUsage, loadState,
hasOtherProcessObservers);
RegisterSnapshot(snapshot);
@ -5450,7 +5446,7 @@ void Snapshot::SaveItem(const nsAString& aKey, const LSValue& aOldValue,
return;
}
if (!mLoadedItems.GetEntry(aKey) && !mUnknownItems.GetEntry(aKey)) {
if (!mLoadedItems.Contains(aKey) && !mUnknownItems.Contains(aKey)) {
mValues.LookupOrInsert(aKey, aOldValue);
}
@ -5698,7 +5694,7 @@ mozilla::ipc::IPCResult Snapshot::RecvLoadValueAndMoreItems(
return IPC_FAIL_NO_REASON(this);
}
if (mLoadedItems.GetEntry(aKey) || mUnknownItems.GetEntry(aKey)) {
if (mLoadedItems.Contains(aKey) || mUnknownItems.Contains(aKey)) {
ASSERT_UNLESS_FUZZING();
return IPC_FAIL_NO_REASON(this);
}
@ -5711,7 +5707,7 @@ mozilla::ipc::IPCResult Snapshot::RecvLoadValueAndMoreItems(
}
if (aValue->IsVoid()) {
mUnknownItems.PutEntry(aKey);
mUnknownItems.Insert(aKey);
} else {
mLoadedItems.PutEntry(aKey);
@ -8499,16 +8495,17 @@ nsCString QuotaClient::GetShutdownStatus() const {
data.AppendInt(static_cast<uint32_t>(gPrepareDatastoreOps->Length()));
data.Append(" (");
nsTHashtable<nsCStringHashKey> ids;
// XXX What's the purpose of adding these to a hashtable before joining them
// to the string? (Maybe this used to be an ordered container before???)
nsTHashSet<nsCString> ids;
std::transform(gPrepareDatastoreOps->cbegin(), gPrepareDatastoreOps->cend(),
MakeInserter(ids), [](const auto& prepareDatastoreOp) {
nsCString id;
prepareDatastoreOp->Stringify(id);
return id;
});
for (const auto& prepareDatastoreOp : *gPrepareDatastoreOps) {
nsCString id;
prepareDatastoreOp->Stringify(id);
ids.PutEntry(id);
}
StringifyTableKeys(ids, data);
StringJoinAppend(data, ", "_ns, ids);
data.Append(")\n");
}
@ -8518,16 +8515,17 @@ nsCString QuotaClient::GetShutdownStatus() const {
data.AppendInt(gDatastores->Count());
data.Append(" (");
nsTHashtable<nsCStringHashKey> ids;
// XXX It might be confusing to remove duplicates here, as the actual list
// won't match the count then.
nsTHashSet<nsCString> ids;
std::transform(gDatastores->Values().cbegin(), gDatastores->Values().cend(),
MakeInserter(ids), [](const auto& entry) {
nsCString id;
entry->Stringify(id);
return id;
});
for (const auto& entry : *gDatastores) {
nsCString id;
entry.GetData()->Stringify(id);
ids.PutEntry(id);
}
StringifyTableKeys(ids, data);
StringJoinAppend(data, ", "_ns, ids);
data.Append(")\n");
}
@ -8537,16 +8535,17 @@ nsCString QuotaClient::GetShutdownStatus() const {
data.AppendInt(static_cast<uint32_t>(gLiveDatabases->Length()));
data.Append(" (");
nsTHashtable<nsCStringHashKey> ids;
// XXX It might be confusing to remove duplicates here, as the actual list
// won't match the count then.
nsTHashSet<nsCString> ids;
std::transform(gLiveDatabases->cbegin(), gLiveDatabases->cend(),
MakeInserter(ids), [](const auto& database) {
nsCString id;
database->Stringify(id);
return id;
});
for (const auto& database : *gLiveDatabases) {
nsCString id;
database->Stringify(id);
ids.PutEntry(id);
}
StringifyTableKeys(ids, data);
StringJoinAppend(data, ", "_ns, ids);
data.Append(")\n");
}

View File

@ -42,7 +42,6 @@
#include "nsStringFlags.h"
#include "nsStringFwd.h"
#include "nsTArray.h"
#include "nsTHashtable.h"
#include "nsTStringRepr.h"
#include "nscore.h"
@ -199,7 +198,7 @@ nsresult LSSnapshot::Init(const nsAString& aKey,
const LSValue& value = itemInfo.value();
if (loadState != LoadState::AllOrderedItems && !value.IsVoid()) {
mLoadedItems.PutEntry(itemInfo.key());
mLoadedItems.Insert(itemInfo.key());
}
mValues.InsertOrUpdate(itemInfo.key(), value.AsString());
@ -207,7 +206,7 @@ nsresult LSSnapshot::Init(const nsAString& aKey,
if (loadState == LoadState::Partial) {
if (aInitInfo.addKeyToUnknownItems()) {
mUnknownItems.PutEntry(aKey);
mUnknownItems.Insert(aKey);
}
mInitLength = aInitInfo.totalLength();
mLength = mInitLength;
@ -630,7 +629,7 @@ nsresult LSSnapshot::GetItemInternal(const nsAString& aKey,
case LoadState::Partial: {
if (mValues.Get(aKey, &result)) {
MOZ_ASSERT(!result.IsVoid());
} else if (mLoadedItems.GetEntry(aKey) || mUnknownItems.GetEntry(aKey)) {
} else if (mLoadedItems.Contains(aKey) || mUnknownItems.Contains(aKey)) {
result.SetIsVoid(true);
} else {
LSValue value;
@ -643,9 +642,9 @@ nsresult LSSnapshot::GetItemInternal(const nsAString& aKey,
result = value.AsString();
if (result.IsVoid()) {
mUnknownItems.PutEntry(aKey);
mUnknownItems.Insert(aKey);
} else {
mLoadedItems.PutEntry(aKey);
mLoadedItems.Insert(aKey);
mValues.InsertOrUpdate(aKey, result);
// mLoadedItems.Count()==mInitLength is checked below.
@ -654,7 +653,7 @@ nsresult LSSnapshot::GetItemInternal(const nsAString& aKey,
for (uint32_t i = 0; i < itemInfos.Length(); i++) {
const LSItemInfo& itemInfo = itemInfos[i];
mLoadedItems.PutEntry(itemInfo.key());
mLoadedItems.Insert(itemInfo.key());
mValues.InsertOrUpdate(itemInfo.key(), itemInfo.value().AsString());
}
@ -692,7 +691,7 @@ nsresult LSSnapshot::GetItemInternal(const nsAString& aKey,
MOZ_ASSERT(!result.IsVoid());
mLoadedItems.PutEntry(aKey);
mLoadedItems.Insert(aKey);
mValues.InsertOrUpdate(aKey, result);
// mLoadedItems.Count()==mInitLength is checked below.
@ -700,7 +699,7 @@ nsresult LSSnapshot::GetItemInternal(const nsAString& aKey,
for (uint32_t i = 0; i < itemInfos.Length(); i++) {
const LSItemInfo& itemInfo = itemInfos[i];
mLoadedItems.PutEntry(itemInfo.key());
mLoadedItems.Insert(itemInfo.key());
mValues.InsertOrUpdate(itemInfo.key(), itemInfo.value().AsString());
}

View File

@ -20,7 +20,7 @@
#include "nsISupports.h"
#include "nsStringFwd.h"
#include "nsTArrayForwardDeclare.h"
#include "nsTHashtable.h"
#include "nsTHashSet.h"
class nsITimer;
@ -95,8 +95,8 @@ class LSSnapshot final : public nsIRunnable {
LSSnapshotChild* mActor;
nsTHashtable<nsStringHashKey> mLoadedItems;
nsTHashtable<nsStringHashKey> mUnknownItems;
nsTHashSet<nsString> mLoadedItems;
nsTHashSet<nsString> mUnknownItems;
nsTHashMap<nsStringHashKey, nsString> mValues;
UniquePtr<SnapshotWriteOptimizer> mWriteOptimizer;
UniquePtr<nsTArray<LSWriteAndNotifyInfo>> mWriteAndNotifyInfos;