Title: [276303] trunk/Source
Revision
276303
Author
da...@apple.com
Date
2021-04-20 07:48:06 -0700 (Tue, 20 Apr 2021)

Log Message

Refactor sorted array mapping machinery in LocaleToScriptMapping.cpp for reuse elsewhere
https://bugs.webkit.org/show_bug.cgi?id=224733

Reviewed by Yusuke Suzuki.

Source/WebCore:

* Modules/indexeddb/client/IDBConnectionToServer.h: Removed unneeded includes.
* Modules/mediastream/RTCRtpSFrameTransformer.h: Ditto.
* Modules/mediastream/libwebrtc/LibWebRTCPeerConnectionBackend.h: Ditto.
* Modules/webauthn/AuthenticatorCoordinatorClient.h: Ditto.
* Modules/websockets/WebSocketChannel.cpp: Ditto.
* Modules/websockets/WebSocketDeflater.cpp: Ditto.
* bindings/IDLTypes.h: Ditto.
* bridge/jsc/BridgeJSC.h: Ditto.
* contentextensions/DFANode.h: Ditto.
* contentextensions/NFAToDFA.cpp: Ditto.
* contentextensions/Term.h: Ditto.
* css/typedom/StylePropertyMap.h: Ditto.
* css/typedom/StylePropertyMapReadOnly.cpp: Ditto.
* cssjit/SelectorCompiler.cpp: Ditto.

* platform/text/LocaleToScriptMapping.cpp:
(WebCore::scriptNameToCode): Refactor to use SortedArrayMap. Also changed to take StringView.
(WebCore::localeToScriptCodeForFontSelection): Ditto. Cut down on memory allocation by using
StringView instead of String while looping through substrings.

* platform/text/LocaleToScriptMapping.h: Changed scriptNameToCode to take StringView.

Source/WebKit:

* NetworkProcess/PrivateClickMeasurementManager.h: Removed unused include of HashMap.h.
* NetworkProcess/cache/NetworkCacheSubresourcesEntry.h: Ditto.

* WebProcess/WebPage/mac/WebPageMac.mm:
(WebKit::createSelectorExceptionMap): Deleted.
(WebKit::commandNameForSelectorName): Use SortedArrayMap instead of HashMap.

Source/WTF:

* WTF.xcodeproj/project.pbxproj: Added SortedArrayMap.h.
* wtf/CMakeLists.txt: Ditto.

* wtf/SortedArrayMap.h: Added. Builds on the idiom in LocalToScriptMapping, and to be
generic uses std::pair instead of custom structures. Includes the ComparableASCIILiteral
structure for maps that are keyed by case-sensitive ASCII strings.

* wtf/StdLibExtras.h:
(WTF::binarySearchImpl): Use auto in one place to make this a bit more generic.

Modified Paths

Added Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (276302 => 276303)


--- trunk/Source/WTF/ChangeLog	2021-04-20 14:07:14 UTC (rev 276302)
+++ trunk/Source/WTF/ChangeLog	2021-04-20 14:48:06 UTC (rev 276303)
@@ -1,3 +1,20 @@
+2021-04-19  Darin Adler  <da...@apple.com>
+
+        Refactor sorted array mapping machinery in LocaleToScriptMapping.cpp for reuse elsewhere
+        https://bugs.webkit.org/show_bug.cgi?id=224733
+
+        Reviewed by Yusuke Suzuki.
+
+        * WTF.xcodeproj/project.pbxproj: Added SortedArrayMap.h.
+        * wtf/CMakeLists.txt: Ditto.
+
+        * wtf/SortedArrayMap.h: Added. Builds on the idiom in LocalToScriptMapping, and to be
+        generic uses std::pair instead of custom structures. Includes the ComparableASCIILiteral
+        structure for maps that are keyed by case-sensitive ASCII strings.
+
+        * wtf/StdLibExtras.h:
+        (WTF::binarySearchImpl): Use auto in one place to make this a bit more generic.
+
 2021-04-19  Kimmo Kinnunen  <kkinnu...@apple.com>
 
         Enable -Wthread-safety, add attributes to custom lock classes, and provide macros to declare guards

Modified: trunk/Source/WTF/WTF.xcodeproj/project.pbxproj (276302 => 276303)


--- trunk/Source/WTF/WTF.xcodeproj/project.pbxproj	2021-04-20 14:07:14 UTC (rev 276302)
+++ trunk/Source/WTF/WTF.xcodeproj/project.pbxproj	2021-04-20 14:48:06 UTC (rev 276303)
@@ -489,6 +489,7 @@
 		83F2BADE1CF9524E003E99C3 /* Function.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Function.h; sourceTree = "<group>"; };
 		83FBA93119DF459700F30ADB /* TypeCasts.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TypeCasts.h; sourceTree = "<group>"; };
 		86F46F5F1A2840EE00CCBF22 /* RefCounter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RefCounter.h; sourceTree = "<group>"; };
+		93156C8D262C982200EAE27B /* SortedArrayMap.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SortedArrayMap.h; sourceTree = "<group>"; };
 		93241657243BC2E50032FAAE /* VectorCocoa.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = VectorCocoa.h; sourceTree = "<group>"; };
 		933D63191FCB6AB90032ECD6 /* StringHasher.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = StringHasher.h; sourceTree = "<group>"; };
 		9384B73E20DEA284005B73B2 /* CFXPCBridgeSPI.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CFXPCBridgeSPI.h; sourceTree = "<group>"; };
@@ -1247,6 +1248,7 @@
 				0FA6F39220CC73A200A03DCD /* SmallPtrSet.cpp */,
 				7936D6A91C99F8AE000D1AED /* SmallPtrSet.h */,
 				A30D412D1F0DE13F00B71954 /* SoftLinking.h */,
+				93156C8D262C982200EAE27B /* SortedArrayMap.h */,
 				79038E05224B05A7004C0738 /* SpanningTree.h */,
 				A8A4730D151A825B004123FF /* Spectrum.h */,
 				A8A4730E151A825B004123FF /* StackBounds.cpp */,

Modified: trunk/Source/WTF/wtf/CMakeLists.txt (276302 => 276303)


--- trunk/Source/WTF/wtf/CMakeLists.txt	2021-04-20 14:07:14 UTC (rev 276302)
+++ trunk/Source/WTF/wtf/CMakeLists.txt	2021-04-20 14:48:06 UTC (rev 276303)
@@ -237,6 +237,7 @@
     SixCharacterHash.h
     SmallPtrSet.h
     SoftLinking.h
+    SortedArrayMap.h
     SpanningTree.h
     Spectrum.h
     StackBounds.h

Added: trunk/Source/WTF/wtf/SortedArrayMap.h (0 => 276303)


--- trunk/Source/WTF/wtf/SortedArrayMap.h	                        (rev 0)
+++ trunk/Source/WTF/wtf/SortedArrayMap.h	2021-04-20 14:48:06 UTC (rev 276303)
@@ -0,0 +1,157 @@
+/*
+
+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 <wtf/text/StringView.h>
+
+namespace WTF {
+
+// SortedArrayMap is a map like HashMap, but it's read-only. It uses much less memory than HashMap.
+// It uses binary search instead of hashing, so can be outperformed by HashMap for large maps.
+// The array passed to the constructor has std::pair elements: keys first and values second.
+// The array and the SortedArrayMap should typically both be global constant expressions.
+
+template<typename ArrayType> class SortedArrayMap {
+public:
+    using ElementType = typename std::remove_extent_t<ArrayType>;
+    using ValueType = typename ElementType::second_type;
+
+    constexpr SortedArrayMap(const ArrayType&);
+
+    // FIXME: To match HashMap interface better, would be nice to get the default value from traits.
+    template<typename KeyArgument> ValueType get(const KeyArgument&, const ValueType& defaultValue = { }) const;
+
+    // FIXME: Should add a function like this to HashMap so the two kinds of maps are more interchangable.
+    template<typename KeyArgument> const ValueType* tryGet(const KeyArgument&) const;
+
+private:
+    const ArrayType& m_array;
+};
+
+struct ComparableStringView {
+    StringView string;
+};
+
+struct ComparableASCIILiteral {
+    const char* literal;
+    template<std::size_t size> constexpr ComparableASCIILiteral(const char (&characters)[size]) : literal { characters } { }
+    static Optional<ComparableStringView> parse(StringView string) { return { { string } }; }
+};
+
+constexpr bool operator==(ComparableASCIILiteral, ComparableASCIILiteral);
+constexpr bool operator<(ComparableASCIILiteral, ComparableASCIILiteral);
+
+bool operator==(ComparableStringView, ComparableASCIILiteral);
+bool operator<(ComparableStringView, ComparableASCIILiteral);
+bool operator!=(ComparableStringView, ComparableASCIILiteral);
+bool operator==(ComparableASCIILiteral, ComparableStringView);
+
+// FIXME: Use std::is_sorted instead of this and remove it, once we require C++20.
+template<typename Iterator, typename Predicate> constexpr bool isSortedConstExpr(Iterator begin, Iterator end, Predicate predicate)
+{
+    if (begin == end)
+        return true;
+    auto current = begin;
+    auto previous = current;
+    while (++current != end) {
+        if (!predicate(*previous, *current))
+            return false;
+        previous = current;
+    }
+    return true;
+}
+
+template<typename ArrayType> constexpr SortedArrayMap<ArrayType>::SortedArrayMap(const ArrayType& array)
+    : m_array { array }
+{
+    ASSERT_UNDER_CONSTEXPR_CONTEXT(isSortedConstExpr(std::begin(array), std::end(array), [] (auto& a, auto b) {
+        return a.first < b.first;
+    }));
+}
+
+template<typename ArrayType> template<typename KeyArgument> inline auto SortedArrayMap<ArrayType>::tryGet(const KeyArgument& key) const -> const ValueType*
+{
+    auto parsedKey = ElementType::first_type::parse(key);
+    if (!parsedKey)
+        return nullptr;
+    // FIXME: We should enhance tryBinarySearch so it can deduce ElementType.
+    // FIXME: If the array's size is small enough, should do linear search since it is more efficient than binary search.
+    auto element = tryBinarySearch<ElementType>(m_array, std::size(m_array), *parsedKey, [] (auto* element) {
+        return element->first;
+    });
+    return element ? &element->second : nullptr;
+}
+
+template<typename ArrayType> template<typename KeyArgument> inline auto SortedArrayMap<ArrayType>::get(const KeyArgument& key, const ValueType& defaultValue) const -> ValueType
+{
+    auto result = tryGet(key);
+    return result ? *result : defaultValue;
+}
+
+constexpr int strcmpConstExpr(const char* a, const char* b)
+{
+    while (*a == *b && *a && *b) {
+        ++a;
+        ++b;
+    }
+    return *a == *b ? 0 : *a < *b ? -1 : 1;
+}
+
+constexpr bool operator==(ComparableASCIILiteral a, ComparableASCIILiteral b)
+{
+    return !strcmpConstExpr(a.literal, b.literal);
+}
+
+constexpr bool operator<(ComparableASCIILiteral a, ComparableASCIILiteral b)
+{
+    return strcmpConstExpr(a.literal, b.literal) < 0;
+}
+
+inline bool operator==(ComparableStringView a, ComparableASCIILiteral b)
+{
+    return a.string == b.literal;
+}
+
+inline bool operator<(ComparableStringView a, ComparableASCIILiteral b)
+{
+    return codePointCompare(a.string, b.literal) < 0;
+}
+
+inline bool operator!=(ComparableStringView a, ComparableASCIILiteral b)
+{
+    return !(a == b);
+}
+
+inline bool operator==(ComparableASCIILiteral a, ComparableStringView b)
+{
+    return b == a;
+}
+
+}
+
+using WTF::ComparableASCIILiteral;
+using WTF::SortedArrayMap;

Modified: trunk/Source/WTF/wtf/StdLibExtras.h (276302 => 276303)


--- trunk/Source/WTF/wtf/StdLibExtras.h	2021-04-20 14:07:14 UTC (rev 276302)
+++ trunk/Source/WTF/wtf/StdLibExtras.h	2021-04-20 14:48:06 UTC (rev 276303)
@@ -206,7 +206,7 @@
     size_t offset = 0;
     while (size > 1) {
         size_t pos = (size - 1) >> 1;
-        KeyType val = extractKey(&array[offset + pos]);
+        auto val = extractKey(&array[offset + pos]);
         
         if (val == key)
             return &array[offset + pos];

Modified: trunk/Source/WebCore/ChangeLog (276302 => 276303)


--- trunk/Source/WebCore/ChangeLog	2021-04-20 14:07:14 UTC (rev 276302)
+++ trunk/Source/WebCore/ChangeLog	2021-04-20 14:48:06 UTC (rev 276303)
@@ -1,3 +1,32 @@
+2021-04-19  Darin Adler  <da...@apple.com>
+
+        Refactor sorted array mapping machinery in LocaleToScriptMapping.cpp for reuse elsewhere
+        https://bugs.webkit.org/show_bug.cgi?id=224733
+
+        Reviewed by Yusuke Suzuki.
+
+        * Modules/indexeddb/client/IDBConnectionToServer.h: Removed unneeded includes.
+        * Modules/mediastream/RTCRtpSFrameTransformer.h: Ditto.
+        * Modules/mediastream/libwebrtc/LibWebRTCPeerConnectionBackend.h: Ditto.
+        * Modules/webauthn/AuthenticatorCoordinatorClient.h: Ditto.
+        * Modules/websockets/WebSocketChannel.cpp: Ditto.
+        * Modules/websockets/WebSocketDeflater.cpp: Ditto.
+        * bindings/IDLTypes.h: Ditto.
+        * bridge/jsc/BridgeJSC.h: Ditto.
+        * contentextensions/DFANode.h: Ditto.
+        * contentextensions/NFAToDFA.cpp: Ditto.
+        * contentextensions/Term.h: Ditto.
+        * css/typedom/StylePropertyMap.h: Ditto.
+        * css/typedom/StylePropertyMapReadOnly.cpp: Ditto.
+        * cssjit/SelectorCompiler.cpp: Ditto.
+
+        * platform/text/LocaleToScriptMapping.cpp:
+        (WebCore::scriptNameToCode): Refactor to use SortedArrayMap. Also changed to take StringView.
+        (WebCore::localeToScriptCodeForFontSelection): Ditto. Cut down on memory allocation by using
+        StringView instead of String while looping through substrings.
+
+        * platform/text/LocaleToScriptMapping.h: Changed scriptNameToCode to take StringView.
+
 2021-04-20  Zalan Bujtas  <za...@apple.com>
 
         [LFC] Implement Box::isSizeContainmentBox()

Modified: trunk/Source/WebCore/Modules/indexeddb/client/IDBConnectionToServer.h (276302 => 276303)


--- trunk/Source/WebCore/Modules/indexeddb/client/IDBConnectionToServer.h	2021-04-20 14:07:14 UTC (rev 276302)
+++ trunk/Source/WebCore/Modules/indexeddb/client/IDBConnectionToServer.h	2021-04-20 14:48:06 UTC (rev 276303)
@@ -29,7 +29,6 @@
 #include "IDBConnectionToServerDelegate.h"
 #include "IDBResourceIdentifier.h"
 #include <wtf/Function.h>
-#include <wtf/HashMap.h>
 #include <wtf/Ref.h>
 #include <wtf/ThreadSafeRefCounted.h>
 

Modified: trunk/Source/WebCore/Modules/mediastream/RTCRtpSFrameTransformer.h (276302 => 276303)


--- trunk/Source/WebCore/Modules/mediastream/RTCRtpSFrameTransformer.h	2021-04-20 14:07:14 UTC (rev 276302)
+++ trunk/Source/WebCore/Modules/mediastream/RTCRtpSFrameTransformer.h	2021-04-20 14:48:06 UTC (rev 276303)
@@ -29,7 +29,6 @@
 
 #include "ExceptionOr.h"
 #include "RTCRtpTransformBackend.h"
-#include <wtf/HashMap.h>
 #include <wtf/Lock.h>
 #include <wtf/ThreadSafeRefCounted.h>
 

Modified: trunk/Source/WebCore/Modules/mediastream/libwebrtc/LibWebRTCPeerConnectionBackend.h (276302 => 276303)


--- trunk/Source/WebCore/Modules/mediastream/libwebrtc/LibWebRTCPeerConnectionBackend.h	2021-04-20 14:07:14 UTC (rev 276302)
+++ trunk/Source/WebCore/Modules/mediastream/libwebrtc/LibWebRTCPeerConnectionBackend.h	2021-04-20 14:48:06 UTC (rev 276303)
@@ -28,7 +28,6 @@
 
 #include "PeerConnectionBackend.h"
 #include "RealtimeMediaSource.h"
-#include <wtf/HashMap.h>
 
 namespace webrtc {
 class IceCandidateInterface;

Modified: trunk/Source/WebCore/Modules/webauthn/AuthenticatorCoordinatorClient.h (276302 => 276303)


--- trunk/Source/WebCore/Modules/webauthn/AuthenticatorCoordinatorClient.h	2021-04-20 14:07:14 UTC (rev 276302)
+++ trunk/Source/WebCore/Modules/webauthn/AuthenticatorCoordinatorClient.h	2021-04-20 14:48:06 UTC (rev 276303)
@@ -29,7 +29,6 @@
 
 #include "ExceptionData.h"
 #include <wtf/CompletionHandler.h>
-#include <wtf/HashMap.h>
 #include <wtf/WeakPtr.h>
 
 namespace WebCore {

Modified: trunk/Source/WebCore/Modules/websockets/WebSocketChannel.cpp (276302 => 276303)


--- trunk/Source/WebCore/Modules/websockets/WebSocketChannel.cpp	2021-04-20 14:07:14 UTC (rev 276302)
+++ trunk/Source/WebCore/Modules/websockets/WebSocketChannel.cpp	2021-04-20 14:48:06 UTC (rev 276303)
@@ -55,9 +55,7 @@
 #include "WebSocketHandshake.h"
 #include <_javascript_Core/ArrayBuffer.h>
 #include <wtf/FastMalloc.h>
-#include <wtf/HashMap.h>
 #include <wtf/text/CString.h>
-#include <wtf/text/StringHash.h>
 
 namespace WebCore {
 

Modified: trunk/Source/WebCore/Modules/websockets/WebSocketDeflater.cpp (276302 => 276303)


--- trunk/Source/WebCore/Modules/websockets/WebSocketDeflater.cpp	2021-04-20 14:07:14 UTC (rev 276302)
+++ trunk/Source/WebCore/Modules/websockets/WebSocketDeflater.cpp	2021-04-20 14:48:06 UTC (rev 276303)
@@ -33,10 +33,7 @@
 
 #include "Logging.h"
 #include <wtf/FastMalloc.h>
-#include <wtf/HashMap.h>
 #include <wtf/StdLibExtras.h>
-#include <wtf/text/StringHash.h>
-#include <wtf/text/WTFString.h>
 #include <zlib.h>
 
 namespace WebCore {

Modified: trunk/Source/WebCore/bindings/IDLTypes.h (276302 => 276303)


--- trunk/Source/WebCore/bindings/IDLTypes.h	2021-04-20 14:07:14 UTC (rev 276302)
+++ trunk/Source/WebCore/bindings/IDLTypes.h	2021-04-20 14:48:06 UTC (rev 276303)
@@ -29,7 +29,6 @@
 #include <_javascript_Core/HandleTypes.h>
 #include <_javascript_Core/Strong.h>
 #include <wtf/Brigand.h>
-#include <wtf/HashMap.h>
 #include <wtf/StdLibExtras.h>
 #include <wtf/URL.h>
 

Modified: trunk/Source/WebCore/bridge/jsc/BridgeJSC.h (276302 => 276303)


--- trunk/Source/WebCore/bridge/jsc/BridgeJSC.h	2021-04-20 14:07:14 UTC (rev 276302)
+++ trunk/Source/WebCore/bridge/jsc/BridgeJSC.h	2021-04-20 14:48:06 UTC (rev 276303)
@@ -24,12 +24,10 @@
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-#ifndef BridgeJSC_h
-#define BridgeJSC_h
+#pragma once
 
 #include "Bridge.h"
 #include <_javascript_Core/JSString.h>
-#include <wtf/HashMap.h>
 #include <wtf/RefCounted.h>
 #include <wtf/Vector.h>
 
@@ -137,5 +135,3 @@
 } // namespace Bindings
 
 } // namespace JSC
-
-#endif

Modified: trunk/Source/WebCore/contentextensions/DFANode.h (276302 => 276303)


--- trunk/Source/WebCore/contentextensions/DFANode.h	2021-04-20 14:07:14 UTC (rev 276302)
+++ trunk/Source/WebCore/contentextensions/DFANode.h	2021-04-20 14:48:06 UTC (rev 276303)
@@ -28,7 +28,6 @@
 #if ENABLE(CONTENT_EXTENSIONS)
 
 #include "ContentExtensionsDebugging.h"
-#include <wtf/HashMap.h>
 #include <wtf/Vector.h>
 
 namespace WebCore {

Modified: trunk/Source/WebCore/contentextensions/NFAToDFA.cpp (276302 => 276303)


--- trunk/Source/WebCore/contentextensions/NFAToDFA.cpp	2021-04-20 14:07:14 UTC (rev 276302)
+++ trunk/Source/WebCore/contentextensions/NFAToDFA.cpp	2021-04-20 14:48:06 UTC (rev 276303)
@@ -35,7 +35,6 @@
 #include "NFA.h"
 #include "SerializedNFA.h"
 #include <wtf/DataLog.h>
-#include <wtf/HashMap.h>
 #include <wtf/HashSet.h>
 
 namespace WebCore {

Modified: trunk/Source/WebCore/contentextensions/Term.h (276302 => 276303)


--- trunk/Source/WebCore/contentextensions/Term.h	2021-04-20 14:07:14 UTC (rev 276302)
+++ trunk/Source/WebCore/contentextensions/Term.h	2021-04-20 14:48:06 UTC (rev 276303)
@@ -31,11 +31,9 @@
 #include "NFA.h"
 #include <unicode/utypes.h>
 #include <wtf/ASCIICType.h>
-#include <wtf/HashMap.h>
 #include <wtf/Hasher.h>
 #include <wtf/Vector.h>
 #include <wtf/text/StringBuilder.h>
-#include <wtf/text/WTFString.h>
 
 namespace WebCore {
 

Modified: trunk/Source/WebCore/css/typedom/StylePropertyMap.h (276302 => 276303)


--- trunk/Source/WebCore/css/typedom/StylePropertyMap.h	2021-04-20 14:07:14 UTC (rev 276302)
+++ trunk/Source/WebCore/css/typedom/StylePropertyMap.h	2021-04-20 14:48:06 UTC (rev 276303)
@@ -28,9 +28,6 @@
 #if ENABLE(CSS_TYPED_OM)
 
 #include "StylePropertyMapReadOnly.h"
-#include <wtf/HashMap.h>
-#include <wtf/RefCounted.h>
-#include <wtf/text/WTFString.h>
 
 namespace WebCore {
 

Modified: trunk/Source/WebCore/css/typedom/StylePropertyMapReadOnly.cpp (276302 => 276303)


--- trunk/Source/WebCore/css/typedom/StylePropertyMapReadOnly.cpp	2021-04-20 14:07:14 UTC (rev 276302)
+++ trunk/Source/WebCore/css/typedom/StylePropertyMapReadOnly.cpp	2021-04-20 14:48:06 UTC (rev 276303)
@@ -40,11 +40,9 @@
 #include "TypedOMCSSStyleValue.h"
 #include "TypedOMCSSUnitValue.h"
 #include "TypedOMCSSUnparsedValue.h"
-#include <wtf/HashMap.h>
 
 namespace WebCore {
 
-
 RefPtr<TypedOMCSSStyleValue> StylePropertyMapReadOnly::reifyValue(CSSValue* value, Document& document, Element*)
 {
     if (!value)

Modified: trunk/Source/WebCore/cssjit/SelectorCompiler.cpp (276302 => 276303)


--- trunk/Source/WebCore/cssjit/SelectorCompiler.cpp	2021-04-20 14:07:14 UTC (rev 276302)
+++ trunk/Source/WebCore/cssjit/SelectorCompiler.cpp	2021-04-20 14:48:06 UTC (rev 276303)
@@ -56,7 +56,6 @@
 #include <_javascript_Core/VM.h>
 #include <limits>
 #include <wtf/Deque.h>
-#include <wtf/HashMap.h>
 #include <wtf/HashSet.h>
 #include <wtf/Vector.h>
 #include <wtf/text/CString.h>

Modified: trunk/Source/WebCore/platform/text/LocaleToScriptMapping.cpp (276302 => 276303)


--- trunk/Source/WebCore/platform/text/LocaleToScriptMapping.cpp	2021-04-20 14:07:14 UTC (rev 276302)
+++ trunk/Source/WebCore/platform/text/LocaleToScriptMapping.cpp	2021-04-20 14:48:06 UTC (rev 276303)
@@ -32,12 +32,13 @@
 #include "config.h"
 #include "LocaleToScriptMapping.h"
 
-#include <wtf/HashMap.h>
-#include <wtf/NeverDestroyed.h>
-#include <wtf/text/StringHash.h>
+#include <wtf/Optional.h>
+#include <wtf/SortedArrayMap.h>
+#include <wtf/text/WTFString.h>
 
 namespace WebCore {
 
+// FIXME: Consider moving alongside ComparableASCIILiteral and giving an even better name.
 template<typename StorageInteger>
 class PackedASCIILowerCodes {
 public:
@@ -112,19 +113,14 @@
     StorageInteger m_value { 0 };
 };
 
-using ScriptName = PackedASCIILowerCodes<uint32_t>;
-struct ScriptNameCode {
-    ScriptName name;
-    UScriptCode code;
-};
-
-UScriptCode scriptNameToCode(const String& scriptName)
+UScriptCode scriptNameToCode(StringView scriptName)
 {
     // This generally maps an ISO 15924 script code to its UScriptCode, but certain families of script codes are
     // treated as a single script for assigning a per-script font in Settings. For example, "hira" is mapped to
     // USCRIPT_KATAKANA_OR_HIRAGANA instead of USCRIPT_HIRAGANA, since we want all Japanese scripts to be rendered
     // using the same font setting.
-    static constexpr ScriptNameCode scriptNameCodeList[] = {
+    using ScriptName = PackedASCIILowerCodes<uint32_t>;
+    static constexpr std::pair<ScriptName, UScriptCode> scriptNameCodeList[] = {
         { "arab", USCRIPT_ARABIC },
         { "armn", USCRIPT_ARMENIAN },
         { "bali", USCRIPT_BALINESE },
@@ -232,38 +228,16 @@
         { "zyyy", USCRIPT_COMMON },
         { "zzzz", USCRIPT_UNKNOWN },
     };
-
     static_assert(ScriptName("arab").value() == 0x61726162U);
     static_assert(ScriptName("zzzz").value() == 0x7a7a7a7aU);
-
-    ASSERT(
-        std::is_sorted(std::begin(scriptNameCodeList), std::end(scriptNameCodeList),
-            [](const ScriptNameCode& a, const ScriptNameCode& b) {
-                return a.name < b.name;
-            }));
-
-    auto name = ScriptName::parse(scriptName);
-    if (!name)
-        return USCRIPT_INVALID_CODE;
-
-    auto* element = tryBinarySearch<ScriptNameCode>(scriptNameCodeList, std::size(scriptNameCodeList), name.value(),
-        [](const ScriptNameCode* scriptNameCode) {
-            return scriptNameCode->name;
-        });
-    if (element)
-        return element->code;
-    return USCRIPT_INVALID_CODE;
+    static constexpr SortedArrayMap map { scriptNameCodeList };
+    return map.get(scriptName, USCRIPT_INVALID_CODE);
 }
 
-using LocaleName = PackedASCIILowerCodes<uint64_t>;
-struct LocaleScript {
-    LocaleName locale;
-    UScriptCode script;
-};
-
 UScriptCode localeToScriptCodeForFontSelection(const String& locale)
 {
-    static constexpr LocaleScript localeScriptList[] = {
+    using LocaleName = PackedASCIILowerCodes<uint64_t>;
+    static constexpr std::pair<LocaleName, UScriptCode> localeScriptList[] = {
         { "aa", USCRIPT_LATIN },
         { "ab", USCRIPT_CYRILLIC },
         { "ady", USCRIPT_CYRILLIC },
@@ -463,35 +437,14 @@
         { "zh_tw", USCRIPT_TRADITIONAL_HAN },
         { "zu", USCRIPT_LATIN },
     };
-
     static_assert(LocaleName("aa").value() == 0x6161000000000000ULL);
     static_assert(LocaleName("zh_tw").value() == 0x7a685f7477000000ULL);
-
-    ASSERT(
-        std::is_sorted(std::begin(localeScriptList), std::end(localeScriptList),
-            [](const LocaleScript& a, const LocaleScript& b) {
-                return a.locale < b.locale;
-            }));
-
-    auto tryFindScriptCode = [&] (const String& string) -> Optional<UScriptCode> {
-        auto localeName = LocaleName::parse(string);
-        if (!localeName)
-            return WTF::nullopt;
-
-        auto* element = tryBinarySearch<LocaleScript>(localeScriptList, std::size(localeScriptList), localeName.value(),
-            [](const LocaleScript* localeScript) {
-                return localeScript->locale;
-            });
-        if (element)
-            return element->script;
-        return WTF::nullopt;
-    };
-
-    String canonicalLocale = locale;
-    canonicalLocale.replace('-', '_');
-    while (!canonicalLocale.isEmpty()) {
-        if (auto scriptCode = tryFindScriptCode(canonicalLocale))
-            return scriptCode.value();
+    static constexpr SortedArrayMap map { localeScriptList };
+    String canonicalLocaleString = locale;
+    canonicalLocaleString.replace('-', '_');
+    for (StringView canonicalLocale = canonicalLocaleString; !canonicalLocale.isEmpty(); ) {
+        if (auto scriptCode = map.tryGet(canonicalLocale))
+            return *scriptCode;
         auto underscorePosition = canonicalLocale.reverseFind('_');
         if (underscorePosition == notFound)
             break;

Modified: trunk/Source/WebCore/platform/text/LocaleToScriptMapping.h (276302 => 276303)


--- trunk/Source/WebCore/platform/text/LocaleToScriptMapping.h	2021-04-20 14:07:14 UTC (rev 276302)
+++ trunk/Source/WebCore/platform/text/LocaleToScriptMapping.h	2021-04-20 14:48:06 UTC (rev 276303)
@@ -28,8 +28,7 @@
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-#ifndef LocaleToScriptMapping_h
-#define LocaleToScriptMapping_h
+#pragma once
 
 #include <unicode/uscript.h>
 #include <wtf/Forward.h>
@@ -37,8 +36,6 @@
 namespace WebCore {
 
 UScriptCode localeToScriptCodeForFontSelection(const String&);
-WEBCORE_EXPORT UScriptCode scriptNameToCode(const String&);
+WEBCORE_EXPORT UScriptCode scriptNameToCode(StringView);
 
 }
-
-#endif

Modified: trunk/Source/WebKit/ChangeLog (276302 => 276303)


--- trunk/Source/WebKit/ChangeLog	2021-04-20 14:07:14 UTC (rev 276302)
+++ trunk/Source/WebKit/ChangeLog	2021-04-20 14:48:06 UTC (rev 276303)
@@ -1,3 +1,17 @@
+2021-04-19  Darin Adler  <da...@apple.com>
+
+        Refactor sorted array mapping machinery in LocaleToScriptMapping.cpp for reuse elsewhere
+        https://bugs.webkit.org/show_bug.cgi?id=224733
+
+        Reviewed by Yusuke Suzuki.
+
+        * NetworkProcess/PrivateClickMeasurementManager.h: Removed unused include of HashMap.h.
+        * NetworkProcess/cache/NetworkCacheSubresourcesEntry.h: Ditto.
+
+        * WebProcess/WebPage/mac/WebPageMac.mm:
+        (WebKit::createSelectorExceptionMap): Deleted.
+        (WebKit::commandNameForSelectorName): Use SortedArrayMap instead of HashMap.
+
 2021-04-20  Kimmo Kinnunen  <kkinnu...@apple.com>
 
         WebGL GPU Process implementation should use thread safety annotations

Modified: trunk/Source/WebKit/NetworkProcess/PrivateClickMeasurementManager.h (276302 => 276303)


--- trunk/Source/WebKit/NetworkProcess/PrivateClickMeasurementManager.h	2021-04-20 14:07:14 UTC (rev 276302)
+++ trunk/Source/WebKit/NetworkProcess/PrivateClickMeasurementManager.h	2021-04-20 14:48:06 UTC (rev 276303)
@@ -34,7 +34,6 @@
 #include <WebCore/Timer.h>
 #include <pal/SessionID.h>
 #include <wtf/CompletionHandler.h>
-#include <wtf/HashMap.h>
 #include <wtf/JSONValues.h>
 #include <wtf/WeakPtr.h>
 #include <wtf/text/WTFString.h>

Modified: trunk/Source/WebKit/NetworkProcess/cache/NetworkCacheSubresourcesEntry.h (276302 => 276303)


--- trunk/Source/WebKit/NetworkProcess/cache/NetworkCacheSubresourcesEntry.h	2021-04-20 14:07:14 UTC (rev 276302)
+++ trunk/Source/WebKit/NetworkProcess/cache/NetworkCacheSubresourcesEntry.h	2021-04-20 14:48:06 UTC (rev 276303)
@@ -29,7 +29,6 @@
 
 #include "NetworkCacheStorage.h"
 #include <WebCore/ResourceRequest.h>
-#include <wtf/HashMap.h>
 #include <wtf/URL.h>
 
 namespace WebKit {

Modified: trunk/Source/WebKit/WebProcess/WebPage/mac/WebPageMac.mm (276302 => 276303)


--- trunk/Source/WebKit/WebProcess/WebPage/mac/WebPageMac.mm	2021-04-20 14:07:14 UTC (rev 276302)
+++ trunk/Source/WebKit/WebProcess/WebPage/mac/WebPageMac.mm	2021-04-20 14:48:06 UTC (rev 276303)
@@ -95,6 +95,7 @@
 #import <pal/spi/cocoa/NSAccessibilitySPI.h>
 #import <pal/spi/mac/NSApplicationSPI.h>
 #import <wtf/SetForScope.h>
+#import <wtf/SortedArrayMap.h>
 
 #if ENABLE(WIRELESS_PLAYBACK_TARGET)
 #import <WebCore/MediaPlaybackTargetCocoa.h>
@@ -207,32 +208,22 @@
                 && (path.endsWithIgnoringASCIICase(".pdf") || path.endsWithIgnoringASCIICase(".ps"))));
 }
 
-typedef HashMap<String, String> SelectorNameMap;
-
-// Map selectors into Editor command names.
-// This is not needed for any selectors that have the same name as the Editor command.
-static const SelectorNameMap* createSelectorExceptionMap()
-{
-    SelectorNameMap* map = new HashMap<String, String>;
-
-    map->add("insertNewlineIgnoringFieldEditor:", "InsertNewline");
-    map->add("insertParagraphSeparator:", "InsertNewline");
-    map->add("insertTabIgnoringFieldEditor:", "InsertTab");
-    map->add("pageDown:", "MovePageDown");
-    map->add("pageDownAndModifySelection:", "MovePageDownAndModifySelection");
-    map->add("pageUp:", "MovePageUp");
-    map->add("pageUpAndModifySelection:", "MovePageUpAndModifySelection");
-
-    return map;
-}
-
 static String commandNameForSelectorName(const String& selectorName)
 {
-    // Check the exception map first.
-    static const SelectorNameMap* exceptionMap = createSelectorExceptionMap();
-    SelectorNameMap::const_iterator it = exceptionMap->find(selectorName);
-    if (it != exceptionMap->end())
-        return it->value;
+    // Map selectors into Editor command names.
+    // This is not needed for any selectors that have the same name as the Editor command.
+    static constexpr std::pair<ComparableASCIILiteral, ASCIILiteral> selectorExceptions[] = {
+        { "insertNewlineIgnoringFieldEditor:", "InsertNewline"_s },
+        { "insertParagraphSeparator:", "InsertNewline"_s },
+        { "insertTabIgnoringFieldEditor:", "InsertTab"_s },
+        { "pageDown:", "MovePageDown"_s },
+        { "pageDownAndModifySelection:", "MovePageDownAndModifySelection"_s },
+        { "pageUp:", "MovePageUp"_s },
+        { "pageUpAndModifySelection:", "MovePageUpAndModifySelection"_s },
+    };
+    static constexpr SortedArrayMap map { selectorExceptions };
+    if (auto commandName = map.tryGet(selectorName))
+        return *commandName;
 
     // Remove the trailing colon.
     // No need to capitalize the command name since Editor command names are not case sensitive.
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to