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

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

Issue 2569733003: Use hash set instead of vector for changed RuleSets. (Closed)
Patch Set: Created 4 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 // 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/ActiveStyleSheets.h" 5 #include "core/css/ActiveStyleSheets.h"
6 6
7 #include "core/css/CSSStyleSheet.h" 7 #include "core/css/CSSStyleSheet.h"
8 #include "core/css/RuleSet.h" 8 #include "core/css/RuleSet.h"
9 #include "core/css/resolver/ScopedStyleResolver.h" 9 #include "core/css/resolver/ScopedStyleResolver.h"
10 #include "core/dom/ContainerNode.h" 10 #include "core/dom/ContainerNode.h"
11 #include "core/dom/StyleChangeReason.h" 11 #include "core/dom/StyleChangeReason.h"
12 #include "core/dom/StyleEngine.h" 12 #include "core/dom/StyleEngine.h"
13 13
14 namespace blink { 14 namespace blink {
15 15
16 ActiveSheetsChange compareActiveStyleSheets( 16 ActiveSheetsChange compareActiveStyleSheets(
17 const ActiveStyleSheetVector& oldStyleSheets, 17 const ActiveStyleSheetVector& oldStyleSheets,
18 const ActiveStyleSheetVector& newStyleSheets, 18 const ActiveStyleSheetVector& newStyleSheets,
19 HeapVector<Member<RuleSet>>& changedRuleSets) { 19 HeapHashSet<Member<RuleSet>>& changedRuleSets) {
20 unsigned newStyleSheetCount = newStyleSheets.size(); 20 unsigned newStyleSheetCount = newStyleSheets.size();
21 unsigned oldStyleSheetCount = oldStyleSheets.size(); 21 unsigned oldStyleSheetCount = oldStyleSheets.size();
22 22
23 unsigned minCount = std::min(newStyleSheetCount, oldStyleSheetCount); 23 unsigned minCount = std::min(newStyleSheetCount, oldStyleSheetCount);
24 unsigned index = 0; 24 unsigned index = 0;
25 25
26 // Walk the common prefix of stylesheets. If the stylesheet rules were 26 // Walk the common prefix of stylesheets. If the stylesheet rules were
27 // modified since last time, add them to the list of changed rulesets. 27 // modified since last time, add them to the list of changed rulesets.
28 for (; index < minCount && 28 for (; index < minCount &&
29 newStyleSheets[index].first == oldStyleSheets[index].first; 29 newStyleSheets[index].first == oldStyleSheets[index].first;
30 index++) { 30 index++) {
31 if (newStyleSheets[index].second == oldStyleSheets[index].second) 31 if (newStyleSheets[index].second == oldStyleSheets[index].second)
32 continue; 32 continue;
33 33
34 if (newStyleSheets[index].second) 34 if (newStyleSheets[index].second)
35 changedRuleSets.append(newStyleSheets[index].second); 35 changedRuleSets.add(newStyleSheets[index].second);
36 if (oldStyleSheets[index].second) 36 if (oldStyleSheets[index].second)
37 changedRuleSets.append(oldStyleSheets[index].second); 37 changedRuleSets.add(oldStyleSheets[index].second);
38 } 38 }
39 39
40 if (index == oldStyleSheetCount) { 40 if (index == oldStyleSheetCount) {
41 if (index == newStyleSheetCount) 41 if (index == newStyleSheetCount) {
42 return changedRuleSets.size() ? ActiveSheetsChanged 42 return changedRuleSets.isEmpty() ? NoActiveSheetsChanged
43 : NoActiveSheetsChanged; 43 : ActiveSheetsChanged;
44 }
44 45
45 // Sheets added at the end. 46 // Sheets added at the end.
46 for (; index < newStyleSheetCount; index++) { 47 for (; index < newStyleSheetCount; index++) {
47 if (newStyleSheets[index].second) 48 if (newStyleSheets[index].second)
48 changedRuleSets.append(newStyleSheets[index].second); 49 changedRuleSets.add(newStyleSheets[index].second);
49 } 50 }
50 return changedRuleSets.size() ? ActiveSheetsAppended 51 return changedRuleSets.isEmpty() ? NoActiveSheetsChanged
51 : NoActiveSheetsChanged; 52 : ActiveSheetsAppended;
52 } 53 }
53 54
54 if (index == newStyleSheetCount) { 55 if (index == newStyleSheetCount) {
55 // Sheets removed from the end. 56 // Sheets removed from the end.
56 for (; index < oldStyleSheetCount; index++) { 57 for (; index < oldStyleSheetCount; index++) {
57 if (oldStyleSheets[index].second) 58 if (oldStyleSheets[index].second)
58 changedRuleSets.append(oldStyleSheets[index].second); 59 changedRuleSets.add(oldStyleSheets[index].second);
59 } 60 }
60 return changedRuleSets.size() ? ActiveSheetsChanged : NoActiveSheetsChanged; 61 return changedRuleSets.isEmpty() ? NoActiveSheetsChanged
62 : ActiveSheetsChanged;
61 } 63 }
62 64
63 DCHECK(index < oldStyleSheetCount && index < newStyleSheetCount); 65 DCHECK(index < oldStyleSheetCount && index < newStyleSheetCount);
64 66
65 // Both the new and old active stylesheet vectors have stylesheets following 67 // Both the new and old active stylesheet vectors have stylesheets following
66 // the common prefix. Figure out which were added or removed by sorting the 68 // the common prefix. Figure out which were added or removed by sorting the
67 // merged vector of old and new sheets. 69 // merged vector of old and new sheets.
68 70
69 ActiveStyleSheetVector mergedSorted; 71 ActiveStyleSheetVector mergedSorted;
70 mergedSorted.reserveCapacity(oldStyleSheetCount + newStyleSheetCount - 72 mergedSorted.reserveCapacity(oldStyleSheetCount + newStyleSheetCount -
71 2 * index); 73 2 * index);
72 mergedSorted.appendRange(oldStyleSheets.begin() + index, 74 mergedSorted.appendRange(oldStyleSheets.begin() + index,
73 oldStyleSheets.end()); 75 oldStyleSheets.end());
74 mergedSorted.appendRange(newStyleSheets.begin() + index, 76 mergedSorted.appendRange(newStyleSheets.begin() + index,
75 newStyleSheets.end()); 77 newStyleSheets.end());
76 78
77 std::sort(mergedSorted.begin(), mergedSorted.end()); 79 std::sort(mergedSorted.begin(), mergedSorted.end());
78 80
79 auto mergedIterator = mergedSorted.begin(); 81 auto mergedIterator = mergedSorted.begin();
80 while (mergedIterator != mergedSorted.end()) { 82 while (mergedIterator != mergedSorted.end()) {
81 const auto& sheet1 = *mergedIterator++; 83 const auto& sheet1 = *mergedIterator++;
82 if (mergedIterator == mergedSorted.end() || 84 if (mergedIterator == mergedSorted.end() ||
83 (*mergedIterator).first != sheet1.first) { 85 (*mergedIterator).first != sheet1.first) {
84 // Sheet either removed or inserted. 86 // Sheet either removed or inserted.
85 if (sheet1.second) 87 if (sheet1.second)
86 changedRuleSets.append(sheet1.second); 88 changedRuleSets.add(sheet1.second);
87 continue; 89 continue;
88 } 90 }
89 91
90 // Sheet present in both old and new. 92 // Sheet present in both old and new.
91 const auto& sheet2 = *mergedIterator++; 93 const auto& sheet2 = *mergedIterator++;
92 94
93 if (sheet1.second == sheet2.second) 95 if (sheet1.second == sheet2.second)
94 continue; 96 continue;
95 97
96 // Active rules for the given stylesheet changed. 98 // Active rules for the given stylesheet changed.
97 // DOM, CSSOM, or media query changes. 99 // DOM, CSSOM, or media query changes.
98 if (sheet1.second) 100 if (sheet1.second)
99 changedRuleSets.append(sheet1.second); 101 changedRuleSets.add(sheet1.second);
100 if (sheet2.second) 102 if (sheet2.second)
101 changedRuleSets.append(sheet2.second); 103 changedRuleSets.add(sheet2.second);
102 } 104 }
103 return changedRuleSets.size() ? ActiveSheetsChanged : NoActiveSheetsChanged; 105 return changedRuleSets.isEmpty() ? NoActiveSheetsChanged
106 : ActiveSheetsChanged;
104 } 107 }
105 108
106 } // namespace blink 109 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/core/css/ActiveStyleSheets.h ('k') | third_party/WebKit/Source/core/css/ActiveStyleSheetsTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698