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

Side by Side Diff: third_party/WebKit/Source/core/dom/custom/CustomElementRegistry.cpp

Issue 2828643002: Make customElements.define faster
Patch Set: Remove some unneeded casts. Created 3 years, 7 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
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "core/dom/custom/CustomElementRegistry.h" 5 #include "core/dom/custom/CustomElementRegistry.h"
6 6
7 #include <limits>
7 #include "bindings/core/v8/ExceptionState.h" 8 #include "bindings/core/v8/ExceptionState.h"
8 #include "bindings/core/v8/ScriptCustomElementDefinitionBuilder.h" 9 #include "bindings/core/v8/ScriptCustomElementDefinitionBuilder.h"
9 #include "bindings/core/v8/ScriptPromise.h" 10 #include "bindings/core/v8/ScriptPromise.h"
10 #include "bindings/core/v8/ScriptPromiseResolver.h" 11 #include "bindings/core/v8/ScriptPromiseResolver.h"
11 #include "core/HTMLElementTypeHelpers.h" 12 #include "core/HTMLElementTypeHelpers.h"
12 #include "core/dom/Document.h" 13 #include "core/dom/Document.h"
13 #include "core/dom/Element.h" 14 #include "core/dom/Element.h"
14 #include "core/dom/ElementDefinitionOptions.h" 15 #include "core/dom/ElementDefinitionOptions.h"
15 #include "core/dom/ExceptionCode.h" 16 #include "core/dom/ExceptionCode.h"
16 #include "core/dom/custom/CEReactionsScope.h" 17 #include "core/dom/custom/CEReactionsScope.h"
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 DEFINE_TRACE(CustomElementRegistry) { 87 DEFINE_TRACE(CustomElementRegistry) {
87 visitor->Trace(definitions_); 88 visitor->Trace(definitions_);
88 visitor->Trace(owner_); 89 visitor->Trace(owner_);
89 visitor->Trace(v0_); 90 visitor->Trace(v0_);
90 visitor->Trace(upgrade_candidates_); 91 visitor->Trace(upgrade_candidates_);
91 visitor->Trace(when_defined_promise_map_); 92 visitor->Trace(when_defined_promise_map_);
92 } 93 }
93 94
94 DEFINE_TRACE_WRAPPERS(CustomElementRegistry) { 95 DEFINE_TRACE_WRAPPERS(CustomElementRegistry) {
95 visitor->TraceWrappers(&CustomElementReactionStack::Current()); 96 visitor->TraceWrappers(&CustomElementReactionStack::Current());
97 for (auto definition : definitions_)
98 visitor->TraceWrappers(definition);
96 } 99 }
97 100
98 CustomElementDefinition* CustomElementRegistry::define( 101 CustomElementDefinition* CustomElementRegistry::define(
99 ScriptState* script_state, 102 ScriptState* script_state,
100 const AtomicString& name, 103 const AtomicString& name,
101 const ScriptValue& constructor, 104 const ScriptValue& constructor,
102 const ElementDefinitionOptions& options, 105 const ElementDefinitionOptions& options,
103 ExceptionState& exception_state) { 106 ExceptionState& exception_state) {
104 ScriptCustomElementDefinitionBuilder builder(script_state, this, constructor, 107 ScriptCustomElementDefinitionBuilder builder(script_state, this, constructor,
105 exception_state); 108 exception_state);
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
176 return nullptr; 179 return nullptr;
177 180
178 // "Then, perform the following substep, regardless of whether 181 // "Then, perform the following substep, regardless of whether
179 // the above steps threw an exception or not: Unset this 182 // the above steps threw an exception or not: Unset this
180 // CustomElementRegistry's element definition is running 183 // CustomElementRegistry's element definition is running
181 // flag." 184 // flag."
182 // (ElementDefinitionIsRunning destructor does this.) 185 // (ElementDefinitionIsRunning destructor does this.)
183 } 186 }
184 187
185 CustomElementDescriptor descriptor(name, local_name); 188 CustomElementDescriptor descriptor(name, local_name);
186 CustomElementDefinition* definition = builder.Build(descriptor); 189 if (UNLIKELY(definitions_.size() >= std::numeric_limits<uint32_t>::max()))
190 return nullptr;
191 uint32_t id = definitions_.size() + 1;
192 CustomElementDefinition* definition = builder.Build(descriptor, id);
187 CHECK(!exception_state.HadException()); 193 CHECK(!exception_state.HadException());
188 CHECK(definition->Descriptor() == descriptor); 194 CHECK(definition->Descriptor() == descriptor);
189 DefinitionMap::AddResult result = 195 definitions_.emplace_back(
190 definitions_.insert(descriptor.GetName(), definition); 196 TraceWrapperMember<CustomElementDefinition>(this, definition));
197 DefinitionMap::AddResult result = nameIdMap_.insert(descriptor.GetName(), id);
191 CHECK(result.is_new_entry); 198 CHECK(result.is_new_entry);
192 199
193 HeapVector<Member<Element>> candidates; 200 HeapVector<Member<Element>> candidates;
194 CollectCandidates(descriptor, &candidates); 201 CollectCandidates(descriptor, &candidates);
195 for (Element* candidate : candidates) 202 for (Element* candidate : candidates)
196 definition->EnqueueUpgradeReaction(candidate); 203 definition->EnqueueUpgradeReaction(candidate);
197 204
198 // 16: when-defined promise processing 205 // 16: when-defined promise processing
199 const auto& entry = when_defined_promise_map_.find(name); 206 const auto& entry = when_defined_promise_map_.find(name);
200 if (entry != when_defined_promise_map_.end()) { 207 if (entry != when_defined_promise_map_.end()) {
(...skipping 26 matching lines...) Expand all
227 definition = DefinitionForName(desc.GetName()); 234 definition = DefinitionForName(desc.GetName());
228 // 4&5. ...and local name equal to localName, return that definition 235 // 4&5. ...and local name equal to localName, return that definition
229 if (definition and definition->Descriptor().LocalName() == desc.LocalName()) { 236 if (definition and definition->Descriptor().LocalName() == desc.LocalName()) {
230 return definition; 237 return definition;
231 } 238 }
232 // 6. Return null 239 // 6. Return null
233 return nullptr; 240 return nullptr;
234 } 241 }
235 242
236 bool CustomElementRegistry::NameIsDefined(const AtomicString& name) const { 243 bool CustomElementRegistry::NameIsDefined(const AtomicString& name) const {
237 return definitions_.Contains(name); 244 return nameIdMap_.Contains(name);
238 } 245 }
239 246
240 void CustomElementRegistry::Entangle(V0CustomElementRegistrationContext* v0) { 247 void CustomElementRegistry::Entangle(V0CustomElementRegistrationContext* v0) {
241 v0_->insert(v0); 248 v0_->insert(v0);
242 v0->SetV1(this); 249 v0->SetV1(this);
243 } 250 }
244 251
245 bool CustomElementRegistry::V0NameIsDefined(const AtomicString& name) { 252 bool CustomElementRegistry::V0NameIsDefined(const AtomicString& name) {
246 for (const auto& v0 : *v0_) { 253 for (const auto& v0 : *v0_) {
247 if (v0->NameIsDefined(name)) 254 if (v0->NameIsDefined(name))
248 return true; 255 return true;
249 } 256 }
250 return false; 257 return false;
251 } 258 }
252 259
253 CustomElementDefinition* CustomElementRegistry::DefinitionForName( 260 CustomElementDefinition* CustomElementRegistry::DefinitionForName(
254 const AtomicString& name) const { 261 const AtomicString& name) const {
255 return definitions_.at(name); 262 return DefinitionForId(nameIdMap_.at(name));
263 }
264
265 CustomElementDefinition* CustomElementRegistry::DefinitionForId(
266 CustomElementDefinition::Id id) const {
kochi 2017/04/28 06:24:15 Add DCHECK(id <= difinitions_.size())?
267 return id ? definitions_[id - 1].Get() : nullptr;
256 } 268 }
257 269
258 void CustomElementRegistry::AddCandidate(Element* candidate) { 270 void CustomElementRegistry::AddCandidate(Element* candidate) {
259 const AtomicString& name = candidate->localName(); 271 const AtomicString& name = candidate->localName();
260 if (NameIsDefined(name) || V0NameIsDefined(name)) 272 if (NameIsDefined(name) || V0NameIsDefined(name))
261 return; 273 return;
262 UpgradeCandidateMap::iterator it = upgrade_candidates_->find(name); 274 UpgradeCandidateMap::iterator it = upgrade_candidates_->find(name);
263 UpgradeCandidateSet* set; 275 UpgradeCandidateSet* set;
264 if (it != upgrade_candidates_->end()) { 276 if (it != upgrade_candidates_->end()) {
265 set = it->value; 277 set = it->value;
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
305 upgrade_candidates_->erase(it); 317 upgrade_candidates_->erase(it);
306 318
307 Document* document = owner_->document(); 319 Document* document = owner_->document();
308 if (!document) 320 if (!document)
309 return; 321 return;
310 322
311 sorter.Sorted(elements, document); 323 sorter.Sorted(elements, document);
312 } 324 }
313 325
314 } // namespace blink 326 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698