| Index: Source/core/dom/Document.cpp | 
| diff --git a/Source/core/dom/Document.cpp b/Source/core/dom/Document.cpp | 
| index 0e83a6fc8b4c3f1f9d4761ba9695a15ff178e575..2ff74af468c220194b9f562a67df8bbc212f0ea9 100644 | 
| --- a/Source/core/dom/Document.cpp | 
| +++ b/Source/core/dom/Document.cpp | 
| @@ -86,6 +86,7 @@ | 
| #include "core/dom/NodeRenderingTraversal.h" | 
| #include "core/dom/NodeTraversal.h" | 
| #include "core/dom/NodeWithIndex.h" | 
| +#include "core/dom/NthIndexCache.h" | 
| #include "core/dom/ProcessingInstruction.h" | 
| #include "core/dom/RequestAnimationFrameCallback.h" | 
| #include "core/dom/ScriptRunner.h" | 
| @@ -474,6 +475,7 @@ Document::Document(const DocumentInit& initializer, DocumentClassFlags documentC | 
| , m_hasViewportUnits(false) | 
| , m_styleRecalcElementCounter(0) | 
| , m_parserSyncPolicy(AllowAsynchronousParsing) | 
| +    , m_useNthIndexCache(false) | 
| { | 
| if (m_frame) { | 
| ASSERT(m_frame->page()); | 
| @@ -656,6 +658,22 @@ SelectorQueryCache& Document::selectorQueryCache() | 
| return *m_selectorQueryCache; | 
| } | 
|  | 
| +NthIndexCache* Document::nthIndexCache() | 
| +{ | 
| +    if (!m_useNthIndexCache) | 
| +        return nullptr; | 
| +    if (!m_nthIndexCache) | 
| +        m_nthIndexCache = NthIndexCache::create(*this); | 
| +    return m_nthIndexCache.get(); | 
| +} | 
| + | 
| +void Document::setUseNthIndexCache(bool use) | 
| +{ | 
| +    m_useNthIndexCache = use; | 
| +    if (!use && m_nthIndexCache) | 
| +        m_nthIndexCache->clear(); | 
| +} | 
| + | 
| MediaQueryMatcher& Document::mediaQueryMatcher() | 
| { | 
| if (!m_mediaQueryMatcher) | 
| @@ -1713,6 +1731,8 @@ void Document::updateRenderTree(StyleRecalcChange change) | 
| if (inStyleRecalc()) | 
| return; | 
|  | 
| +    UseNthIndexCacheScope nthIndexScope(*this); | 
| + | 
| // Entering here from inside layout or paint would be catastrophic since recalcStyle can | 
| // tear down the render tree or (unfortunately) run script. Kill the whole renderer if | 
| // someone managed to get into here from inside layout or paint. | 
| @@ -5714,6 +5734,7 @@ DEFINE_TRACE(Document) | 
| visitor->trace(m_visibilityObservers); | 
| visitor->trace(m_userActionElements); | 
| visitor->trace(m_svgExtensions); | 
| +    visitor->trace(m_nthIndexCache); | 
| visitor->trace(m_timeline); | 
| visitor->trace(m_compositorPendingAnimations); | 
| visitor->trace(m_contextDocument); | 
|  |