diff options
author | Moonchild <git-repo@palemoon.org> | 2019-05-25 12:32:37 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-25 12:32:37 +0000 |
commit | 915ffc660053cc48153c00dec66b913517dba794 (patch) | |
tree | 3342c893aaa82e08e6b019c567f233e48632104f /xpcom/ds | |
parent | 72260727093c92eaca78a3ea4d32bfafa49af8c3 (diff) | |
parent | c0c64138caf25dfd8d8b87e51f36a35311fab823 (diff) | |
download | UXP-915ffc660053cc48153c00dec66b913517dba794.tar UXP-915ffc660053cc48153c00dec66b913517dba794.tar.gz UXP-915ffc660053cc48153c00dec66b913517dba794.tar.lz UXP-915ffc660053cc48153c00dec66b913517dba794.tar.xz UXP-915ffc660053cc48153c00dec66b913517dba794.zip |
Merge pull request #1115 from win7-7/NS_Atomize-cache
Add NS_Atomize cache for main thread use
Diffstat (limited to 'xpcom/ds')
-rw-r--r-- | xpcom/ds/nsAtomTable.cpp | 50 | ||||
-rw-r--r-- | xpcom/ds/nsIAtom.idl | 5 |
2 files changed, 53 insertions, 2 deletions
diff --git a/xpcom/ds/nsAtomTable.cpp b/xpcom/ds/nsAtomTable.cpp index 3dd3bd36c..4c93625ae 100644 --- a/xpcom/ds/nsAtomTable.cpp +++ b/xpcom/ds/nsAtomTable.cpp @@ -364,17 +364,29 @@ static const PLDHashTableOps AtomTableOps = { //---------------------------------------------------------------------- +#define RECENTLY_USED_MAIN_THREAD_ATOM_CACHE_SIZE 31 +static nsIAtom* + sRecentlyUsedMainThreadAtoms[RECENTLY_USED_MAIN_THREAD_ATOM_CACHE_SIZE] = {}; + void DynamicAtom::GCAtomTable() { - MutexAutoLock lock(*gAtomTableLock); - GCAtomTableLocked(lock, GCKind::RegularOperation); + if (NS_IsMainThread()) { + MutexAutoLock lock(*gAtomTableLock); + GCAtomTableLocked(lock, GCKind::RegularOperation); + } } void DynamicAtom::GCAtomTableLocked(const MutexAutoLock& aProofOfLock, GCKind aKind) { + + MOZ_ASSERT(NS_IsMainThread()); + for (uint32_t i = 0; i < RECENTLY_USED_MAIN_THREAD_ATOM_CACHE_SIZE; ++i) { + sRecentlyUsedMainThreadAtoms[i] = nullptr; + } + uint32_t removedCount = 0; // Use a non-atomic temporary for cheaper increments. nsAutoCString nonZeroRefcountAtoms; uint32_t nonZeroRefcountAtomsCount = 0; @@ -712,6 +724,40 @@ NS_Atomize(const nsAString& aUTF16String) return atom.forget(); } +already_AddRefed<nsIAtom> +NS_AtomizeMainThread(const nsAString& aUTF16String) +{ + MOZ_ASSERT(NS_IsMainThread()); + nsCOMPtr<nsIAtom> retVal; + uint32_t hash; + AtomTableKey key(aUTF16String.Data(), aUTF16String.Length(), &hash); + uint32_t index = hash % RECENTLY_USED_MAIN_THREAD_ATOM_CACHE_SIZE; + nsIAtom* atom = + sRecentlyUsedMainThreadAtoms[index]; + if (atom) { + uint32_t length = atom->GetLength(); + if (length == key.mLength && + (memcmp(atom->GetUTF16String(), + key.mUTF16String, length * sizeof(char16_t)) == 0)) { + retVal = atom; + return retVal.forget(); + } + } + + MutexAutoLock lock(*gAtomTableLock); + AtomTableEntry* he = static_cast<AtomTableEntry*>(gAtomTable->Add(&key)); + + if (he->mAtom) { + retVal = he->mAtom; + } else { + retVal = DynamicAtom::Create(aUTF16String, hash); + he->mAtom = retVal; + } + + sRecentlyUsedMainThreadAtoms[index] = retVal; + return retVal.forget(); +} + nsrefcnt NS_GetNumberOfAtoms(void) { diff --git a/xpcom/ds/nsIAtom.idl b/xpcom/ds/nsIAtom.idl index c02540838..6e8602c42 100644 --- a/xpcom/ds/nsIAtom.idl +++ b/xpcom/ds/nsIAtom.idl @@ -120,6 +120,11 @@ extern already_AddRefed<nsIAtom> NS_Atomize(const char16_t* aUTF16String); extern already_AddRefed<nsIAtom> NS_Atomize(const nsAString& aUTF16String); /** + * An optimized version of the method above for the main thread. + */ +extern already_AddRefed<nsIAtom> NS_AtomizeMainThread(const nsAString& aUTF16String); + +/** * Return a count of the total number of atoms currently * alive in the system. */ |