Skip to content

Commit e427b3d

Browse files
committed
Extract named items caches in HTMLCollection as a class
https://bugs.webkit.org/show_bug.cgi?id=129365 Reviewed by Antti Koivisto. Extracted CollectionNamedElementCache, like CollectionIndexCache, out of HTMLCollection. We can move more named item related functions into this class in the future. HTMLCollection's member variables m_isNameCacheValid, m_idCache, and m_nameCache were replaced by a single unique_ptr that holds an instance of CollectionNamedElementCache since this object rarely exists in most HTMLCollections. Also removed m_isItemRefElementsCacheValid since it was only used by Microdata API removed in r153772 and renamed a whole bunch of member functions and variables for consistency. * dom/Document.cpp: (WebCore::Document::collectionCachedIdNameMap): (WebCore::Document::collectionWillClearIdNameMap): * dom/Document.h: * dom/NodeRareData.h: (WebCore::NodeListsNodeData::adoptDocument): * html/HTMLAllCollection.cpp: (WebCore::HTMLAllCollection::namedItemWithIndex): * html/HTMLCollection.cpp: (WebCore::HTMLCollection::HTMLCollection): (WebCore::HTMLCollection::~HTMLCollection): (WebCore::HTMLCollection::invalidateCache): (WebCore::HTMLCollection::invalidateNamedElementCache): Renamed from invalidateIdNameCacheMaps. (WebCore::HTMLCollection::namedItem): (WebCore::HTMLCollection::updateNamedElementCache): Renamed from updateNameCache. (WebCore::HTMLCollection::namedItems): * html/HTMLCollection.h: (WebCore::CollectionNamedElementCache::findElementsWithId): Renamed from HTMLCollection::idCache. (WebCore::CollectionNamedElementCache::findElementsWithName): Renamed from HTMLCollection::nameCache. (WebCore::CollectionNamedElementCache::appendIdCache): Moved from HTMLCollection. (WebCore::CollectionNamedElementCache::appendNameCache): Ditto. (WebCore::CollectionNamedElementCache::find): Ditto. (WebCore::CollectionNamedElementCache::append): Ditto. (WebCore::HTMLCollection::invalidateCache): (WebCore::HTMLCollection::hasNamedElementCache): Renamed from hasIdNameCache. (WebCore::HTMLCollection::createNameItemCache): Added. (WebCore::HTMLCollection::namedItemCaches): Added. * html/HTMLFormControlsCollection.cpp: (WebCore::HTMLFormControlsCollection::updateNamedElementCache): * html/HTMLFormControlsCollection.h: Canonical link: https://commits.webkit.org/147466@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@164772 268f45cc-cd09-0410-ab3c-d52691b4dbfc
1 parent 9f45161 commit e427b3d

9 files changed

Lines changed: 180 additions & 85 deletions

Source/WebCore/ChangeLog

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,92 @@
1+
2014-02-26 Ryosuke Niwa <rniwa@webkit.org>
2+
3+
Extract named items caches in HTMLCollection as a class
4+
https://bugs.webkit.org/show_bug.cgi?id=129365
5+
6+
Reviewed by Antti Koivisto.
7+
8+
Extracted CollectionNamedElementCache, like CollectionIndexCache, out of HTMLCollection.
9+
We can move more named item related functions into this class in the future.
10+
11+
HTMLCollection's member variables m_isNameCacheValid, m_idCache, and m_nameCache were replaced by
12+
a single unique_ptr that holds an instance of CollectionNamedElementCache since this object rarely
13+
exists in most HTMLCollections.
14+
15+
Also removed m_isItemRefElementsCacheValid since it was only used by Microdata API removed in r153772
16+
and renamed a whole bunch of member functions and variables for consistency.
17+
18+
* dom/Document.cpp:
19+
(WebCore::Document::collectionCachedIdNameMap):
20+
(WebCore::Document::collectionWillClearIdNameMap):
21+
* dom/Document.h:
22+
* dom/NodeRareData.h:
23+
(WebCore::NodeListsNodeData::adoptDocument):
24+
* html/HTMLAllCollection.cpp:
25+
(WebCore::HTMLAllCollection::namedItemWithIndex):
26+
* html/HTMLCollection.cpp:
27+
(WebCore::HTMLCollection::HTMLCollection):
28+
(WebCore::HTMLCollection::~HTMLCollection):
29+
(WebCore::HTMLCollection::invalidateCache):
30+
(WebCore::HTMLCollection::invalidateNamedElementCache): Renamed from invalidateIdNameCacheMaps.
31+
(WebCore::HTMLCollection::namedItem):
32+
(WebCore::HTMLCollection::updateNamedElementCache): Renamed from updateNameCache.
33+
(WebCore::HTMLCollection::namedItems):
34+
35+
* html/HTMLCollection.h:
36+
(WebCore::CollectionNamedElementCache::findElementsWithId): Renamed from HTMLCollection::idCache.
37+
(WebCore::CollectionNamedElementCache::findElementsWithName): Renamed from HTMLCollection::nameCache.
38+
(WebCore::CollectionNamedElementCache::appendIdCache): Moved from HTMLCollection.
39+
(WebCore::CollectionNamedElementCache::appendNameCache): Ditto.
40+
(WebCore::CollectionNamedElementCache::find): Ditto.
41+
(WebCore::CollectionNamedElementCache::append): Ditto.
42+
43+
(WebCore::HTMLCollection::invalidateCache):
44+
(WebCore::HTMLCollection::hasNamedElementCache): Renamed from hasIdNameCache.
45+
(WebCore::HTMLCollection::createNameItemCache): Added.
46+
(WebCore::HTMLCollection::namedItemCaches): Added.
47+
48+
* html/HTMLFormControlsCollection.cpp:
49+
(WebCore::HTMLFormControlsCollection::updateNamedElementCache):
50+
* html/HTMLFormControlsCollection.h:
51+
52+
2014-02-26 Ryosuke Niwa <rniwa@webkit.org>
53+
54+
Extract named items caches in HTMLCollection as a class
55+
https://bugs.webkit.org/show_bug.cgi?id=129365
56+
57+
Reviewed by Antti Koivisto.
58+
59+
Extracted CollectionNamedItemCaches, like CollectionIndexCache, out of HTMLCollection.
60+
We can move more named item related functions into this class in the future.
61+
62+
HTMLCollection's member variables m_isNameCacheValid, m_idCache, and m_nameCache were replaced by
63+
a single unique_ptr that holds an instance of CollectionNamedItemCaches since this object rarely
64+
exists in most HTMLCollections.
65+
66+
Also removed m_isItemRefElementsCacheValid since it was only used by Microdata API removed in r153772.
67+
68+
* html/HTMLAllCollection.cpp:
69+
(WebCore::HTMLAllCollection::namedItemWithIndex):
70+
* html/HTMLCollection.cpp:
71+
(WebCore::HTMLCollection::HTMLCollection):
72+
(WebCore::HTMLCollection::invalidateCache):
73+
(WebCore::HTMLCollection::invalidateIdNameCacheMaps):
74+
(WebCore::HTMLCollection::namedItem):
75+
(WebCore::HTMLCollection::updateNameCache):
76+
(WebCore::HTMLCollection::namedItems):
77+
* html/HTMLCollection.h:
78+
(WebCore::CollectionNamedItemCaches::idCache):
79+
(WebCore::CollectionNamedItemCaches::nameCache):
80+
(WebCore::CollectionNamedItemCaches::appendIdCache):
81+
(WebCore::CollectionNamedItemCaches::appendNameCache):
82+
(WebCore::CollectionNamedItemCaches::find):
83+
(WebCore::CollectionNamedItemCaches::append):
84+
(WebCore::HTMLCollection::hasIdNameCache):
85+
(WebCore::HTMLCollection::createNameItemCaches):
86+
(WebCore::HTMLCollection::namedItemCachesAssertingExistence):
87+
* html/HTMLFormControlsCollection.cpp:
88+
(WebCore::HTMLFormControlsCollection::updateNameCache):
89+
190
2014-02-26 Ryosuke Niwa <rniwa@webkit.org>
291

392
Indenting an indented image element resulted in an extra indentation

Source/WebCore/dom/Document.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3479,13 +3479,13 @@ void Document::unregisterCollection(HTMLCollection& collection, bool hasIdNameMa
34793479

34803480
void Document::collectionCachedIdNameMap(const HTMLCollection& collection)
34813481
{
3482-
ASSERT_UNUSED(collection, collection.hasIdNameCache());
3482+
ASSERT_UNUSED(collection, collection.hasNamedElementCache());
34833483
m_nodeListAndCollectionCounts[InvalidateOnIdNameAttrChange]++;
34843484
}
34853485

34863486
void Document::collectionWillClearIdNameMap(const HTMLCollection& collection)
34873487
{
3488-
ASSERT_UNUSED(collection, collection.hasIdNameCache());
3488+
ASSERT_UNUSED(collection, collection.hasNamedElementCache());
34893489
ASSERT(m_nodeListAndCollectionCounts[InvalidateOnIdNameAttrChange]);
34903490
m_nodeListAndCollectionCounts[InvalidateOnIdNameAttrChange]--;
34913491
}

Source/WebCore/dom/Document.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -761,8 +761,8 @@ class Document : public ContainerNode, public TreeScope, public ScriptExecutionC
761761

762762
void registerNodeList(LiveNodeList&);
763763
void unregisterNodeList(LiveNodeList&);
764-
void registerCollection(HTMLCollection&, bool hasIdNameCache);
765-
void unregisterCollection(HTMLCollection&, bool hasIdNameCache);
764+
void registerCollection(HTMLCollection&, bool hasNamedElementCache);
765+
void unregisterCollection(HTMLCollection&, bool hasNamedElementCache);
766766
void collectionCachedIdNameMap(const HTMLCollection&);
767767
void collectionWillClearIdNameMap(const HTMLCollection&);
768768
bool shouldInvalidateNodeListAndCollectionCaches(const QualifiedName* attrName = nullptr) const;

Source/WebCore/dom/NodeRareData.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,8 @@ class NodeListsNodeData {
254254

255255
for (auto it : m_cachedCollections) {
256256
HTMLCollection& collection = *it.value;
257-
oldDocument->unregisterCollection(collection, collection.hasIdNameCache());
258-
newDocument->registerCollection(collection, collection.hasIdNameCache());
257+
oldDocument->unregisterCollection(collection, collection.hasNamedElementCache());
258+
newDocument->registerCollection(collection, collection.hasNamedElementCache());
259259
collection.invalidateCache();
260260
}
261261
}

Source/WebCore/html/HTMLAllCollection.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,18 @@ HTMLAllCollection::~HTMLAllCollection()
4646

4747
Node* HTMLAllCollection::namedItemWithIndex(const AtomicString& name, unsigned index) const
4848
{
49-
updateNameCache();
49+
updateNamedElementCache();
50+
const CollectionNamedElementCache& cache = namedItemCaches();
5051

51-
if (Vector<Element*>* cache = idCache(name)) {
52-
if (index < cache->size())
53-
return cache->at(index);
54-
index -= cache->size();
52+
if (const Vector<Element*>* elements = cache.findElementsWithId(name)) {
53+
if (index < elements->size())
54+
return elements->at(index);
55+
index -= elements->size();
5556
}
5657

57-
if (Vector<Element*>* cache = nameCache(name)) {
58-
if (index < cache->size())
59-
return cache->at(index);
58+
if (const Vector<Element*>* elements = cache.findElementsWithName(name)) {
59+
if (index < elements->size())
60+
return elements->at(index);
6061
}
6162

6263
return 0;

Source/WebCore/html/HTMLCollection.cpp

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -133,19 +133,17 @@ static NodeListInvalidationType invalidationTypeExcludingIdAndNameAttributes(Col
133133

134134
HTMLCollection::HTMLCollection(ContainerNode& ownerNode, CollectionType type, ElementTraversalType traversalType)
135135
: m_ownerNode(ownerNode)
136-
, m_rootType(rootTypeFromCollectionType(type))
136+
, m_collectionType(type)
137137
, m_invalidationType(invalidationTypeExcludingIdAndNameAttributes(type))
138+
, m_rootType(rootTypeFromCollectionType(type))
138139
, m_shouldOnlyIncludeDirectChildren(shouldOnlyIncludeDirectChildren(type))
139-
, m_isNameCacheValid(false)
140-
, m_collectionType(type)
141140
, m_usesCustomForwardOnlyTraversal(traversalType == CustomForwardOnlyTraversal)
142-
, m_isItemRefElementsCacheValid(false)
143141
{
144142
ASSERT(m_rootType == static_cast<unsigned>(rootTypeFromCollectionType(type)));
145143
ASSERT(m_invalidationType == static_cast<unsigned>(invalidationTypeExcludingIdAndNameAttributes(type)));
146144
ASSERT(m_collectionType == static_cast<unsigned>(type));
147145

148-
document().registerCollection(*this, hasIdNameCache());
146+
document().registerCollection(*this, hasNamedElementCache());
149147
}
150148

151149
PassRefPtr<HTMLCollection> HTMLCollection::create(ContainerNode& base, CollectionType type)
@@ -155,7 +153,7 @@ PassRefPtr<HTMLCollection> HTMLCollection::create(ContainerNode& base, Collectio
155153

156154
HTMLCollection::~HTMLCollection()
157155
{
158-
document().unregisterCollection(*this, hasIdNameCache());
156+
document().unregisterCollection(*this, hasNamedElementCache());
159157
// HTMLNameCollection removes cache by itself.
160158
if (type() != WindowNamedItems && type() != DocumentNamedItems)
161159
ownerNode().nodeLists()->removeCachedCollection(this);
@@ -367,18 +365,15 @@ Element* HTMLCollection::collectionTraverseBackward(Element& current, unsigned c
367365
void HTMLCollection::invalidateCache() const
368366
{
369367
m_indexCache.invalidate();
370-
m_isItemRefElementsCacheValid = false;
371-
if (hasIdNameCache())
372-
invalidateIdNameCacheMaps();
368+
if (hasNamedElementCache())
369+
invalidateNamedElementCache();
373370
}
374371

375-
void HTMLCollection::invalidateIdNameCacheMaps() const
372+
void HTMLCollection::invalidateNamedElementCache() const
376373
{
377-
ASSERT(hasIdNameCache());
374+
ASSERT(hasNamedElementCache());
378375
document().collectionWillClearIdNameMap(*this);
379-
m_isNameCacheValid = false;
380-
m_idCache.clear();
381-
m_nameCache.clear();
376+
m_namedElementCache = nullptr;
382377
}
383378

384379
Node* HTMLCollection::namedItem(const AtomicString& name) const
@@ -414,41 +409,41 @@ Node* HTMLCollection::namedItem(const AtomicString& name) const
414409
}
415410

416411
// The pathological case. We need to walk the entire subtree.
417-
updateNameCache();
412+
updateNamedElementCache();
413+
ASSERT(m_namedElementCache);
418414

419-
if (Vector<Element*>* idResults = idCache(name)) {
415+
if (const Vector<Element*>* idResults = m_namedElementCache->findElementsWithId(name)) {
420416
if (idResults->size())
421417
return idResults->at(0);
422418
}
423419

424-
if (Vector<Element*>* nameResults = nameCache(name)) {
420+
if (const Vector<Element*>* nameResults = m_namedElementCache->findElementsWithName(name)) {
425421
if (nameResults->size())
426422
return nameResults->at(0);
427423
}
428424

429425
return 0;
430426
}
431427

432-
void HTMLCollection::updateNameCache() const
428+
void HTMLCollection::updateNamedElementCache() const
433429
{
434-
if (hasIdNameCache())
430+
if (hasNamedElementCache())
435431
return;
436432

437433
ContainerNode& root = rootNode();
434+
CollectionNamedElementCache& cache = createNameItemCache();
438435

439436
unsigned count;
440437
for (Element* element = firstElement(root); element; element = traverseForward(*element, 1, count, root)) {
441438
const AtomicString& idAttrVal = element->getIdAttribute();
442439
if (!idAttrVal.isEmpty())
443-
appendIdCache(idAttrVal, element);
440+
cache.appendIdCache(idAttrVal, element);
444441
if (!element->isHTMLElement())
445442
continue;
446443
const AtomicString& nameAttrVal = element->getNameAttribute();
447444
if (!nameAttrVal.isEmpty() && idAttrVal != nameAttrVal && (type() != DocAll || nameShouldBeVisibleInDocumentAll(toHTMLElement(*element))))
448-
appendNameCache(nameAttrVal, element);
445+
cache.appendNameCache(nameAttrVal, element);
449446
}
450-
451-
setHasIdNameCache();
452447
}
453448

454449
bool HTMLCollection::hasNamedItem(const AtomicString& name) const
@@ -463,10 +458,11 @@ void HTMLCollection::namedItems(const AtomicString& name, Vector<Ref<Element>>&
463458
if (name.isEmpty())
464459
return;
465460

466-
updateNameCache();
461+
updateNamedElementCache();
462+
ASSERT(m_namedElementCache);
467463

468-
Vector<Element*>* idResults = idCache(name);
469-
Vector<Element*>* nameResults = nameCache(name);
464+
const Vector<Element*>* idResults = m_namedElementCache->findElementsWithId(name);
465+
const Vector<Element*>* nameResults = m_namedElementCache->findElementsWithName(name);
470466

471467
for (unsigned i = 0; idResults && i < idResults->size(); ++i)
472468
result.append(*idResults->at(i));
@@ -480,12 +476,4 @@ PassRefPtr<NodeList> HTMLCollection::tags(const String& name)
480476
return ownerNode().getElementsByTagName(name);
481477
}
482478

483-
void HTMLCollection::append(NodeCacheMap& map, const AtomicString& key, Element* element)
484-
{
485-
OwnPtr<Vector<Element*>>& vector = map.add(key.impl(), nullptr).iterator->value;
486-
if (!vector)
487-
vector = adoptPtr(new Vector<Element*>);
488-
vector->append(element);
489-
}
490-
491479
} // namespace WebCore

0 commit comments

Comments
 (0)