Title: [285574] trunk/Source/WebCore
Revision
285574
Author
[email protected]
Date
2021-11-10 06:40:24 -0800 (Wed, 10 Nov 2021)

Log Message

Use Hasher for hashing MatchResult for MatchedDeclarationsCache
https://bugs.webkit.org/show_bug.cgi?id=232930

Reviewed by Kimmo Kinnunen.

We currently use hashMemory over a Vector<MatchedProperties>. This works correctly only as long as
the MatchedProperties struct is fully packed. Any unitilized memory in the struct leads to badness.

* WebCore.xcodeproj/project.pbxproj:
* style/ElementRuleCollector.h:
(WebCore::Style::MatchResult::operator== const): Deleted.
(WebCore::Style::MatchResult::operator!= const): Deleted.
(WebCore::Style::MatchResult::isEmpty const): Deleted.
(WebCore::Style::operator==): Deleted.
(WebCore::Style::operator!=): Deleted.

Move MatchResult to a file of its own.

* style/MatchResult.h: Added.
(WebCore::Style::MatchResult::isEmpty const):
(WebCore::Style::operator==):
(WebCore::Style::operator!=):
(WebCore::Style::add):

Implement Hasher functions for the types.

* style/MatchedDeclarationsCache.cpp:
(WebCore::Style::MatchedDeclarationsCache::computeHash):

use WTF::computeHash

* style/MatchedDeclarationsCache.h:
* style/PageRuleCollector.h:
* style/PropertyCascade.h:

Modified Paths

Added Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (285573 => 285574)


--- trunk/Source/WebCore/ChangeLog	2021-11-10 14:31:47 UTC (rev 285573)
+++ trunk/Source/WebCore/ChangeLog	2021-11-10 14:40:24 UTC (rev 285574)
@@ -1,3 +1,40 @@
+2021-11-10  Antti Koivisto  <[email protected]>
+
+        Use Hasher for hashing MatchResult for MatchedDeclarationsCache
+        https://bugs.webkit.org/show_bug.cgi?id=232930
+
+        Reviewed by Kimmo Kinnunen.
+
+        We currently use hashMemory over a Vector<MatchedProperties>. This works correctly only as long as
+        the MatchedProperties struct is fully packed. Any unitilized memory in the struct leads to badness.
+
+        * WebCore.xcodeproj/project.pbxproj:
+        * style/ElementRuleCollector.h:
+        (WebCore::Style::MatchResult::operator== const): Deleted.
+        (WebCore::Style::MatchResult::operator!= const): Deleted.
+        (WebCore::Style::MatchResult::isEmpty const): Deleted.
+        (WebCore::Style::operator==): Deleted.
+        (WebCore::Style::operator!=): Deleted.
+
+        Move MatchResult to a file of its own.
+
+        * style/MatchResult.h: Added.
+        (WebCore::Style::MatchResult::isEmpty const):
+        (WebCore::Style::operator==):
+        (WebCore::Style::operator!=):
+        (WebCore::Style::add):
+
+        Implement Hasher functions for the types.
+
+        * style/MatchedDeclarationsCache.cpp:
+        (WebCore::Style::MatchedDeclarationsCache::computeHash):
+
+        use WTF::computeHash
+
+        * style/MatchedDeclarationsCache.h:
+        * style/PageRuleCollector.h:
+        * style/PropertyCascade.h:
+
 2021-11-10  Enrique Ocaña González  <[email protected]>
 
         [Media] Make currentTime compliant with the spec when readyState is HAVE_NOTHING

Modified: trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj (285573 => 285574)


--- trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj	2021-11-10 14:31:47 UTC (rev 285573)
+++ trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj	2021-11-10 14:40:24 UTC (rev 285574)
@@ -5267,6 +5267,7 @@
 		E446143C0CD689CC00FADA75 /* JSHTMLSourceElement.h in Headers */ = {isa = PBXBuildFile; fileRef = E4B423720CBFB6E000AF2ECE /* JSHTMLSourceElement.h */; };
 		E44614520CD68A3500FADA75 /* RenderVideo.h in Headers */ = {isa = PBXBuildFile; fileRef = E4B41E340CBFB60900AF2ECE /* RenderVideo.h */; settings = {ATTRIBUTES = (Private, ); }; };
 		E447F5B3266CF63D00133F00 /* TextBoxSelectableRange.h in Headers */ = {isa = PBXBuildFile; fileRef = E447F5B1266CF63D00133F00 /* TextBoxSelectableRange.h */; settings = {ATTRIBUTES = (Private, ); }; };
+		E44A0256273AE0C000993A56 /* MatchResult.h in Headers */ = {isa = PBXBuildFile; fileRef = E44A0254273AE0BF00993A56 /* MatchResult.h */; };
 		E44B4BB4141650D7002B1D8B /* SelectorChecker.h in Headers */ = {isa = PBXBuildFile; fileRef = E44B4BB2141650D7002B1D8B /* SelectorChecker.h */; };
 		E44FA1851BCA6B5A0091B6EF /* ComposedTreeIterator.h in Headers */ = {isa = PBXBuildFile; fileRef = E44FA1841BCA6B5A0091B6EF /* ComposedTreeIterator.h */; };
 		E451C6312394027900993190 /* LayoutUnits.h in Headers */ = {isa = PBXBuildFile; fileRef = 6F73918C2106CEDD006AF262 /* LayoutUnits.h */; settings = {ATTRIBUTES = (Private, ); }; };
@@ -16872,6 +16873,7 @@
 		E44614120CD6826900FADA75 /* JSTimeRanges.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = JSTimeRanges.cpp; sourceTree = "<group>"; };
 		E44614130CD6826900FADA75 /* JSTimeRanges.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSTimeRanges.h; sourceTree = "<group>"; };
 		E447F5B1266CF63D00133F00 /* TextBoxSelectableRange.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TextBoxSelectableRange.h; sourceTree = "<group>"; };
+		E44A0254273AE0BF00993A56 /* MatchResult.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MatchResult.h; sourceTree = "<group>"; };
 		E44B4BB1141650D7002B1D8B /* SelectorChecker.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = SelectorChecker.cpp; sourceTree = "<group>"; };
 		E44B4BB2141650D7002B1D8B /* SelectorChecker.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SelectorChecker.h; sourceTree = "<group>"; };
 		E44EE3A617576E5500EEE8CF /* FontGenericFamilies.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = FontGenericFamilies.cpp; sourceTree = "<group>"; };
@@ -18918,11 +18920,11 @@
 		1A569CC40D7E2B60007C3983 /* bridge */ = {
 			isa = PBXGroup;
 			children = (
+				59B5977111086556007159E8 /* jsc */,
 				1A569CE20D7E2B82007C3983 /* objc */,
 				599E758F11055A1F00D904FA /* Bridge.h */,
 				1A71D5790F33819000F9CE4E /* IdentifierRep.cpp */,
 				1A71D57A0F33819000F9CE4E /* IdentifierRep.h */,
-				59B5977111086556007159E8 /* jsc */,
 				1A569CEF0D7E2B82007C3983 /* runtime_array.cpp */,
 				1A569CF00D7E2B82007C3983 /* runtime_array.h */,
 				1A569CF10D7E2B82007C3983 /* runtime_method.cpp */,
@@ -30208,6 +30210,7 @@
 				4A9CC81F16BF9BB400EC645A /* InspectorCSSOMWrappers.h */,
 				E45BA6AB2374927B004DFC07 /* MatchedDeclarationsCache.cpp */,
 				E45BA6A82374926B004DFC07 /* MatchedDeclarationsCache.h */,
+				E44A0254273AE0BF00993A56 /* MatchResult.h */,
 				FBDB619C16D6034600BB3394 /* PageRuleCollector.cpp */,
 				FBDB61A016D6037E00BB3394 /* PageRuleCollector.h */,
 				7128CB3E271EE58900A13DA6 /* PropertyAllowlist.cpp */,
@@ -34951,6 +34954,7 @@
 				9728C3141268E4390041E89B /* MarkupAccumulator.h in Headers */,
 				00C60E3F13D76D7E0092A275 /* MarkupTokenizerInlines.h in Headers */,
 				E45BA6AA2374926C004DFC07 /* MatchedDeclarationsCache.h in Headers */,
+				E44A0256273AE0C000993A56 /* MatchResult.h in Headers */,
 				FABE72F51059C1EB00D888CC /* MathMLAnnotationElement.h in Headers */,
 				FABE72F51059C1EB00D999DD /* MathMLElement.h in Headers */,
 				44A28AAC12DFB8AC00AE923B /* MathMLElementFactory.h in Headers */,

Modified: trunk/Source/WebCore/style/ElementRuleCollector.h (285573 => 285574)


--- trunk/Source/WebCore/style/ElementRuleCollector.h	2021-11-10 14:31:47 UTC (rev 285573)
+++ trunk/Source/WebCore/style/ElementRuleCollector.h	2021-11-10 14:40:24 UTC (rev 285574)
@@ -21,6 +21,7 @@
 
 #pragma once
 
+#include "MatchResult.h"
 #include "MediaQueryEvaluator.h"
 #include "PropertyAllowlist.h"
 #include "RuleSet.h"
@@ -66,31 +67,6 @@
     unsigned cascadeLayerPriority;
 };
 
-struct MatchedProperties {
-    RefPtr<const StyleProperties> properties;
-    uint16_t linkMatchType { SelectorChecker::MatchAll };
-    PropertyAllowlist allowlistType { PropertyAllowlist::None };
-    ScopeOrdinal styleScopeOrdinal { ScopeOrdinal::Element };
-};
-
-struct MatchResult {
-    bool isCacheable { true };
-    Vector<MatchedProperties> userAgentDeclarations;
-    Vector<MatchedProperties> userDeclarations;
-    Vector<MatchedProperties> authorDeclarations;
-
-    bool operator==(const MatchResult& other) const
-    {
-        return isCacheable == other.isCacheable
-            && userAgentDeclarations == other.userAgentDeclarations
-            && userDeclarations == other.userDeclarations
-            && authorDeclarations == other.authorDeclarations;
-    }
-    bool operator!=(const MatchResult& other) const { return !(*this == other); }
-
-    bool isEmpty() const { return userAgentDeclarations.isEmpty() && userDeclarations.isEmpty() && authorDeclarations.isEmpty(); }
-};
-
 class ElementRuleCollector {
 public:
     ElementRuleCollector(const Element&, const ScopeRuleSets&, const SelectorFilter*);
@@ -174,15 +150,5 @@
     PseudoIdSet m_matchedPseudoElementIds;
 };
 
-inline bool operator==(const MatchedProperties& a, const MatchedProperties& b)
-{
-    return a.properties == b.properties && a.linkMatchType == b.linkMatchType;
-}
-
-inline bool operator!=(const MatchedProperties& a, const MatchedProperties& b)
-{
-    return !(a == b);
-}
-
 } // namespace Style
 } // namespace WebCore

Added: trunk/Source/WebCore/style/MatchResult.h (0 => 285574)


--- trunk/Source/WebCore/style/MatchResult.h	                        (rev 0)
+++ trunk/Source/WebCore/style/MatchResult.h	2021-11-10 14:40:24 UTC (rev 285574)
@@ -0,0 +1,87 @@
+/*
+ * Copyright (C) 2021 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1.  Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ * 2.  Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimer in the
+ *     documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#pragma once
+
+#include "PropertyAllowlist.h"
+#include "SelectorChecker.h"
+#include "StyleProperties.h"
+#include "StyleScopeOrdinal.h"
+#include <wtf/Hasher.h>
+
+namespace WebCore::Style {
+
+struct MatchedProperties {
+    RefPtr<const StyleProperties> properties;
+    uint16_t linkMatchType { SelectorChecker::MatchAll };
+    PropertyAllowlist allowlistType { PropertyAllowlist::None };
+    ScopeOrdinal styleScopeOrdinal { ScopeOrdinal::Element };
+};
+
+struct MatchResult {
+    bool isCacheable { true };
+    Vector<MatchedProperties> userAgentDeclarations;
+    Vector<MatchedProperties> userDeclarations;
+    Vector<MatchedProperties> authorDeclarations;
+
+    bool isEmpty() const { return userAgentDeclarations.isEmpty() && userDeclarations.isEmpty() && authorDeclarations.isEmpty(); }
+};
+
+inline bool operator==(const MatchResult& a, const MatchResult& b)
+{
+    return a.isCacheable == b.isCacheable
+        && a.userAgentDeclarations == b.userAgentDeclarations
+        && a.userDeclarations == b.userDeclarations
+        && a.authorDeclarations == b.authorDeclarations;
+}
+
+inline bool operator!=(const MatchResult& a, const MatchResult& b)
+{
+    return !(a == b);
+}
+
+inline bool operator==(const MatchedProperties& a, const MatchedProperties& b)
+{
+    return a.properties == b.properties
+        && a.linkMatchType == b.linkMatchType
+        && a.allowlistType == b.allowlistType
+        && a.styleScopeOrdinal == b.styleScopeOrdinal;
+}
+
+inline bool operator!=(const MatchedProperties& a, const MatchedProperties& b)
+{
+    return !(a == b);
+}
+
+inline void add(Hasher& hasher, const MatchedProperties& matchedProperties)
+{
+    add(hasher, matchedProperties.properties.get(), matchedProperties.linkMatchType, matchedProperties.allowlistType, matchedProperties.styleScopeOrdinal);
+}
+
+inline void add(Hasher& hasher, const MatchResult& matchResult)
+{
+    add(hasher, matchResult.userAgentDeclarations, matchResult.userDeclarations, matchResult.authorDeclarations);
+}
+
+}

Modified: trunk/Source/WebCore/style/MatchedDeclarationsCache.cpp (285573 => 285574)


--- trunk/Source/WebCore/style/MatchedDeclarationsCache.cpp	2021-11-10 14:31:47 UTC (rev 285573)
+++ trunk/Source/WebCore/style/MatchedDeclarationsCache.cpp	2021-11-10 14:40:24 UTC (rev 285574)
@@ -88,9 +88,7 @@
     if (!matchResult.isCacheable)
         return 0;
 
-    return StringHasher::hashMemory(matchResult.userAgentDeclarations.data(), sizeof(MatchedProperties) * matchResult.userAgentDeclarations.size())
-        ^ StringHasher::hashMemory(matchResult.userDeclarations.data(), sizeof(MatchedProperties) * matchResult.userDeclarations.size())
-        ^ StringHasher::hashMemory(matchResult.authorDeclarations.data(), sizeof(MatchedProperties) * matchResult.authorDeclarations.size());
+    return WTF::computeHash(matchResult);
 }
 
 const MatchedDeclarationsCache::Entry* MatchedDeclarationsCache::find(unsigned hash, const MatchResult& matchResult)

Modified: trunk/Source/WebCore/style/MatchedDeclarationsCache.h (285573 => 285574)


--- trunk/Source/WebCore/style/MatchedDeclarationsCache.h	2021-11-10 14:31:47 UTC (rev 285573)
+++ trunk/Source/WebCore/style/MatchedDeclarationsCache.h	2021-11-10 14:40:24 UTC (rev 285574)
@@ -25,7 +25,7 @@
 
 #pragma once
 
-#include "ElementRuleCollector.h"
+#include "MatchResult.h"
 #include "RenderStyle.h"
 #include "Timer.h"
 

Modified: trunk/Source/WebCore/style/PageRuleCollector.h (285573 => 285574)


--- trunk/Source/WebCore/style/PageRuleCollector.h	2021-11-10 14:31:47 UTC (rev 285573)
+++ trunk/Source/WebCore/style/PageRuleCollector.h	2021-11-10 14:40:24 UTC (rev 285574)
@@ -21,7 +21,7 @@
 
 #pragma once
 
-#include "ElementRuleCollector.h"
+#include "MatchResult.h"
 #include "StyleScopeRuleSets.h"
 #include <wtf/Forward.h>
 

Modified: trunk/Source/WebCore/style/PropertyCascade.h (285573 => 285574)


--- trunk/Source/WebCore/style/PropertyCascade.h	2021-11-10 14:31:47 UTC (rev 285573)
+++ trunk/Source/WebCore/style/PropertyCascade.h	2021-11-10 14:40:24 UTC (rev 285574)
@@ -26,7 +26,7 @@
 #pragma once
 
 #include "CascadeLevel.h"
-#include "ElementRuleCollector.h"
+#include "MatchResult.h"
 #include "StyleBuilderState.h"
 #include <bitset>
 
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to