Skip to content

Commit

Permalink
Bug 77999 - Part 2: Add RuleProcessorCache. r=dbaron
Browse files Browse the repository at this point in the history
  • Loading branch information
heycam committed Jun 26, 2015
1 parent ea9aba1 commit 7c894d7
Show file tree
Hide file tree
Showing 8 changed files with 488 additions and 2 deletions.
2 changes: 2 additions & 0 deletions layout/build/nsLayoutStatics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "nsGkAtoms.h"
#include "nsImageFrame.h"
#include "nsLayoutStylesheetCache.h"
#include "mozilla/RuleProcessorCache.h"
#include "nsPrincipal.h"
#include "nsRange.h"
#include "nsRegion.h"
Expand Down Expand Up @@ -376,6 +377,7 @@ nsLayoutStatics::Shutdown()
nsAttrValue::Shutdown();
nsContentUtils::Shutdown();
nsLayoutStylesheetCache::Shutdown();
RuleProcessorCache::Shutdown();

ShutdownJSEnvironment();
nsGlobalWindow::ShutDown();
Expand Down
14 changes: 14 additions & 0 deletions layout/style/CSSStyleSheet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include "mozilla/dom/CSSStyleSheetBinding.h"
#include "nsComponentManagerUtils.h"
#include "nsNullPrincipal.h"
#include "mozilla/RuleProcessorCache.h"

using namespace mozilla;
using namespace mozilla::dom;
Expand Down Expand Up @@ -1075,6 +1076,7 @@ CSSStyleSheet::CSSStyleSheet(CORSMode aCORSMode, ReferrerPolicy aReferrerPolicy)
mOwningNode(nullptr),
mDisabled(false),
mDirty(false),
mInRuleProcessorCache(false),
mScopeElement(nullptr),
mRuleProcessors(nullptr)
{
Expand All @@ -1093,6 +1095,7 @@ CSSStyleSheet::CSSStyleSheet(const CSSStyleSheet& aCopy,
mOwningNode(aOwningNodeToUse),
mDisabled(aCopy.mDisabled),
mDirty(aCopy.mDirty),
mInRuleProcessorCache(false),
mScopeElement(nullptr),
mInner(aCopy.mInner),
mRuleProcessors(nullptr)
Expand Down Expand Up @@ -1135,6 +1138,9 @@ CSSStyleSheet::~CSSStyleSheet()
NS_ASSERTION(mRuleProcessors->Length() == 0, "destructing sheet with rule processor reference");
delete mRuleProcessors; // weak refs, should be empty here anyway
}
if (mInRuleProcessorCache) {
RuleProcessorCache::RemoveSheet(this);
}
}

void
Expand Down Expand Up @@ -1703,10 +1709,18 @@ CSSStyleSheet::List(FILE* out, int32_t aIndent) const
void
CSSStyleSheet::ClearRuleCascades()
{
bool removedSheetFromRuleProcessorCache = false;
if (mRuleProcessors) {
nsCSSRuleProcessor **iter = mRuleProcessors->Elements(),
**end = iter + mRuleProcessors->Length();
for(; iter != end; ++iter) {
if (!removedSheetFromRuleProcessorCache && (*iter)->IsShared()) {
// Since the sheet has been modified, we need to remove all
// RuleProcessorCache entries that contain this sheet, as the
// list of @-moz-document rules might have changed.
RuleProcessorCache::RemoveSheet(this);
removedSheetFromRuleProcessorCache = true;
}
(*iter)->ClearRuleCascades();
}
}
Expand Down
3 changes: 3 additions & 0 deletions layout/style/CSSStyleSheet.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ class CSSStyleSheet final : public nsIStyleSheet,

nsresult ParseSheet(const nsAString& aInput);

void SetInRuleProcessorCache() { mInRuleProcessorCache = true; }

// nsIDOMStyleSheet interface
NS_DECL_NSIDOMSTYLESHEET

Expand Down Expand Up @@ -357,6 +359,7 @@ class CSSStyleSheet final : public nsIStyleSheet,
nsINode* mOwningNode; // weak ref
bool mDisabled;
bool mDirty; // has been modified
bool mInRuleProcessorCache;
nsRefPtr<dom::Element> mScopeElement;

CSSStyleSheetInner* mInner;
Expand Down
284 changes: 284 additions & 0 deletions layout/style/RuleProcessorCache.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,284 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/*
* cache of re-usable nsCSSRuleProcessors for given sets of style sheets
*/

#include "RuleProcessorCache.h"

#include <algorithm>
#include "nsCSSRuleProcessor.h"
#include "nsThreadUtils.h"

using namespace mozilla;

NS_IMPL_ISUPPORTS(RuleProcessorCache, nsIMemoryReporter)

MOZ_DEFINE_MALLOC_SIZE_OF(RuleProcessorCacheMallocSizeOf)

NS_IMETHODIMP
RuleProcessorCache::CollectReports(nsIHandleReportCallback* aHandleReport,
nsISupports* aData, bool aAnonymize)
{
return MOZ_COLLECT_REPORT(
"explicit/layout/rule-processor-cache", KIND_HEAP, UNITS_BYTES,
SizeOfIncludingThis(RuleProcessorCacheMallocSizeOf),
"Memory used for cached rule processors.");
}

RuleProcessorCache::~RuleProcessorCache()
{
UnregisterWeakMemoryReporter(this);

for (Entry& e : mEntries) {
for (DocumentEntry& de : e.mDocumentEntries) {
if (de.mRuleProcessor->GetExpirationState()->IsTracked()) {
mExpirationTracker.RemoveObject(de.mRuleProcessor);
}
de.mRuleProcessor->SetInRuleProcessorCache(false);
}
}
}

void
RuleProcessorCache::InitMemoryReporter()
{
RegisterWeakMemoryReporter(this);
}

/* static */ bool
RuleProcessorCache::EnsureGlobal()
{
MOZ_ASSERT(NS_IsMainThread());

if (gShutdown) {
return false;
}

if (!gRuleProcessorCache) {
gRuleProcessorCache = new RuleProcessorCache;
gRuleProcessorCache->InitMemoryReporter();
}
return true;
}

/* static */ void
RuleProcessorCache::RemoveSheet(CSSStyleSheet* aSheet)
{
if (!EnsureGlobal()) {
return;
}
gRuleProcessorCache->DoRemoveSheet(aSheet);
}

#ifdef DEBUG
/* static */ bool
RuleProcessorCache::HasRuleProcessor(nsCSSRuleProcessor* aRuleProcessor)
{
if (!EnsureGlobal()) {
return false;
}
return gRuleProcessorCache->DoHasRuleProcessor(aRuleProcessor);
}
#endif

/* static */ void
RuleProcessorCache::RemoveRuleProcessor(nsCSSRuleProcessor* aRuleProcessor)
{
if (!EnsureGlobal()) {
return;
}
gRuleProcessorCache->DoRemoveRuleProcessor(aRuleProcessor);
}

/* static */ nsCSSRuleProcessor*
RuleProcessorCache::GetRuleProcessor(const nsTArray<CSSStyleSheet*>& aSheets,
nsPresContext* aPresContext)
{
if (!EnsureGlobal()) {
return nullptr;
}
return gRuleProcessorCache->DoGetRuleProcessor(aSheets, aPresContext);
}

/* static */ void
RuleProcessorCache::PutRuleProcessor(
const nsTArray<CSSStyleSheet*>& aSheets,
nsTArray<css::DocumentRule*>&& aDocumentRulesInSheets,
const nsDocumentRuleResultCacheKey& aCacheKey,
nsCSSRuleProcessor* aRuleProcessor)
{
if (!EnsureGlobal()) {
return;
}
gRuleProcessorCache->DoPutRuleProcessor(aSheets, Move(aDocumentRulesInSheets),
aCacheKey, aRuleProcessor);
}

/* static */ void
RuleProcessorCache::StartTracking(nsCSSRuleProcessor* aRuleProcessor)
{
if (!EnsureGlobal()) {
return;
}
return gRuleProcessorCache->DoStartTracking(aRuleProcessor);
}

/* static */ void
RuleProcessorCache::StopTracking(nsCSSRuleProcessor* aRuleProcessor)
{
if (!EnsureGlobal()) {
return;
}
return gRuleProcessorCache->DoStopTracking(aRuleProcessor);
}

void
RuleProcessorCache::DoRemoveSheet(CSSStyleSheet* aSheet)
{
Entry* last = std::remove_if(mEntries.begin(), mEntries.end(),
HasSheet_ThenRemoveRuleProcessors(this, aSheet));
mEntries.TruncateLength(last - mEntries.begin());
}

nsCSSRuleProcessor*
RuleProcessorCache::DoGetRuleProcessor(const nsTArray<CSSStyleSheet*>& aSheets,
nsPresContext* aPresContext)
{
for (Entry& e : mEntries) {
if (e.mSheets == aSheets) {
for (DocumentEntry& de : e.mDocumentEntries) {
if (de.mCacheKey.Matches(aPresContext, e.mDocumentRulesInSheets)) {
return de.mRuleProcessor;
}
}
// Entry::mSheets is unique; if we matched aSheets but didn't
// find a matching DocumentEntry, we won't find one later in
// mEntries.
return nullptr;
}
}
return nullptr;
}

void
RuleProcessorCache::DoPutRuleProcessor(
const nsTArray<CSSStyleSheet*>& aSheets,
nsTArray<css::DocumentRule*>&& aDocumentRulesInSheets,
const nsDocumentRuleResultCacheKey& aCacheKey,
nsCSSRuleProcessor* aRuleProcessor)
{
MOZ_ASSERT(!aRuleProcessor->IsInRuleProcessorCache());

Entry* entry = nullptr;
for (Entry& e : mEntries) {
if (e.mSheets == aSheets) {
entry = &e;
break;
}
}

if (!entry) {
entry = mEntries.AppendElement();
entry->mSheets = aSheets;
entry->mDocumentRulesInSheets = aDocumentRulesInSheets;
for (CSSStyleSheet* sheet : aSheets) {
sheet->SetInRuleProcessorCache();
}
} else {
MOZ_ASSERT(entry->mDocumentRulesInSheets == aDocumentRulesInSheets,
"DocumentRule array shouldn't have changed");
}

#ifdef DEBUG
for (DocumentEntry& de : entry->mDocumentEntries) {
MOZ_ASSERT(de.mCacheKey != aCacheKey,
"should not have duplicate document cache keys");
}
#endif

DocumentEntry* documentEntry = entry->mDocumentEntries.AppendElement();
documentEntry->mCacheKey = aCacheKey;
documentEntry->mRuleProcessor = aRuleProcessor;
aRuleProcessor->SetInRuleProcessorCache(true);
}

#ifdef DEBUG
bool
RuleProcessorCache::DoHasRuleProcessor(nsCSSRuleProcessor* aRuleProcessor)
{
for (Entry& e : mEntries) {
for (DocumentEntry& de : e.mDocumentEntries) {
if (de.mRuleProcessor == aRuleProcessor) {
return true;
}
}
}
return false;
}
#endif

void
RuleProcessorCache::DoRemoveRuleProcessor(nsCSSRuleProcessor* aRuleProcessor)
{
MOZ_ASSERT(aRuleProcessor->IsInRuleProcessorCache());

aRuleProcessor->SetInRuleProcessorCache(false);
mExpirationTracker.RemoveObjectIfTracked(aRuleProcessor);
for (Entry& e : mEntries) {
for (size_t i = 0; i < e.mDocumentEntries.Length(); i++) {
if (e.mDocumentEntries[i].mRuleProcessor == aRuleProcessor) {
e.mDocumentEntries.RemoveElementAt(i);
return;
}
}
}

MOZ_ASSERT_UNREACHABLE("should have found rule processor");
}

void
RuleProcessorCache::DoStartTracking(nsCSSRuleProcessor* aRuleProcessor)
{
mExpirationTracker.AddObject(aRuleProcessor);
}

void
RuleProcessorCache::DoStopTracking(nsCSSRuleProcessor* aRuleProcessor)
{
mExpirationTracker.RemoveObjectIfTracked(aRuleProcessor);
}

size_t
RuleProcessorCache::SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf)
{
size_t n = aMallocSizeOf(this);

int count = 0;
n += mEntries.SizeOfExcludingThis(aMallocSizeOf);
for (Entry& e : mEntries) {
n += e.mDocumentEntries.SizeOfExcludingThis(aMallocSizeOf);
for (DocumentEntry& de : e.mDocumentEntries) {
count++;
n += de.mRuleProcessor->SizeOfIncludingThis(aMallocSizeOf);
}
}

return n;
}

void
RuleProcessorCache::ExpirationTracker::RemoveObjectIfTracked(
nsCSSRuleProcessor* aRuleProcessor)
{
if (aRuleProcessor->GetExpirationState()->IsTracked()) {
RemoveObject(aRuleProcessor);
}
}

bool RuleProcessorCache::gShutdown = false;
mozilla::StaticRefPtr<RuleProcessorCache> RuleProcessorCache::gRuleProcessorCache;
Loading

0 comments on commit 7c894d7

Please sign in to comment.