Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(97)

Side by Side Diff: Source/core/dom/NthIndexCache.cpp

Issue 1023393002: Cache element indices for :nth-child and :nth-last-child selectors. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Clear cache after each operation Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "config.h"
6 #include "core/dom/NthIndexCache.h"
7
8 #include "core/dom/Document.h"
9
10 namespace blink {
11
12 void NthIndexCache::clear()
13 {
14 if (m_parentMap)
15 m_parentMap->clear();
16 }
17
18 NthIndexCache::NthIndexData& NthIndexCache::ensureNthIndexDataFor(Node& parent)
19 {
20 if (!m_parentMap)
21 m_parentMap = adoptPtrWillBeNoop(new ParentMap());
22
23 ParentMap::AddResult addResult = m_parentMap->add(&parent, nullptr);
24 if (addResult.isNewEntry)
25 addResult.storedValue->value = adoptPtrWillBeNoop(new NthIndexData());
26
27 ASSERT(addResult.storedValue->value);
28 return *addResult.storedValue->value;
29 }
30
31 unsigned NthIndexCache::NthIndexData::cacheNthIndices(Element& element)
32 {
33 unsigned index = 0;
34 const unsigned spread = 3;
35 unsigned count = 0;
36 for (Element* sibling = ElementTraversal::firstChild(*element.parentNode()); sibling; sibling = ElementTraversal::nextSibling(*sibling)) {
37 if (!(++count % spread))
38 m_elementIndexMap.add(sibling, count);
39 if (sibling == &element)
40 index = count;
41 }
42 ASSERT(count && index);
43 m_count = count;
44 return index;
45 }
46
47 DEFINE_TRACE(NthIndexCache)
48 {
49 #if ENABLE(OILPAN)
50 visitor->trace(m_parentMap);
51 #endif
52 }
53
54 DEFINE_TRACE(NthIndexCache::NthIndexData)
55 {
56 #if ENABLE(OILPAN)
57 visitor->trace(m_elementIndexMap);
58 #endif
59 }
60
61 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698