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

Side by Side Diff: Source/core/css/RuleFeature.cpp

Issue 143873016: Implement style invalidation tree walk for targeted style recalc upon class change. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 6 years, 10 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 | Annotate | Revision Log
« no previous file with comments | « Source/core/css/RuleFeature.h ('k') | Source/core/css/analyzer/DescendantInvalidationSet.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org) 2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 2004-2005 Allan Sandfeld Jensen (kde@carewolf.com) 3 * (C) 2004-2005 Allan Sandfeld Jensen (kde@carewolf.com)
4 * Copyright (C) 2006, 2007 Nicholas Shanks (webkit@nickshanks.com) 4 * Copyright (C) 2006, 2007 Nicholas Shanks (webkit@nickshanks.com)
5 * Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Apple Inc. All r ights reserved. 5 * Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Apple Inc. All r ights reserved.
6 * Copyright (C) 2007 Alexey Proskuryakov <ap@webkit.org> 6 * Copyright (C) 2007 Alexey Proskuryakov <ap@webkit.org>
7 * Copyright (C) 2007, 2008 Eric Seidel <eric@webkit.org> 7 * Copyright (C) 2007, 2008 Eric Seidel <eric@webkit.org>
8 * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.t orchmobile.com/) 8 * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.t orchmobile.com/)
9 * Copyright (c) 2011, Code Aurora Forum. All rights reserved. 9 * Copyright (c) 2011, Code Aurora Forum. All rights reserved.
10 * Copyright (C) Research In Motion Limited 2011. All rights reserved. 10 * Copyright (C) Research In Motion Limited 2011. All rights reserved.
(...skipping 16 matching lines...) Expand all
27 */ 27 */
28 28
29 #include "config.h" 29 #include "config.h"
30 #include "core/css/RuleFeature.h" 30 #include "core/css/RuleFeature.h"
31 31
32 #include "HTMLNames.h" 32 #include "HTMLNames.h"
33 #include "RuntimeEnabledFeatures.h" 33 #include "RuntimeEnabledFeatures.h"
34 #include "core/css/CSSSelector.h" 34 #include "core/css/CSSSelector.h"
35 #include "core/css/CSSSelectorList.h" 35 #include "core/css/CSSSelectorList.h"
36 #include "core/css/RuleSet.h" 36 #include "core/css/RuleSet.h"
37 #include "core/dom/Document.h"
38 #include "core/dom/Element.h"
39 #include "core/dom/Node.h"
40 #include "core/dom/shadow/ElementShadow.h"
41 #include "core/dom/shadow/ShadowRoot.h"
42 #include "wtf/BitVector.h"
37 43
38 namespace WebCore { 44 namespace WebCore {
39 45
40 static bool isSkippableComponentForInvalidation(const CSSSelector& selector) 46 static bool isSkippableComponentForInvalidation(const CSSSelector& selector)
41 { 47 {
42 if (selector.matchesPseudoElement() || selector.pseudoType() == CSSSelector: :PseudoHost) 48 if (selector.matchesPseudoElement() || selector.pseudoType() == CSSSelector: :PseudoHost)
43 return false; 49 return false;
44 return true; 50 return true;
45 } 51 }
46 52
47 // This method is somewhat conservative in what it acceptss. 53 // This method is somewhat conservative in what it acceptss.
48 static bool supportsClassDescendantInvalidation(const CSSSelector& selector) 54 static bool supportsClassDescendantInvalidation(const CSSSelector& selector)
49 { 55 {
50 bool foundDescendantRelation = false; 56 bool foundDescendantRelation = false;
51 bool foundAncestorIdent = false; 57 bool foundAncestorIdent = false;
52 bool foundIdent = false; 58 bool foundIdent = false;
53 for (const CSSSelector* component = &selector; component; component = compon ent->tagHistory()) { 59 for (const CSSSelector* component = &selector; component; component = compon ent->tagHistory()) {
54 60
55 // FIXME: We should allow pseudo elements, but we need to change how the y hook 61 // FIXME: We should allow pseudo elements, but we need to change how the y hook
56 // into recalcStyle by moving them to recalcOwnStyle instead of recalcCh ildStyle. 62 // into recalcStyle by moving them to recalcOwnStyle instead of recalcCh ildStyle.
57 63
58 if (component->m_match == CSSSelector::Tag 64 // FIXME: next up: Tag and Id.
59 || component->m_match == CSSSelector::Id 65 if (component->m_match == CSSSelector::Class) {
60 || component->m_match == CSSSelector::Class) {
61 if (!foundDescendantRelation) 66 if (!foundDescendantRelation)
62 foundIdent = true; 67 foundIdent = true;
63 else 68 else
64 foundAncestorIdent = true; 69 foundAncestorIdent = true;
65 } else if (!isSkippableComponentForInvalidation(*component)) { 70 } else if (!isSkippableComponentForInvalidation(*component)) {
66 return false; 71 return false;
67 } 72 }
68 // FIXME: We can probably support ChildTree and DescendantTree. 73 // FIXME: We can probably support ChildTree and DescendantTree.
69 switch (component->relation()) { 74 switch (component->relation()) {
70 case CSSSelector::Descendant: 75 case CSSSelector::Descendant:
(...skipping 12 matching lines...) Expand all
83 void extractClassIdOrTag(const CSSSelector& selector, HashSet<AtomicString>& cla sses, AtomicString& id, AtomicString& tagName) 88 void extractClassIdOrTag(const CSSSelector& selector, HashSet<AtomicString>& cla sses, AtomicString& id, AtomicString& tagName)
84 { 89 {
85 if (selector.m_match == CSSSelector::Tag) 90 if (selector.m_match == CSSSelector::Tag)
86 tagName = selector.tagQName().localName(); 91 tagName = selector.tagQName().localName();
87 else if (selector.m_match == CSSSelector::Id) 92 else if (selector.m_match == CSSSelector::Id)
88 id = selector.value(); 93 id = selector.value();
89 else if (selector.m_match == CSSSelector::Class) 94 else if (selector.m_match == CSSSelector::Class)
90 classes.add(selector.value()); 95 classes.add(selector.value());
91 } 96 }
92 97
98 RuleFeatureSet::RuleFeatureSet()
99 : m_targetedStyleRecalcEnabled(RuntimeEnabledFeatures::targetedStyleRecalcEn abled())
100 {
101 }
102
93 bool RuleFeatureSet::updateClassInvalidationSets(const CSSSelector& selector) 103 bool RuleFeatureSet::updateClassInvalidationSets(const CSSSelector& selector)
94 { 104 {
95 if (!supportsClassDescendantInvalidation(selector)) 105 if (!supportsClassDescendantInvalidation(selector))
96 return false; 106 return false;
97 107
98 HashSet<AtomicString> classes; 108 HashSet<AtomicString> classes;
99 AtomicString id; 109 AtomicString id;
100 AtomicString tagName; 110 AtomicString tagName;
101 111
102 const CSSSelector* lastSelector = &selector; 112 const CSSSelector* lastSelector = &selector;
(...skipping 15 matching lines...) Expand all
118 } 128 }
119 } 129 }
120 return true; 130 return true;
121 } 131 }
122 132
123 void RuleFeatureSet::addAttributeInASelector(const AtomicString& attributeName) 133 void RuleFeatureSet::addAttributeInASelector(const AtomicString& attributeName)
124 { 134 {
125 m_metadata.attrsInRules.add(attributeName); 135 m_metadata.attrsInRules.add(attributeName);
126 } 136 }
127 137
128
129 void RuleFeatureSet::collectFeaturesFromRuleData(const RuleData& ruleData) 138 void RuleFeatureSet::collectFeaturesFromRuleData(const RuleData& ruleData)
130 { 139 {
131
132 FeatureMetadata metadata; 140 FeatureMetadata metadata;
133 collectFeaturesFromSelector(ruleData.selector(), metadata); 141 collectFeaturesFromSelector(ruleData.selector(), metadata);
134 m_metadata.add(metadata); 142 m_metadata.add(metadata);
135 if (RuntimeEnabledFeatures::targetedStyleRecalcEnabled()) { 143 if (m_targetedStyleRecalcEnabled) {
136 bool selectorUsesClassInvalidationSet = updateClassInvalidationSets(rule Data.selector()); 144 bool selectorUsesClassInvalidationSet = updateClassInvalidationSets(rule Data.selector());
137 if (!selectorUsesClassInvalidationSet) { 145 if (!selectorUsesClassInvalidationSet) {
138 for (HashSet<AtomicString>::const_iterator it = metadata.classesInRu les.begin(); it != metadata.classesInRules.end(); ++it) { 146 for (HashSet<AtomicString>::const_iterator it = metadata.classesInRu les.begin(); it != metadata.classesInRules.end(); ++it) {
139 DescendantInvalidationSet& invalidationSet = ensureClassInvalida tionSet(*it); 147 DescendantInvalidationSet& invalidationSet = ensureClassInvalida tionSet(*it);
140 invalidationSet.setWholeSubtreeInvalid(); 148 invalidationSet.setWholeSubtreeInvalid();
141 } 149 }
142 } 150 }
143 } 151 }
152
144 if (metadata.foundSiblingSelector) 153 if (metadata.foundSiblingSelector)
145 siblingRules.append(RuleFeature(ruleData.rule(), ruleData.selectorIndex( ), ruleData.hasDocumentSecurityOrigin())); 154 siblingRules.append(RuleFeature(ruleData.rule(), ruleData.selectorIndex( ), ruleData.hasDocumentSecurityOrigin()));
146 if (ruleData.containsUncommonAttributeSelector()) 155 if (ruleData.containsUncommonAttributeSelector())
147 uncommonAttributeRules.append(RuleFeature(ruleData.rule(), ruleData.sele ctorIndex(), ruleData.hasDocumentSecurityOrigin())); 156 uncommonAttributeRules.append(RuleFeature(ruleData.rule(), ruleData.sele ctorIndex(), ruleData.hasDocumentSecurityOrigin()));
148 } 157 }
149 158
150 DescendantInvalidationSet& RuleFeatureSet::ensureClassInvalidationSet(const Atom icString& className) 159 DescendantInvalidationSet& RuleFeatureSet::ensureClassInvalidationSet(const Atom icString& className)
151 { 160 {
152 InvalidationSetMap::AddResult addResult = m_classInvalidationSets.add(classN ame, 0); 161 InvalidationSetMap::AddResult addResult = m_classInvalidationSets.add(classN ame, 0);
153 if (addResult.isNewEntry) 162 if (addResult.isNewEntry)
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
231 uncommonAttributeRules.append(other.uncommonAttributeRules); 240 uncommonAttributeRules.append(other.uncommonAttributeRules);
232 } 241 }
233 242
234 void RuleFeatureSet::clear() 243 void RuleFeatureSet::clear()
235 { 244 {
236 m_metadata.clear(); 245 m_metadata.clear();
237 siblingRules.clear(); 246 siblingRules.clear();
238 uncommonAttributeRules.clear(); 247 uncommonAttributeRules.clear();
239 } 248 }
240 249
250 void RuleFeatureSet::scheduleStyleInvalidationForClassChange(const SpaceSplitStr ing& changedClasses, Element* element)
251 {
252 if (computeInvalidationSetsForClassChange(changedClasses, element) && !m_tar getedStyleRecalcEnabled)
253 element->setNeedsStyleRecalc();
254 }
255
256 void RuleFeatureSet::scheduleStyleInvalidationForClassChange(const SpaceSplitStr ing& oldClasses, const SpaceSplitString& newClasses, Element* element)
257 {
258 if (computeInvalidationSetsForClassChange(oldClasses, newClasses, element) & & !m_targetedStyleRecalcEnabled)
259 element->setNeedsStyleRecalc();
260 }
261
262 bool RuleFeatureSet::computeInvalidationSetsForClassChange(const SpaceSplitStrin g& changedClasses, Element* element)
263 {
264 unsigned changedSize = changedClasses.size();
265 for (unsigned i = 0; i < changedSize; ++i) {
266 if (m_targetedStyleRecalcEnabled)
267 addClassToInvalidationSet(changedClasses[i], element);
268 else if (m_metadata.classesInRules.contains(changedClasses[i]))
269 return true;
270 }
271 return false;
272 }
273
274 bool RuleFeatureSet::computeInvalidationSetsForClassChange(const SpaceSplitStrin g& oldClasses, const SpaceSplitString& newClasses, Element* element)
275 {
276 if (!oldClasses.size())
277 return computeInvalidationSetsForClassChange(newClasses, element);
278
279 // Class vectors tend to be very short. This is faster than using a hash tab le.
280 BitVector remainingClassBits;
281 remainingClassBits.ensureSize(oldClasses.size());
282
283 for (unsigned i = 0; i < newClasses.size(); ++i) {
284 bool found = false;
285 for (unsigned j = 0; j < oldClasses.size(); ++j) {
286 if (newClasses[i] == oldClasses[j]) {
287 // Mark each class that is still in the newClasses so we can ski p doing
288 // an n^2 search below when looking for removals. We can't break from
289 // this loop early since a class can appear more than once.
290 remainingClassBits.quickSet(j);
291 found = true;
292 }
293 }
294 // Class was added.
295 if (!found) {
296 if (m_targetedStyleRecalcEnabled)
297 addClassToInvalidationSet(newClasses[i], element);
298 else if (m_metadata.classesInRules.contains(newClasses[i]))
299 return true;
300 }
301 }
302
303 for (unsigned i = 0; i < oldClasses.size(); ++i) {
304 if (remainingClassBits.quickGet(i))
305 continue;
306
307 // Class was removed.
308 if (m_targetedStyleRecalcEnabled)
309 addClassToInvalidationSet(oldClasses[i], element);
310 else if (m_metadata.classesInRules.contains(oldClasses[i]))
311 return true;
312 }
313 return false;
314 }
315
316 void RuleFeatureSet::addClassToInvalidationSet(const AtomicString& className, El ement* element)
317 {
318 if (DescendantInvalidationSet* invalidationSet = m_classInvalidationSets.get (className)) {
319 ensurePendingInvalidationList(element).append(invalidationSet);
320 element->setNeedsStyleInvalidation();
321 }
322 }
323
324 RuleFeatureSet::InvalidationList& RuleFeatureSet::ensurePendingInvalidationList( Element* element)
325 {
326 PendingInvalidationMap::AddResult addResult = m_pendingInvalidationMap.add(e lement, 0);
327 if (addResult.isNewEntry)
328 addResult.iterator->value = new InvalidationList;
329 return *addResult.iterator->value;
330 }
331
332 void RuleFeatureSet::computeStyleInvalidation(Document& document)
333 {
334 Vector<AtomicString> invalidationClasses;
335 if (Element* documentElement = document.documentElement()) {
336 if (documentElement->childNeedsStyleInvalidation()) {
337 invalidateStyleForClassChange(documentElement, invalidationClasses, false);
338 }
339 }
340 document.clearChildNeedsStyleInvalidation();
341 m_pendingInvalidationMap.clear();
342 }
343
344 bool RuleFeatureSet::invalidateStyleForClassChangeOnChildren(Element* element, V ector<AtomicString>& invalidationClasses, bool foundInvalidationSet)
345 {
346 bool someChildrenNeedStyleRecalc = false;
347 for (ShadowRoot* root = element->youngestShadowRoot(); root; root = root->ol derShadowRoot()) {
348 for (Node* child = root->firstChild(); child; child = child->nextSibling ()) {
349 if (child->isElementNode()) {
350 Element* childElement = toElement(child);
351 bool childRecalced = invalidateStyleForClassChange(childElement, invalidationClasses, foundInvalidationSet);
352 someChildrenNeedStyleRecalc = someChildrenNeedStyleRecalc || chi ldRecalced;
353 }
354 }
355 }
356 for (Node* child = element->firstChild(); child; child = child->nextSibling( )) {
357 if (child->isElementNode()) {
358 Element* childElement = toElement(child);
359 bool childRecalced = invalidateStyleForClassChange(childElement, inv alidationClasses, foundInvalidationSet);
360 someChildrenNeedStyleRecalc = someChildrenNeedStyleRecalc || childRe calced;
361 }
362 }
363 return someChildrenNeedStyleRecalc;
364 }
365
366 bool RuleFeatureSet::invalidateStyleForClassChange(Element* element, Vector<Atom icString>& invalidationClasses, bool foundInvalidationSet)
367 {
368 int oldSize = invalidationClasses.size();
369 if (element->needsStyleInvalidation()) {
370 if (InvalidationList* invalidationList = m_pendingInvalidationMap.get(el ement)) {
371 foundInvalidationSet = true;
372 for (InvalidationList::const_iterator it = invalidationList->begin() ; it != invalidationList->end(); ++it) {
373 if ((*it)->wholeSubtreeInvalid()) {
374 element->setNeedsStyleRecalc(SubtreeStyleChange);
375 invalidationClasses.remove(oldSize, invalidationClasses.size () - oldSize);
376 element->clearChildNeedsStyleInvalidation();
377 return true;
378 }
379 (*it)->getClasses(invalidationClasses);
380 }
381 }
382 }
383
384 bool thisElementNeedsStyleRecalc = false;
385
386 if (element->hasClass()) {
387 const SpaceSplitString& classNames = element->classNames();
388 for (Vector<AtomicString>::const_iterator it = invalidationClasses.begin (); it != invalidationClasses.end(); ++it) {
389 if (classNames.contains(*it)) {
390 thisElementNeedsStyleRecalc = true;
391 break;
392 }
393 }
394 }
395
396 // foundInvalidationSet will be true if we are in a subtree of a node with a DescendantInvalidationSet on it.
397 // We need to check all nodes in the subtree of such a node.
398 if (foundInvalidationSet || element->childNeedsStyleInvalidation()) {
399 bool someChildrenNeedStyleRecalc = invalidateStyleForClassChangeOnChildr en(element, invalidationClasses, foundInvalidationSet);
400 // We only need to possibly recalc style if this node is in the subtree of a node with a DescendantInvalidationSet on it.
401 if (foundInvalidationSet)
402 thisElementNeedsStyleRecalc = thisElementNeedsStyleRecalc || someChi ldrenNeedStyleRecalc;
403 }
404
405 if (thisElementNeedsStyleRecalc)
406 element->setNeedsStyleRecalc(LocalStyleChange);
407
408 invalidationClasses.remove(oldSize, invalidationClasses.size() - oldSize);
409 element->clearChildNeedsStyleInvalidation();
410 return thisElementNeedsStyleRecalc;
411 }
412
241 } // namespace WebCore 413 } // namespace WebCore
OLDNEW
« no previous file with comments | « Source/core/css/RuleFeature.h ('k') | Source/core/css/analyzer/DescendantInvalidationSet.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698