Bug 598832 part 5. Move caching of nth-index stuff to the TreeMatchContext. r=dbaron

This commit is contained in:
Boris Zbarsky 2011-03-29 13:29:21 -04:00
parent 14b6974cbf
commit ed28ec7565
5 changed files with 303 additions and 102 deletions

View File

@ -131,6 +131,7 @@ CPPSRCS = \
nsHTMLStyleSheet.cpp \
nsLayoutStylesheetCache.cpp \
nsMediaFeatures.cpp \
nsNthIndexCache.cpp \
nsROCSSPrimitiveValue.cpp \
nsRuleData.cpp \
nsRuleNode.cpp \

View File

@ -93,6 +93,7 @@
#include "mozilla/Services.h"
#include "mozilla/dom/Element.h"
#include "nsGenericElement.h"
#include "nsNthIndexCache.h"
using namespace mozilla::dom;
namespace css = mozilla::css;
@ -1128,11 +1129,6 @@ RuleProcessorData::RuleProcessorData(nsPresContext* aPresContext,
NS_ASSERTION(aElement, "null element leaked into SelectorMatches");
mNthIndices[0][0] = -2;
mNthIndices[0][1] = -2;
mNthIndices[1][0] = -2;
mNthIndices[1][1] = -2;
NS_ASSERTION(aElement->GetOwnerDoc(), "Document-less node here?");
// get the tag and parent
@ -1281,80 +1277,6 @@ RuleProcessorData::GetContentStateForVisitedHandling(
return contentState;
}
PRInt32
RuleProcessorData::GetNthIndex(PRBool aIsOfType, PRBool aIsFromEnd,
PRBool aCheckEdgeOnly)
{
NS_ASSERTION(mParentContent, "caller should check mParentContent");
PRInt32 &slot = mNthIndices[aIsOfType][aIsFromEnd];
if (slot != -2 && (slot != -1 || aCheckEdgeOnly))
return slot;
if (mPreviousSiblingData &&
(!aIsOfType ||
(mPreviousSiblingData->mNameSpaceID == mNameSpaceID &&
mPreviousSiblingData->mContentTag == mContentTag))) {
slot = mPreviousSiblingData->mNthIndices[aIsOfType][aIsFromEnd];
if (slot > 0) {
slot += (aIsFromEnd ? -1 : 1);
NS_ASSERTION(slot > 0, "How did that happen?");
return slot;
}
}
PRInt32 result = 1;
nsIContent* parent = mParentContent;
PRUint32 childCount;
nsIContent * const * curChildPtr = parent->GetChildArray(&childCount);
#ifdef DEBUG
nsMutationGuard debugMutationGuard;
#endif
PRInt32 increment;
nsIContent * const * stopPtr;
if (aIsFromEnd) {
stopPtr = curChildPtr - 1;
curChildPtr = stopPtr + childCount;
increment = -1;
} else {
increment = 1;
stopPtr = curChildPtr + childCount;
}
for ( ; ; curChildPtr += increment) {
if (curChildPtr == stopPtr) {
// mContent is the root of an anonymous content subtree.
result = 0; // special value to indicate that it is not at any index
break;
}
nsIContent* child = *curChildPtr;
if (child == mElement)
break;
if (child->IsElement() &&
(!aIsOfType ||
(child->Tag() == mContentTag &&
child->GetNameSpaceID() == mNameSpaceID))) {
if (aCheckEdgeOnly) {
// The caller only cares whether or not the result is 1, and we
// now know it's not.
result = -1;
break;
}
++result;
}
}
#ifdef DEBUG
NS_ASSERTION(!debugMutationGuard.Mutated(0), "Unexpected mutations happened");
#endif
slot = result;
return result;
}
/**
* A |NodeMatchContext| has data about matching a selector (without
* combinators) against a single node. It contains only input to the
@ -1506,9 +1428,11 @@ edgeChildMatches(RuleProcessorData& data, TreeMatchContext& aTreeMatchContext,
parent->SetFlags(NODE_HAS_EDGE_CHILD_SELECTOR);
return (!checkFirst ||
data.GetNthIndex(PR_FALSE, PR_FALSE, PR_TRUE) == 1) &&
aTreeMatchContext.mNthIndexCache.
GetNthIndex(data.mElement, PR_FALSE, PR_FALSE, PR_TRUE) == 1) &&
(!checkLast ||
data.GetNthIndex(PR_FALSE, PR_TRUE, PR_TRUE) == 1);
aTreeMatchContext.mNthIndexCache.
GetNthIndex(data.mElement, PR_FALSE, PR_TRUE, PR_TRUE) == 1);
}
static inline PRBool
@ -1529,7 +1453,8 @@ nthChildGenericMatches(RuleProcessorData& data,
parent->SetFlags(NODE_HAS_SLOW_SELECTOR_LATER_SIBLINGS);
}
const PRInt32 index = data.GetNthIndex(isOfType, isFromEnd, PR_FALSE);
const PRInt32 index = aTreeMatchContext.mNthIndexCache.
GetNthIndex(data.mElement, isOfType, isFromEnd, PR_FALSE);
if (index <= 0) {
// Node is anonymous content (not really a child of its parent).
return PR_FALSE;
@ -1567,9 +1492,11 @@ edgeOfTypeMatches(RuleProcessorData& data, TreeMatchContext& aTreeMatchContext,
}
return (!checkFirst ||
data.GetNthIndex(PR_TRUE, PR_FALSE, PR_TRUE) == 1) &&
aTreeMatchContext.mNthIndexCache.
GetNthIndex(data.mElement, PR_TRUE, PR_FALSE, PR_TRUE) == 1) &&
(!checkLast ||
data.GetNthIndex(PR_TRUE, PR_TRUE, PR_TRUE) == 1);
aTreeMatchContext.mNthIndexCache.
GetNthIndex(data.mElement, PR_TRUE, PR_TRUE, PR_TRUE) == 1);
}
static inline PRBool

View File

@ -0,0 +1,159 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla.org code.
*
* The Initial Developer of the Original Code is
* Mozilla Foundation.
* Portions created by the Initial Developer are Copyright (C) 2011
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Boris Zbarsky <bzbarsky@mit.edu> (original author)
* L. David Baron <dbaron@dbaron.org>
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
/*
* A class that computes and caches the indices used for :nth-* pseudo-class
* matching.
*/
#include "nsNthIndexCache.h"
#include "nsIContent.h"
nsNthIndexCache::nsNthIndexCache()
{
}
nsNthIndexCache::~nsNthIndexCache()
{
}
void
nsNthIndexCache::Reset()
{
mCache.clear();
}
inline bool
nsNthIndexCache::SiblingMatchesElement(nsIContent* aSibling, Element* aElement,
PRBool aIsOfType)
{
return aSibling->IsElement() &&
(!aIsOfType ||
aSibling->NodeInfo()->NameAndNamespaceEquals(aElement->NodeInfo()));
}
inline bool
nsNthIndexCache::IndexDetermined(nsIContent* aSibling, Element* aChild,
PRBool aIsOfType, PRBool aIsFromEnd,
PRBool aCheckEdgeOnly, PRInt32& aResult)
{
if (SiblingMatchesElement(aSibling, aChild, aIsOfType)) {
if (aCheckEdgeOnly) {
// The caller only cares whether or not the result is 1, and we
// now know it's not.
aResult = -1;
return true;
}
Cache::Ptr siblingEntry = mCache.lookup(aSibling);
if (siblingEntry) {
PRInt32 siblingIndex = siblingEntry->value.mNthIndices[aIsOfType][aIsFromEnd];
NS_ASSERTION(siblingIndex != 0,
"How can a non-anonymous node have an anonymous sibling?");
if (siblingIndex > 0) {
aResult = siblingIndex + 1;
return true;
}
}
++aResult;
}
return false;
}
PRInt32
nsNthIndexCache::GetNthIndex(Element* aChild, PRBool aIsOfType,
PRBool aIsFromEnd, PRBool aCheckEdgeOnly)
{
NS_ASSERTION(aChild->GetParent(), "caller should check GetParent()");
if (aChild->IsRootOfAnonymousSubtree()) {
return 0;
}
if (!mCache.initialized() && !mCache.init()) {
// Give up and just don't match.
return 0;
}
Cache::AddPtr entry = mCache.lookupForAdd(aChild);
if (!entry && !mCache.add(entry, aChild)) {
// No good; don't match.
return 0;
}
PRInt32 &slot = entry->value.mNthIndices[aIsOfType][aIsFromEnd];
if (slot != -2 && (slot != -1 || aCheckEdgeOnly)) {
return slot;
}
PRInt32 result = 1;
if (aIsFromEnd) {
for (nsIContent *cur = aChild->GetNextSibling();
cur;
cur = cur->GetNextSibling()) {
// It doesn't make sense to do cache lookups for siblings when
// aIsFromEnd. In general, the cache will only be primed for
// things that are _before_ us in the DOM.
if (SiblingMatchesElement(cur, aChild, aIsOfType)) {
if (aCheckEdgeOnly) {
// The caller only cares whether or not the result is 1, and we
// now know it's not.
result = -1;
break;
}
++result;
}
}
} else {
for (nsIContent *cur = aChild->GetPreviousSibling();
cur;
cur = cur->GetPreviousSibling()) {
if (IndexDetermined(cur, aChild, aIsOfType, aIsFromEnd, aCheckEdgeOnly,
result)) {
break;
}
}
}
slot = result;
return result;
}

View File

@ -0,0 +1,122 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla.org code.
*
* The Initial Developer of the Original Code is
* Mozilla Foundation.
* Portions created by the Initial Developer are Copyright (C) 2011
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Boris Zbarsky <bzbarsky@mit.edu> (original author)
* L. David Baron <dbaron@dbaron.org>
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#ifndef nsContentIndexCache_h__
#define nsContentIndexCache_h__
#include "nscore.h"
#include "jshashtable.h"
#include "mozilla/dom/Element.h"
/*
* A class that computes and caches the indices used for :nth-* pseudo-class
* matching.
*/
class nsNthIndexCache {
private:
typedef mozilla::dom::Element Element;
public:
/**
* Constructor and destructor out of line so that we don't try to
* instantiate the hashtable template all over the place.
*/
nsNthIndexCache();
~nsNthIndexCache();
// Returns a 1-based index of the child in its parent. If the child
// is not in its parent's child list (i.e., it is anonymous content),
// returns 0.
// If aCheckEdgeOnly is true, the function will return 1 if the result
// is 1, and something other than 1 (maybe or maybe not a valid
// result) otherwise.
// This must only be called on nodes which have a non-null parent.
PRInt32 GetNthIndex(Element* aChild, PRBool aIsOfType, PRBool aIsFromEnd,
PRBool aCheckEdgeOnly);
void Reset();
private:
/**
* Returns true if aSibling and aElement should be considered in the same
* list for nth-index purposes, taking aIsOfType into account.
*/
inline bool SiblingMatchesElement(nsIContent* aSibling, Element* aElement,
PRBool aIsOfType);
/**
* Returns true if aResult has been set to the correct value for aChild and
* no more work needs to be done. Returns false otherwise.
*/
inline bool IndexDetermined(nsIContent* aSibling, Element* aChild,
PRBool aIsOfType, PRBool aIsFromEnd,
PRBool aCheckEdgeOnly, PRInt32& aResult);
struct CacheEntry {
CacheEntry() {
mNthIndices[0][0] = -2;
mNthIndices[0][1] = -2;
mNthIndices[1][0] = -2;
mNthIndices[1][1] = -2;
}
// This node's index for :nth-child(), :nth-last-child(),
// :nth-of-type(), :nth-last-of-type(). If -2, needs to be computed.
// If -1, needs to be computed but known not to be 1.
// If 0, the node is not at any index in its parent.
// The first subscript is 0 for -child and 1 for -of-type, the second
// subscript is 0 for nth- and 1 for nth-last-.
PRInt32 mNthIndices[2][2];
};
class SystemAllocPolicy {
public:
void *malloc(size_t bytes) { return ::malloc(bytes); }
void *realloc(void *p, size_t bytes) { return ::realloc(p, bytes); }
void free(void *p) { ::free(p); }
void reportAllocOverflow() const {}
};
typedef js::HashMap<nsIContent*, CacheEntry, js::DefaultHasher<nsIContent*>,
SystemAllocPolicy> Cache;
Cache mCache;
};
#endif /* nsContentIndexCache_h__ */

View File

@ -50,13 +50,13 @@
#include "nsIContent.h"
#include "nsCSSPseudoElements.h"
#include "nsRuleWalker.h"
#include "nsNthIndexCache.h"
class nsIStyleSheet;
class nsIAtom;
class nsICSSPseudoComparator;
class nsAttrValue;
/**
* A |TreeMatchContext| has data about a matching operation. The
* data are not node-specific but are invariants of the DOM tree the
@ -81,23 +81,32 @@ struct TreeMatchContext {
// nsRuleWalker::VisitedHandlingType.
nsRuleWalker::VisitedHandlingType mVisitedHandling;
// The document we're working with.
nsIDocument* const mDocument;
// Root of scoped stylesheet (set and unset by the supplier of the
// scoped stylesheet).
nsIContent* mScopedRoot;
// Whether our document is HTML (as opposed to XML of some sort,
// including XHTML).
// XXX XBL2 issue: Should we be caching this? What should it be for XBL2?
const PRPackedBool mIsHTMLDocument;
// Possibly remove use of mCompatMode in SelectorMatches?
// XXX XBL2 issue: Should we be caching this? What should it be for XBL2?
const nsCompatibility mCompatMode;
// The nth-index cache we should use
nsNthIndexCache mNthIndexCache;
TreeMatchContext(PRBool aForStyling,
nsRuleWalker::VisitedHandlingType aVisitedHandling,
nsIDocument* aDocument)
: mForStyling(aForStyling)
, mHaveRelevantLink(PR_FALSE)
, mVisitedHandling(aVisitedHandling)
, mDocument(aDocument)
, mScopedRoot(nsnull)
, mIsHTMLDocument(aDocument->IsHTML())
, mCompatMode(aDocument->GetCompatibilityMode())
@ -172,15 +181,6 @@ public:
nsRuleWalker::VisitedHandlingType aVisitedHandling,
PRBool aIsRelevantLink);
// Returns a 1-based index of the child in its parent. If the child
// is not in its parent's child list (i.e., it is anonymous content),
// returns 0.
// If aCheckEdgeOnly is true, the function will return 1 if the result
// is 1, and something other than 1 (maybe or maybe not a valid
// result) otherwise.
PRInt32 GetNthIndex(PRBool aIsOfType, PRBool aIsFromEnd,
PRBool aCheckEdgeOnly);
nsPresContext* mPresContext;
mozilla::dom::Element* mElement; // weak ref, must not be null
nsIContent* mParentContent; // mElement->GetParent(); weak ref
@ -200,14 +200,6 @@ public:
private:
nsString *mLanguage; // NULL means we haven't found out the language yet
// This node's index for :nth-child(), :nth-last-child(),
// :nth-of-type(), :nth-last-of-type(). If -2, needs to be computed.
// If -1, needs to be computed but known not to be 1.
// If 0, the node is not at any index in its parent.
// The first subscript is 0 for -child and 1 for -of-type, the second
// subscript is 0 for nth- and 1 for nth-last-.
PRInt32 mNthIndices[2][2];
// mContentState is initialized lazily.
nsEventStates mContentState; // eventStateMgr->GetContentState() or
// mElement->IntrinsicState() if we have no ESM