Bug 1368291 - Style sharing for lazy pseudos. r=emilio

MozReview-Commit-ID: 9u8FzDXFZcX
This commit is contained in:
Bobby Holley 2017-08-06 09:42:55 -07:00
Родитель b36bbd4e9a
Коммит 499e5efc52
4 изменённых файлов: 109 добавлений и 16 удалений

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

@ -52,4 +52,25 @@ ServoStyleContext::GetCachedInheritingAnonBoxStyle(nsIAtom* aAnonBox) const
return current;
}
ServoStyleContext*
ServoStyleContext::GetCachedLazyPseudoStyle(CSSPseudoElementType aPseudo) const
{
MOZ_ASSERT(aPseudo != CSSPseudoElementType::NotPseudo &&
aPseudo != CSSPseudoElementType::InheritingAnonBox &&
aPseudo != CSSPseudoElementType::NonInheritingAnonBox);
MOZ_ASSERT(!IsLazilyCascadedPseudoElement(), "Lazy pseudos can't inherit lazy pseudos");
if (nsCSSPseudoElements::PseudoElementSupportsUserActionState(aPseudo)) {
return nullptr;
}
auto* current = mNextLazyPseudoStyle.get();
while (current && current->GetPseudoType() != aPseudo) {
current = current->mNextLazyPseudoStyle.get();
}
return current;
}
} // namespace mozilla

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

@ -34,13 +34,19 @@ public:
return ComputedData()->visited_style.mPtr;
}
bool IsLazilyCascadedPseudoElement() const
{
return IsPseudoElement() &&
!nsCSSPseudoElements::IsEagerlyCascadedInServo(GetPseudoType());
}
ServoStyleContext* GetCachedInheritingAnonBoxStyle(nsIAtom* aAnonBox) const;
void SetCachedInheritedAnonBoxStyle(nsIAtom* aAnonBox,
ServoStyleContext& aStyle)
ServoStyleContext* aStyle)
{
MOZ_ASSERT(!GetCachedInheritingAnonBoxStyle(aAnonBox));
MOZ_ASSERT(!aStyle.mNextInheritingAnonBoxStyle);
MOZ_ASSERT(!aStyle->mNextInheritingAnonBoxStyle);
// NOTE(emilio): Since we use it to cache inheriting anon boxes in a linked
// list, we can't use that cache if the style we're inheriting from is an
@ -52,8 +58,35 @@ public:
return;
}
mNextInheritingAnonBoxStyle.swap(aStyle.mNextInheritingAnonBoxStyle);
mNextInheritingAnonBoxStyle = &aStyle;
mNextInheritingAnonBoxStyle.swap(aStyle->mNextInheritingAnonBoxStyle);
mNextInheritingAnonBoxStyle = aStyle;
}
ServoStyleContext* GetCachedLazyPseudoStyle(CSSPseudoElementType aPseudo) const;
void SetCachedLazyPseudoStyle(ServoStyleContext* aStyle)
{
MOZ_ASSERT(aStyle->GetPseudo() && !aStyle->IsAnonBox());
MOZ_ASSERT(!GetCachedLazyPseudoStyle(aStyle->GetPseudoType()));
MOZ_ASSERT(!aStyle->mNextLazyPseudoStyle);
MOZ_ASSERT(!IsLazilyCascadedPseudoElement(), "lazy pseudos can't inherit lazy pseudos");
MOZ_ASSERT(aStyle->IsLazilyCascadedPseudoElement());
// Since we're caching lazy pseudo styles on the ComputedValues of the
// originating element, we can assume that we either have the same
// originating element, or that they were at least similar enough to share
// the same ComputedValues, which means that they would match the same
// pseudo rules. This allows us to avoid matching selectors and checking
// the rule node before deciding to share.
//
// The one place this optimization breaks is with pseudo-elements that
// support state (like :hover). So we just avoid sharing in those cases.
if (nsCSSPseudoElements::PseudoElementSupportsUserActionState(aStyle->GetPseudoType())) {
return;
}
mNextLazyPseudoStyle.swap(aStyle->mNextLazyPseudoStyle);
mNextLazyPseudoStyle = aStyle;
}
/**
@ -72,6 +105,17 @@ private:
// Otherwise it represents the next entry in the cache of the parent style
// context.
RefPtr<ServoStyleContext> mNextInheritingAnonBoxStyle;
// A linked-list cache of lazy pseudo styles inheriting from this style _if
// the style isn't a lazy pseudo style itself_.
//
// Otherwise it represents the next entry in the cache of the parent style
// context.
//
// Note that we store these separately from inheriting anonymous boxes so that
// text nodes inheriting from lazy pseudo styles can share styles, which is
// very important on some pages.
RefPtr<ServoStyleContext> mNextLazyPseudoStyle;
};
} // namespace mozilla

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

@ -414,7 +414,7 @@ ResolveStyleForTextOrFirstLetterContinuation(
&aParent,
inheritTarget).Consume();
MOZ_ASSERT(style);
aParent.SetCachedInheritedAnonBoxStyle(aAnonBox, *style);
aParent.SetCachedInheritedAnonBoxStyle(aAnonBox, style);
}
return style.forget();
@ -482,12 +482,21 @@ ServoStyleSet::ResolvePseudoElementStyle(Element* aOriginatingElement,
mRawSet.get(),
ServoTraversalFlags::Empty).Consume();
} else {
bool cacheable =
!nsCSSPseudoElements::IsEagerlyCascadedInServo(aType) && aParentContext;
computedValues =
Servo_ResolvePseudoStyle(aOriginatingElement,
aType,
/* is_probe = */ false,
aParentContext,
mRawSet.get()).Consume();
cacheable ? aParentContext->GetCachedLazyPseudoStyle(aType) : nullptr;
if (!computedValues) {
computedValues = Servo_ResolvePseudoStyle(aOriginatingElement,
aType,
/* is_probe = */ false,
aParentContext,
mRawSet.get()).Consume();
if (cacheable) {
aParentContext->SetCachedLazyPseudoStyle(computedValues);
}
}
}
MOZ_ASSERT(computedValues);
@ -547,7 +556,7 @@ ServoStyleSet::ResolveInheritingAnonymousBoxStyle(nsIAtom* aPseudoTag,
mRawSet.get()).Consume();
MOZ_ASSERT(style);
if (aParentContext) {
aParentContext->SetCachedInheritedAnonBoxStyle(aPseudoTag, *style);
aParentContext->SetCachedInheritedAnonBoxStyle(aPseudoTag, style);
}
}
@ -809,13 +818,25 @@ ServoStyleSet::ProbePseudoElementStyle(Element* aOriginatingElement,
// aOriginatingElement's styles anyway.
MOZ_ASSERT(aType < CSSPseudoElementType::Count);
bool cacheable =
!nsCSSPseudoElements::IsEagerlyCascadedInServo(aType) && aParentContext;
RefPtr<ServoStyleContext> computedValues =
Servo_ResolvePseudoStyle(aOriginatingElement, aType,
/* is_probe = */ true,
nullptr,
mRawSet.get()).Consume();
cacheable ? aParentContext->GetCachedLazyPseudoStyle(aType) : nullptr;
if (!computedValues) {
return nullptr;
computedValues = Servo_ResolvePseudoStyle(aOriginatingElement, aType,
/* is_probe = */ true,
nullptr,
mRawSet.get()).Consume();
if (!computedValues) {
return nullptr;
}
if (cacheable) {
// NB: We don't need to worry about the before/after handling below
// because those are eager and thus not |cacheable| anyway.
aParentContext->SetCachedLazyPseudoStyle(computedValues);
}
}
// For :before and :after pseudo-elements, having display: none or no

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

@ -15,6 +15,8 @@
// Is this pseudo-element a CSS2 pseudo-element that can be specified
// with the single colon syntax (in addition to the double-colon syntax,
// which can be used for all pseudo-elements)?
//
// Note: We also rely on this for IsEagerlyCascadedInServo.
#define CSS_PSEUDO_ELEMENT_IS_CSS2 (1<<0)
// Is this pseudo-element a pseudo-element that can contain other
// elements?
@ -85,6 +87,11 @@ public:
static bool IsCSS2PseudoElement(nsIAtom *aAtom);
static bool IsEagerlyCascadedInServo(const Type aType)
{
return PseudoElementHasFlags(aType, CSS_PSEUDO_ELEMENT_IS_CSS2);
}
#define CSS_PSEUDO_ELEMENT(_name, _value, _flags) \
static nsICSSPseudoElement* _name;
#include "nsCSSPseudoElementList.h"