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

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

Issue 92083002: Add fast path for tag#id selector queries (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Fix clusterfuzz crash and remove FIXME comment Created 7 years 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
1 /* 1 /*
2 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserv ed. 2 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserv ed.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
58 } 58 }
59 59
60 inline bool keyMatchesLabelForAttribute(StringImpl* key, Element* element) 60 inline bool keyMatchesLabelForAttribute(StringImpl* key, Element* element)
61 { 61 {
62 return isHTMLLabelElement(element) && element->getAttribute(forAttr).impl() == key; 62 return isHTMLLabelElement(element) && element->getAttribute(forAttr).impl() == key;
63 } 63 }
64 64
65 void DocumentOrderedMap::clear() 65 void DocumentOrderedMap::clear()
66 { 66 {
67 m_map.clear(); 67 m_map.clear();
68 m_duplicateCounts.clear();
69 } 68 }
70 69
71 void DocumentOrderedMap::add(StringImpl* key, Element* element) 70 void DocumentOrderedMap::add(StringImpl* key, Element* element)
72 { 71 {
73 ASSERT(key); 72 ASSERT(key);
74 ASSERT(element); 73 ASSERT(element);
75 74
76 if (!m_duplicateCounts.contains(key)) { 75 Map::AddResult addResult = m_map.add(key, MapEntry(element));
77 // Fast path. The key is not already in m_duplicateCounts, so we assume that it's 76 if (addResult.isNewEntry)
78 // also not already in m_map and try to add it. If that add succeeds, we 're done. 77 return;
79 Map::AddResult addResult = m_map.add(key, element);
80 if (addResult.isNewEntry)
81 return;
82 78
83 // The add failed, so this key was already cached in m_map. 79 MapEntry& entry = addResult.iterator->value;
84 // There are multiple elements with this key. Remove the m_map 80 ASSERT(entry.count);
85 // cache for this key so get searches for it next time it is called. 81 entry.element = 0;
86 m_map.remove(addResult.iterator); 82 entry.count++;
87 m_duplicateCounts.add(key); 83 entry.orderedList.clear();
88 } else {
89 // There are multiple elements with this key. Remove the m_map
90 // cache for this key so get will search for it next time it is called.
91 Map::iterator cachedItem = m_map.find(key);
92 if (cachedItem != m_map.end()) {
93 m_map.remove(cachedItem);
94 m_duplicateCounts.add(key);
95 }
96 }
97
98 m_duplicateCounts.add(key);
99 } 84 }
100 85
101 void DocumentOrderedMap::remove(StringImpl* key, Element* element) 86 void DocumentOrderedMap::remove(StringImpl* key, Element* element)
102 { 87 {
103 ASSERT(key); 88 ASSERT(key);
104 ASSERT(element); 89 ASSERT(element);
105 90
106 Map::iterator cachedItem = m_map.find(key); 91 Map::iterator it = m_map.find(key);
107 if (cachedItem != m_map.end() && cachedItem->value == element) 92 if (it == m_map.end())
108 m_map.remove(cachedItem); 93 return;
109 else 94
110 m_duplicateCounts.remove(key); 95 MapEntry& entry = it->value;
96
97 ASSERT(entry.count);
98 if (entry.count == 1) {
99 ASSERT(!entry.element || entry.element == element);
100 m_map.remove(it);
101 } else {
102 if (entry.element == element) {
103 ASSERT(entry.orderedList.isEmpty() || entry.orderedList.first() == e lement);
104 entry.element = entry.orderedList.size() > 1 ? entry.orderedList[1] : 0;
105 }
106 entry.count--;
107 entry.orderedList.clear();
108 }
111 } 109 }
112 110
113 template<bool keyMatches(StringImpl*, Element*)> 111 template<bool keyMatches(StringImpl*, Element*)>
114 inline Element* DocumentOrderedMap::get(StringImpl* key, const TreeScope* scope) const 112 inline Element* DocumentOrderedMap::get(StringImpl* key, const TreeScope* scope) const
115 { 113 {
116 ASSERT(key); 114 ASSERT(key);
117 ASSERT(scope); 115 ASSERT(scope);
118 116
119 Element* element = m_map.get(key); 117 Map::iterator it = m_map.find(key);
120 if (element) 118 if (it == m_map.end())
119 return 0;
120
121 MapEntry& entry = it->value;
122 ASSERT(entry.count);
123 if (entry.element)
124 return entry.element;
125
126 // We know there's at least one node that matches; iterate to find the first one.
127 for (Element* element = ElementTraversal::firstWithin(*scope->rootNode()); e lement; element = ElementTraversal::next(*element)) {
128 if (!keyMatches(key, element))
129 continue;
130 entry.element = element;
121 return element; 131 return element;
122
123 if (m_duplicateCounts.contains(key)) {
124 // We know there's at least one node that matches; iterate to find the f irst one.
125 ASSERT(scope->rootNode());
126 for (element = ElementTraversal::firstWithin(*scope->rootNode()); elemen t; element = ElementTraversal::next(*element)) {
127 if (!keyMatches(key, element))
128 continue;
129 m_duplicateCounts.remove(key);
130 m_map.set(key, element);
131 return element;
132 }
133 ASSERT_NOT_REACHED();
134 } 132 }
135 133 ASSERT_NOT_REACHED();
136 return 0; 134 return 0;
137 } 135 }
138 136
139 Element* DocumentOrderedMap::getElementById(StringImpl* key, const TreeScope* sc ope) const 137 Element* DocumentOrderedMap::getElementById(StringImpl* key, const TreeScope* sc ope) const
140 { 138 {
141 return get<keyMatchesId>(key, scope); 139 return get<keyMatchesId>(key, scope);
142 } 140 }
143 141
142 const Vector<Element*>* DocumentOrderedMap::getAllElementsById(StringImpl* key, const TreeScope* scope) const
143 {
144 ASSERT(key);
145 ASSERT(scope);
146
147 Map::iterator it = m_map.find(key);
148 if (it == m_map.end())
149 return 0;
150
151 MapEntry& entry = it->value;
152 ASSERT(entry.count);
153
154 if (entry.orderedList.isEmpty()) {
155 entry.orderedList.reserveCapacity(entry.count);
156 for (Element* element = entry.element ? entry.element : ElementTraversal ::firstWithin(*scope->rootNode()); entry.orderedList.size() < entry.count; eleme nt = ElementTraversal::next(*element)) {
157 ASSERT(element);
158 if (!keyMatchesId(key, element))
159 continue;
160 entry.orderedList.uncheckedAppend(element);
161 }
162 if (!entry.element)
163 entry.element = entry.orderedList.first();
164 }
165
166 return &entry.orderedList;
167 }
168
144 Element* DocumentOrderedMap::getElementByMapName(StringImpl* key, const TreeScop e* scope) const 169 Element* DocumentOrderedMap::getElementByMapName(StringImpl* key, const TreeScop e* scope) const
145 { 170 {
146 return get<keyMatchesMapName>(key, scope); 171 return get<keyMatchesMapName>(key, scope);
147 } 172 }
148 173
149 Element* DocumentOrderedMap::getElementByLowercasedMapName(StringImpl* key, cons t TreeScope* scope) const 174 Element* DocumentOrderedMap::getElementByLowercasedMapName(StringImpl* key, cons t TreeScope* scope) const
150 { 175 {
151 return get<keyMatchesLowercasedMapName>(key, scope); 176 return get<keyMatchesLowercasedMapName>(key, scope);
152 } 177 }
153 178
154 Element* DocumentOrderedMap::getElementByLabelForAttribute(StringImpl* key, cons t TreeScope* scope) const 179 Element* DocumentOrderedMap::getElementByLabelForAttribute(StringImpl* key, cons t TreeScope* scope) const
155 { 180 {
156 return get<keyMatchesLabelForAttribute>(key, scope); 181 return get<keyMatchesLabelForAttribute>(key, scope);
157 } 182 }
158 183
159 } // namespace WebCore 184 } // namespace WebCore
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698