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

Side by Side Diff: third_party/WebKit/Source/core/css/parser/CSSLazyParsingState.cpp

Issue 2503683003: [WIP] Streaming CSS parser (Closed)
Patch Set: rebase Created 3 years, 11 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/css/parser/CSSLazyParsingState.h" 5 #include "core/css/parser/CSSLazyParsingState.h"
6 #include "core/css/parser/CSSLazyPropertyParserImpl.h" 6 #include "core/css/parser/CSSLazyPropertyParserImpl.h"
7 #include "core/css/parser/CSSParserTokenRange.h" 7 #include "core/css/parser/CSSParserTokenRange.h"
8 #include "core/dom/Document.h" 8 #include "core/dom/Document.h"
9 #include "core/frame/UseCounter.h" 9 #include "core/frame/UseCounter.h"
10 #include "platform/Histogram.h" 10 #include "platform/Histogram.h"
11 11
12 namespace blink { 12 namespace blink {
13 13
14 CSSLazyParsingState::CSSLazyParsingState(const CSSParserContext& context, 14 CSSLazyParsingState::CSSLazyParsingState(const CSSParserContext& context,
15 Vector<String> escapedStrings,
16 const String& sheetText, 15 const String& sheetText,
17 StyleSheetContents* contents) 16 StyleSheetContents* contents)
18 : m_context(context), 17 : m_context(context),
19 m_escapedStrings(std::move(escapedStrings)),
20 m_sheetText(sheetText), 18 m_sheetText(sheetText),
21 m_owningContents(contents), 19 m_owningContents(contents),
22 m_parsedStyleRules(0), 20 m_parsedStyleRules(0),
23 m_totalStyleRules(0), 21 m_totalStyleRules(0),
24 m_styleRulesNeededForNextMilestone(0), 22 m_styleRulesNeededForNextMilestone(0),
25 m_usage(UsageGe0), 23 m_usage(UsageGe0),
26 m_shouldUseCount(!!m_context.useCounter()) { 24 m_shouldUseCount(!!m_context.useCounter()) {
27 recordUsageMetrics(); 25 recordUsageMetrics();
28 } 26 }
29 27
30 CSSLazyPropertyParserImpl* CSSLazyParsingState::createLazyParser( 28 CSSLazyPropertyParserImpl* CSSLazyParsingState::createLazyParser(
31 const CSSParserTokenRange& block) { 29 size_t blockOffset) {
32 ++m_totalStyleRules; 30 ++m_totalStyleRules;
33 return new CSSLazyPropertyParserImpl(std::move(block), this); 31 return new CSSLazyPropertyParserImpl(blockOffset, this);
34 } 32 }
35 33
36 const CSSParserContext& CSSLazyParsingState::context() { 34 const CSSParserContext& CSSLazyParsingState::context() {
37 DCHECK(m_owningContents); 35 DCHECK(m_owningContents);
38 if (!m_shouldUseCount) { 36 if (!m_shouldUseCount) {
39 DCHECK(!m_context.useCounter()); 37 DCHECK(!m_context.useCounter());
40 return m_context; 38 return m_context;
41 } 39 }
42 40
43 // Try as best as possible to grab a valid UseCounter if the underlying 41 // Try as best as possible to grab a valid UseCounter if the underlying
(...skipping 12 matching lines...) Expand all
56 void CSSLazyParsingState::countRuleParsed() { 54 void CSSLazyParsingState::countRuleParsed() {
57 ++m_parsedStyleRules; 55 ++m_parsedStyleRules;
58 while (m_parsedStyleRules > m_styleRulesNeededForNextMilestone) { 56 while (m_parsedStyleRules > m_styleRulesNeededForNextMilestone) {
59 DCHECK_NE(UsageAll, m_usage); 57 DCHECK_NE(UsageAll, m_usage);
60 ++m_usage; 58 ++m_usage;
61 recordUsageMetrics(); 59 recordUsageMetrics();
62 } 60 }
63 } 61 }
64 62
65 bool CSSLazyParsingState::shouldLazilyParseProperties( 63 bool CSSLazyParsingState::shouldLazilyParseProperties(
66 const CSSSelectorList& selectors, 64 const CSSSelectorList& selectors) const {
67 const CSSParserTokenRange& block) const {
68 // Simple heuristic for an empty block. Note that |block| here does not
69 // include {} brackets. We avoid lazy parsing empty blocks so we can avoid
70 // considering them when possible for matching. Lazy blocks must always be
71 // considered. Three tokens is a reasonable minimum for a block:
72 // ident ':' <value>.
73 if (block.end() - block.begin() <= 2)
74 return false;
75
76 // Disallow lazy parsing for blocks which have before/after in their selector 65 // Disallow lazy parsing for blocks which have before/after in their selector
77 // list. This ensures we don't cause a collectFeatures() when we trigger 66 // list. This ensures we don't cause a collectFeatures() when we trigger
78 // parsing for attr() functions which would trigger expensive invalidation 67 // parsing for attr() functions which would trigger expensive invalidation
79 // propagation. 68 // propagation.
80 for (const auto* s = selectors.first(); s; s = CSSSelectorList::next(*s)) { 69 for (const auto* s = selectors.first(); s; s = CSSSelectorList::next(*s)) {
81 for (const CSSSelector* current = s; current; 70 for (const CSSSelector* current = s; current;
82 current = current->tagHistory()) { 71 current = current->tagHistory()) {
83 const CSSSelector::PseudoType type(current->getPseudoType()); 72 const CSSSelector::PseudoType type(current->getPseudoType());
84 if (type == CSSSelector::PseudoBefore || type == CSSSelector::PseudoAfter) 73 if (type == CSSSelector::PseudoBefore || type == CSSSelector::PseudoAfter)
85 return false; 74 return false;
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 108
120 usageHistogram.count(m_usage); 109 usageHistogram.count(m_usage);
121 } 110 }
122 111
123 DEFINE_TRACE(CSSLazyParsingState) { 112 DEFINE_TRACE(CSSLazyParsingState) {
124 visitor->trace(m_owningContents); 113 visitor->trace(m_owningContents);
125 visitor->trace(m_document); 114 visitor->trace(m_document);
126 } 115 }
127 116
128 } // namespace blink 117 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698