File: NthIndexCache.cpp

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (265 lines) | stat: -rw-r--r-- 8,810 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "core/dom/NthIndexCache.h"

#include "core/dom/Document.h"
#include "core/dom/ElementTraversal.h"

namespace blink {

NthIndexCache::NthIndexCache(Document& document)
    : m_document(&document)
#if DCHECK_IS_ON()
      ,
      m_domTreeVersion(document.domTreeVersion())
#endif
{
  document.setNthIndexCache(this);
}

NthIndexCache::~NthIndexCache() {
#if DCHECK_IS_ON()
  DCHECK_EQ(m_domTreeVersion, m_document->domTreeVersion());
#endif
  m_document->setNthIndexCache(nullptr);
}

namespace {

// Generating the cached nth-index counts when the number of children
// exceeds this count. This number is picked based on testing
// querySelectorAll for :nth-child(3n+2) and :nth-of-type(3n+2) on an
// increasing number of children.

const unsigned kCachedSiblingCountLimit = 32;

unsigned uncachedNthChildIndex(Element& element) {
  int index = 1;
  for (const Element* sibling = ElementTraversal::previousSibling(element);
       sibling; sibling = ElementTraversal::previousSibling(*sibling))
    index++;

  return index;
}

unsigned uncachedNthLastChildIndex(Element& element) {
  int index = 1;
  for (const Element* sibling = ElementTraversal::nextSibling(element); sibling;
       sibling = ElementTraversal::nextSibling(*sibling))
    ++index;
  return index;
}

unsigned uncachedNthOfTypeIndex(Element& element, unsigned& siblingCount) {
  int index = 1;
  const QualifiedName& tag = element.tagQName();
  for (const Element* sibling = ElementTraversal::previousSibling(element);
       sibling; sibling = ElementTraversal::previousSibling(*sibling)) {
    if (sibling->tagQName() == tag)
      ++index;
    ++siblingCount;
  }
  return index;
}

unsigned uncachedNthLastOfTypeIndex(Element& element, unsigned& siblingCount) {
  int index = 1;
  const QualifiedName& tag = element.tagQName();
  for (const Element* sibling = ElementTraversal::nextSibling(element); sibling;
       sibling = ElementTraversal::nextSibling(*sibling)) {
    if (sibling->tagQName() == tag)
      ++index;
    ++siblingCount;
  }
  return index;
}

}  // namespace

unsigned NthIndexCache::nthChildIndex(Element& element) {
  if (element.isPseudoElement())
    return 1;
  DCHECK(element.parentNode());
  NthIndexCache* nthIndexCache = element.document().nthIndexCache();
  NthIndexData* nthIndexData = nullptr;
  if (nthIndexCache && nthIndexCache->m_parentMap)
    nthIndexData = nthIndexCache->m_parentMap->get(element.parentNode());
  if (nthIndexData)
    return nthIndexData->nthIndex(element);
  unsigned index = uncachedNthChildIndex(element);
  if (nthIndexCache && index > kCachedSiblingCountLimit)
    nthIndexCache->cacheNthIndexDataForParent(element);
  return index;
}

unsigned NthIndexCache::nthLastChildIndex(Element& element) {
  if (element.isPseudoElement())
    return 1;
  DCHECK(element.parentNode());
  NthIndexCache* nthIndexCache = element.document().nthIndexCache();
  NthIndexData* nthIndexData = nullptr;
  if (nthIndexCache && nthIndexCache->m_parentMap)
    nthIndexData = nthIndexCache->m_parentMap->get(element.parentNode());
  if (nthIndexData)
    return nthIndexData->nthLastIndex(element);
  unsigned index = uncachedNthLastChildIndex(element);
  if (nthIndexCache && index > kCachedSiblingCountLimit)
    nthIndexCache->cacheNthIndexDataForParent(element);
  return index;
}

NthIndexData* NthIndexCache::nthTypeIndexDataForParent(Element& element) const {
  DCHECK(element.parentNode());
  if (!m_parentMapForType)
    return nullptr;
  if (const IndexByType* map = m_parentMapForType->get(element.parentNode()))
    return map->get(element.tagName());
  return nullptr;
}

unsigned NthIndexCache::nthOfTypeIndex(Element& element) {
  if (element.isPseudoElement())
    return 1;
  NthIndexCache* nthIndexCache = element.document().nthIndexCache();
  if (nthIndexCache) {
    if (NthIndexData* nthIndexData =
            nthIndexCache->nthTypeIndexDataForParent(element))
      return nthIndexData->nthOfTypeIndex(element);
  }
  unsigned siblingCount = 0;
  unsigned index = uncachedNthOfTypeIndex(element, siblingCount);
  if (nthIndexCache && siblingCount > kCachedSiblingCountLimit)
    nthIndexCache->cacheNthOfTypeIndexDataForParent(element);
  return index;
}

unsigned NthIndexCache::nthLastOfTypeIndex(Element& element) {
  if (element.isPseudoElement())
    return 1;
  NthIndexCache* nthIndexCache = element.document().nthIndexCache();
  if (nthIndexCache) {
    if (NthIndexData* nthIndexData =
            nthIndexCache->nthTypeIndexDataForParent(element))
      return nthIndexData->nthLastOfTypeIndex(element);
  }
  unsigned siblingCount = 0;
  unsigned index = uncachedNthLastOfTypeIndex(element, siblingCount);
  if (nthIndexCache && siblingCount > kCachedSiblingCountLimit)
    nthIndexCache->cacheNthOfTypeIndexDataForParent(element);
  return index;
}

void NthIndexCache::cacheNthIndexDataForParent(Element& element) {
  DCHECK(element.parentNode());
  if (!m_parentMap)
    m_parentMap = new ParentMap();

  ParentMap::AddResult addResult =
      m_parentMap->add(element.parentNode(), nullptr);
  DCHECK(addResult.isNewEntry);
  addResult.storedValue->value = new NthIndexData(*element.parentNode());
}

NthIndexCache::IndexByType& NthIndexCache::ensureTypeIndexMap(
    ContainerNode& parent) {
  if (!m_parentMapForType)
    m_parentMapForType = new ParentMapForType();

  ParentMapForType::AddResult addResult =
      m_parentMapForType->add(&parent, nullptr);
  if (addResult.isNewEntry)
    addResult.storedValue->value = new IndexByType();

  DCHECK(addResult.storedValue->value);
  return *addResult.storedValue->value;
}

void NthIndexCache::cacheNthOfTypeIndexDataForParent(Element& element) {
  DCHECK(element.parentNode());
  IndexByType::AddResult addResult =
      ensureTypeIndexMap(*element.parentNode()).add(element.tagName(), nullptr);
  DCHECK(addResult.isNewEntry);
  addResult.storedValue->value =
      new NthIndexData(*element.parentNode(), element.tagQName());
}

unsigned NthIndexData::nthIndex(Element& element) const {
  DCHECK(!element.isPseudoElement());

  unsigned index = 0;
  for (Element *sibling = &element; sibling;
       sibling = ElementTraversal::previousSibling(*sibling), index++) {
    auto it = m_elementIndexMap.find(sibling);
    if (it != m_elementIndexMap.end())
      return it->value + index;
  }
  return index;
}

unsigned NthIndexData::nthOfTypeIndex(Element& element) const {
  DCHECK(!element.isPseudoElement());

  unsigned index = 0;
  for (Element *sibling = &element; sibling;
       sibling = ElementTraversal::previousSibling(
           *sibling, HasTagName(element.tagQName())),
               index++) {
    auto it = m_elementIndexMap.find(sibling);
    if (it != m_elementIndexMap.end())
      return it->value + index;
  }
  return index;
}

unsigned NthIndexData::nthLastIndex(Element& element) const {
  return m_count - nthIndex(element) + 1;
}

unsigned NthIndexData::nthLastOfTypeIndex(Element& element) const {
  return m_count - nthOfTypeIndex(element) + 1;
}

NthIndexData::NthIndexData(ContainerNode& parent) {
  // The frequency at which we cache the nth-index for a set of siblings.  A
  // spread value of 3 means every third Element will have its nth-index cached.
  // Using a spread value > 1 is done to save memory. Looking up the nth-index
  // will still be done in constant time in terms of sibling count, at most
  // 'spread' elements will be traversed.
  const unsigned spread = 3;
  unsigned count = 0;
  for (Element* sibling = ElementTraversal::firstChild(parent); sibling;
       sibling = ElementTraversal::nextSibling(*sibling)) {
    if (!(++count % spread))
      m_elementIndexMap.add(sibling, count);
  }
  DCHECK(count);
  m_count = count;
}

NthIndexData::NthIndexData(ContainerNode& parent, const QualifiedName& type) {
  // The frequency at which we cache the nth-index of type for a set of
  // siblings.  A spread value of 3 means every third Element of its type will
  // have its nth-index cached.  Using a spread value > 1 is done to save
  // memory. Looking up the nth-index of its type will still be done in less
  // time, as most number of elements traversed will be equal to find 'spread'
  // elements in the sibling set.
  const unsigned spread = 3;
  unsigned count = 0;
  for (Element* sibling =
           ElementTraversal::firstChild(parent, HasTagName(type));
       sibling;
       sibling = ElementTraversal::nextSibling(*sibling, HasTagName(type))) {
    if (!(++count % spread))
      m_elementIndexMap.add(sibling, count);
  }
  DCHECK(count);
  m_count = count;
}

DEFINE_TRACE(NthIndexData) {
  visitor->trace(m_elementIndexMap);
}

}  // namespace blink