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: Source/core/css/resolver/StyleResolver.cpp

Issue 15871005: Avoid N^2 walk placing renderers when building the render tree (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Merging ToT Created 7 years, 6 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 /* 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, 2013 Apple Inc. All rights reserved. 5 * Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Apple Inc. All rights 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 549 matching lines...) Expand 10 before | Expand all | Expand 10 after
560 } 560 }
561 561
562 // Now check SMIL animation override style. 562 // Now check SMIL animation override style.
563 if (includeSMILProperties && matchAuthorAndUserStyles && m_state.styledEleme nt() && m_state.styledElement()->isSVGElement()) 563 if (includeSMILProperties && matchAuthorAndUserStyles && m_state.styledEleme nt() && m_state.styledElement()->isSVGElement())
564 collector.addElementStyleProperties(toSVGElement(m_state.styledElement() )->animatedSMILStyleProperties(), false /* isCacheable */); 564 collector.addElementStyleProperties(toSVGElement(m_state.styledElement() )->animatedSMILStyleProperties(), false /* isCacheable */);
565 565
566 if (m_state.styledElement() && m_state.styledElement()->hasActiveAnimations( )) 566 if (m_state.styledElement() && m_state.styledElement()->hasActiveAnimations( ))
567 collector.matchedResult().isCacheable = false; 567 collector.matchedResult().isCacheable = false;
568 } 568 }
569 569
570 inline void StyleResolver::initElement(Element* e) 570 inline void StyleResolver::initElement(Element* e, int childIndex)
571 { 571 {
572 if (m_state.element() != e) { 572 if (m_state.element() != e) {
573 m_state.initElement(e); 573 m_state.initElement(e, childIndex);
574 if (e && e == e->document()->documentElement()) { 574 if (e && e == e->document()->documentElement()) {
575 e->document()->setDirectionSetOnDocumentElement(false); 575 e->document()->setDirectionSetOnDocumentElement(false);
576 e->document()->setWritingModeSetOnDocumentElement(false); 576 e->document()->setWritingModeSetOnDocumentElement(false);
577 } 577 }
578 } 578 }
579 } 579 }
580 580
581 static const unsigned cStyleSearchThreshold = 10; 581 static const unsigned cStyleSearchThreshold = 10;
582 static const unsigned cStyleSearchLevelThreshold = 10; 582 static const unsigned cStyleSearchLevelThreshold = 10;
583 583
(...skipping 16 matching lines...) Expand all
600 if (p->inlineStyle()) 600 if (p->inlineStyle())
601 return 0; 601 return 0;
602 if (p->isSVGElement() && toSVGElement(p)->animatedSMILStyleProperties()) 602 if (p->isSVGElement() && toSVGElement(p)->animatedSMILStyleProperties())
603 return 0; 603 return 0;
604 if (p->hasID() && m_features.idsInRules.contains(p->idForStyleResolution().i mpl())) 604 if (p->hasID() && m_features.idsInRules.contains(p->idForStyleResolution().i mpl()))
605 return 0; 605 return 0;
606 606
607 RenderStyle* parentStyle = p->renderStyle(); 607 RenderStyle* parentStyle = p->renderStyle();
608 unsigned subcount = 0; 608 unsigned subcount = 0;
609 Node* thisCousin = p; 609 Node* thisCousin = p;
610 Node* currentNode = p->previousSibling(); 610 Node* currentNode = p->nextSibling();
611 611
612 // Reserve the tries for this level. This effectively makes sure that the al gorithm 612 // Reserve the tries for this level. This effectively makes sure that the al gorithm
613 // will never go deeper than cStyleSearchLevelThreshold levels into recursio n. 613 // will never go deeper than cStyleSearchLevelThreshold levels into recursio n.
614 visitedNodeCount += cStyleSearchThreshold; 614 visitedNodeCount += cStyleSearchThreshold;
615 while (thisCousin) { 615 while (thisCousin) {
616 while (currentNode) { 616 while (currentNode) {
617 ++subcount; 617 ++subcount;
618 if (!currentNode->hasScopedHTMLStyleChild() && currentNode->renderSt yle() == parentStyle && currentNode->lastChild() 618 if (!currentNode->hasScopedHTMLStyleChild() && currentNode->renderSt yle() == parentStyle && currentNode->lastChild()
619 && currentNode->isElementNode() && !parentElementPreventsSharing (toElement(currentNode)) 619 && currentNode->isElementNode() && !parentElementPreventsSharing (toElement(currentNode))
620 && !toElement(currentNode)->shadow() 620 && !toElement(currentNode)->shadow()
621 ) { 621 ) {
622 // Adjust for unused reserved tries. 622 // Adjust for unused reserved tries.
623 visitedNodeCount -= cStyleSearchThreshold - subcount; 623 visitedNodeCount -= cStyleSearchThreshold - subcount;
624 return currentNode->lastChild(); 624 return currentNode->lastChild();
625 } 625 }
626 if (subcount >= cStyleSearchThreshold) 626 if (subcount >= cStyleSearchThreshold)
627 return 0; 627 return 0;
628 currentNode = currentNode->previousSibling(); 628 currentNode = currentNode->nextSibling();
629 } 629 }
630 currentNode = locateCousinList(thisCousin->parentElement(), visitedNodeC ount); 630 currentNode = locateCousinList(thisCousin->parentElement(), visitedNodeC ount);
631 thisCousin = currentNode; 631 thisCousin = currentNode;
632 } 632 }
633 633
634 return 0; 634 return 0;
635 } 635 }
636 636
637 bool StyleResolver::styleSharingCandidateMatchesRuleSet(RuleSet* ruleSet) 637 bool StyleResolver::styleSharingCandidateMatchesRuleSet(RuleSet* ruleSet)
638 { 638 {
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after
814 if (element->isWebVTTElement() && state.element()->isWebVTTElement() && toWe bVTTElement(element)->isPastNode() != toWebVTTElement(state.element())->isPastNo de()) 814 if (element->isWebVTTElement() && state.element()->isWebVTTElement() && toWe bVTTElement(element)->isPastNode() != toWebVTTElement(state.element())->isPastNo de())
815 return false; 815 return false;
816 816
817 if (element == element->document()->webkitCurrentFullScreenElement() || stat e.element() == state.document()->webkitCurrentFullScreenElement()) 817 if (element == element->document()->webkitCurrentFullScreenElement() || stat e.element() == state.document()->webkitCurrentFullScreenElement())
818 return false; 818 return false;
819 return true; 819 return true;
820 } 820 }
821 821
822 inline StyledElement* StyleResolver::findSiblingForStyleSharing(Node* node, unsi gned& count) const 822 inline StyledElement* StyleResolver::findSiblingForStyleSharing(Node* node, unsi gned& count) const
823 { 823 {
824 for (; node; node = node->previousSibling()) { 824 for (; node; node = node->nextSibling()) {
825 if (!node->isStyledElement()) 825 if (!node->isStyledElement())
826 continue; 826 continue;
827 if (canShareStyleWithElement(static_cast<StyledElement*>(node))) 827 if (canShareStyleWithElement(static_cast<StyledElement*>(node)))
828 break; 828 break;
829 if (count++ == cStyleSearchThreshold) 829 if (count++ == cStyleSearchThreshold)
830 return 0; 830 return 0;
831 } 831 }
832 return static_cast<StyledElement*>(node); 832 return static_cast<StyledElement*>(node);
833 } 833 }
834 834
(...skipping 19 matching lines...) Expand all
854 return 0; 854 return 0;
855 if (elementHasDirectionAuto(state.element())) 855 if (elementHasDirectionAuto(state.element()))
856 return 0; 856 return 0;
857 if (state.element()->hasActiveAnimations()) 857 if (state.element()->hasActiveAnimations())
858 return 0; 858 return 0;
859 859
860 // Cache whether state.element is affected by any known class selectors. 860 // Cache whether state.element is affected by any known class selectors.
861 // FIXME: This shouldn't be a member variable. The style sharing code could be factored out of StyleResolver. 861 // FIXME: This shouldn't be a member variable. The style sharing code could be factored out of StyleResolver.
862 state.setElementAffectedByClassRules(state.element() && state.element()->has Class() && classNamesAffectedByRules(state.element()->classNames())); 862 state.setElementAffectedByClassRules(state.element() && state.element()->has Class() && classNamesAffectedByRules(state.element()->classNames()));
863 863
864 // Check previous siblings and their cousins. 864 // Check next siblings and their cousins.
865 unsigned count = 0; 865 unsigned count = 0;
866 unsigned visitedNodeCount = 0; 866 unsigned visitedNodeCount = 0;
867 StyledElement* shareElement = 0; 867 StyledElement* shareElement = 0;
868 Node* cousinList = state.styledElement()->previousSibling(); 868 Node* cousinList = state.styledElement()->nextSibling();
869 while (cousinList) { 869 while (cousinList) {
870 shareElement = findSiblingForStyleSharing(cousinList, count); 870 shareElement = findSiblingForStyleSharing(cousinList, count);
871 if (shareElement) 871 if (shareElement)
872 break; 872 break;
873 cousinList = locateCousinList(cousinList->parentElement(), visitedNodeCo unt); 873 cousinList = locateCousinList(cousinList->parentElement(), visitedNodeCo unt);
874 } 874 }
875 875
876 // If we have exhausted all our budget or our cousins. 876 // If we have exhausted all our budget or our cousins.
877 if (!shareElement) 877 if (!shareElement)
878 return 0; 878 return 0;
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
1061 } 1061 }
1062 1062
1063 static inline bool isAtShadowBoundary(const Element* element) 1063 static inline bool isAtShadowBoundary(const Element* element)
1064 { 1064 {
1065 if (!element) 1065 if (!element)
1066 return false; 1066 return false;
1067 ContainerNode* parentNode = element->parentNode(); 1067 ContainerNode* parentNode = element->parentNode();
1068 return parentNode && parentNode->isShadowRoot(); 1068 return parentNode && parentNode->isShadowRoot();
1069 } 1069 }
1070 1070
1071 PassRefPtr<RenderStyle> StyleResolver::styleForElement(Element* element, RenderS tyle* defaultParent, 1071 PassRefPtr<RenderStyle> StyleResolver::styleForElement(Element* element, RenderS tyle* defaultParent, StyleSharingBehavior sharingBehavior,
1072 StyleSharingBehavior sharingBehavior, RuleMatchingBehavior matchingBehavior, RenderRegion* regionForStyling) 1072 RuleMatchingBehavior matchingBehavior, RenderRegion* regionForStyling, int c hildIndex)
1073 { 1073 {
1074 // Once an element has a renderer, we don't try to destroy it, since otherwi se the renderer 1074 // Once an element has a renderer, we don't try to destroy it, since otherwi se the renderer
1075 // will vanish if a style recalc happens during loading. 1075 // will vanish if a style recalc happens during loading.
1076 if (sharingBehavior == AllowStyleSharing && !element->document()->haveStyles heetsLoaded() && !element->renderer()) { 1076 if (sharingBehavior == AllowStyleSharing && !element->document()->haveStyles heetsLoaded() && !element->renderer()) {
1077 if (!s_styleNotYetAvailable) { 1077 if (!s_styleNotYetAvailable) {
1078 s_styleNotYetAvailable = RenderStyle::create().leakRef(); 1078 s_styleNotYetAvailable = RenderStyle::create().leakRef();
1079 s_styleNotYetAvailable->setDisplay(NONE); 1079 s_styleNotYetAvailable->setDisplay(NONE);
1080 s_styleNotYetAvailable->font().update(m_fontSelector); 1080 s_styleNotYetAvailable->font().update(m_fontSelector);
1081 } 1081 }
1082 element->document()->setHasNodesWithPlaceholderStyle(); 1082 element->document()->setHasNodesWithPlaceholderStyle();
1083 return s_styleNotYetAvailable; 1083 return s_styleNotYetAvailable;
1084 } 1084 }
1085 1085
1086 StyleResolverState& state = m_state; 1086 StyleResolverState& state = m_state;
1087 initElement(element); 1087 initElement(element, childIndex);
1088 state.initForStyleResolve(document(), element, defaultParent, regionForStyli ng); 1088 state.initForStyleResolve(document(), element, defaultParent, regionForStyli ng);
1089 if (sharingBehavior == AllowStyleSharing && !state.distributedToInsertionPoi nt()) { 1089 if (sharingBehavior == AllowStyleSharing && !state.distributedToInsertionPoi nt()) {
1090 RenderStyle* sharedStyle = locateSharedStyle(); 1090 RenderStyle* sharedStyle = locateSharedStyle();
1091 if (sharedStyle) { 1091 if (sharedStyle) {
1092 state.clear(); 1092 state.clear();
1093 return sharedStyle; 1093 return sharedStyle;
1094 } 1094 }
1095 } 1095 }
1096 1096
1097 if (state.parentStyle()) { 1097 if (state.parentStyle()) {
(...skipping 2630 matching lines...) Expand 10 before | Expand all | Expand 10 after
3728 info.addMember(m_state, "state"); 3728 info.addMember(m_state, "state");
3729 3729
3730 // FIXME: move this to a place where it would be called only once? 3730 // FIXME: move this to a place where it would be called only once?
3731 info.addMember(CSSDefaultStyleSheets::defaultStyle, "defaultStyle"); 3731 info.addMember(CSSDefaultStyleSheets::defaultStyle, "defaultStyle");
3732 info.addMember(CSSDefaultStyleSheets::defaultQuirksStyle, "defaultQuirksStyl e"); 3732 info.addMember(CSSDefaultStyleSheets::defaultQuirksStyle, "defaultQuirksStyl e");
3733 info.addMember(CSSDefaultStyleSheets::defaultPrintStyle, "defaultPrintStyle" ); 3733 info.addMember(CSSDefaultStyleSheets::defaultPrintStyle, "defaultPrintStyle" );
3734 info.addMember(CSSDefaultStyleSheets::defaultViewSourceStyle, "defaultViewSo urceStyle"); 3734 info.addMember(CSSDefaultStyleSheets::defaultViewSourceStyle, "defaultViewSo urceStyle");
3735 } 3735 }
3736 3736
3737 } // namespace WebCore 3737 } // namespace WebCore
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698