Title: [248001] branches/safari-608-branch/Source/WebKit
Revision
248001
Author
alanc...@apple.com
Date
2019-07-29 20:56:54 -0700 (Mon, 29 Jul 2019)

Log Message

Cherry-pick r247672. rdar://problem/53449739

    Micro-optimize HashMap & String IPC decoding
    https://bugs.webkit.org/show_bug.cgi?id=199967

    Reviewed by Geoffrey Garen.

    The legacy HashMap decoder (returning a boolean) was failing to WTFMove()
    the key & value when calling HashMap::add(). The modern decoder (returning
    an Optional) was properly using WTFMove(). Rewrite the legacy HashMap decoder
    to call the modern one to reduce code duplication and to get this optimization.

    Also, encode HashMap::size() as a uint32_t instead of a uint64_t since
    HashMap::size() returns an 'unsigned int' type. Finally, update the modern
    decoder to WTFMove(hashMap) when returning. Because the function returns an
    Optional<HashMap> and not a HashMap, I do not believe we get return value
    optimization (RVO).

    Do similar changes to String IPC coders.

    * Platform/IPC/ArgumentCoders.cpp:
    (IPC::decodeStringText):
    (IPC::ArgumentCoder<String>::decode):
    * Platform/IPC/ArgumentCoders.h:

    git-svn-id: https://svn.webkit.org/repository/webkit/trunk@247672 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Modified Paths

Diff

Modified: branches/safari-608-branch/Source/WebKit/ChangeLog (248000 => 248001)


--- branches/safari-608-branch/Source/WebKit/ChangeLog	2019-07-30 03:56:52 UTC (rev 248000)
+++ branches/safari-608-branch/Source/WebKit/ChangeLog	2019-07-30 03:56:54 UTC (rev 248001)
@@ -1,5 +1,60 @@
 2019-07-29  Alan Coon  <alanc...@apple.com>
 
+        Cherry-pick r247672. rdar://problem/53449739
+
+    Micro-optimize HashMap & String IPC decoding
+    https://bugs.webkit.org/show_bug.cgi?id=199967
+    
+    Reviewed by Geoffrey Garen.
+    
+    The legacy HashMap decoder (returning a boolean) was failing to WTFMove()
+    the key & value when calling HashMap::add(). The modern decoder (returning
+    an Optional) was properly using WTFMove(). Rewrite the legacy HashMap decoder
+    to call the modern one to reduce code duplication and to get this optimization.
+    
+    Also, encode HashMap::size() as a uint32_t instead of a uint64_t since
+    HashMap::size() returns an 'unsigned int' type. Finally, update the modern
+    decoder to WTFMove(hashMap) when returning. Because the function returns an
+    Optional<HashMap> and not a HashMap, I do not believe we get return value
+    optimization (RVO).
+    
+    Do similar changes to String IPC coders.
+    
+    * Platform/IPC/ArgumentCoders.cpp:
+    (IPC::decodeStringText):
+    (IPC::ArgumentCoder<String>::decode):
+    * Platform/IPC/ArgumentCoders.h:
+    
+    
+    git-svn-id: https://svn.webkit.org/repository/webkit/trunk@247672 268f45cc-cd09-0410-ab3c-d52691b4dbfc
+
+    2019-07-20  Chris Dumez  <cdu...@apple.com>
+
+            Micro-optimize HashMap & String IPC decoding
+            https://bugs.webkit.org/show_bug.cgi?id=199967
+
+            Reviewed by Geoffrey Garen.
+
+            The legacy HashMap decoder (returning a boolean) was failing to WTFMove()
+            the key & value when calling HashMap::add(). The modern decoder (returning
+            an Optional) was properly using WTFMove(). Rewrite the legacy HashMap decoder
+            to call the modern one to reduce code duplication and to get this optimization.
+
+            Also, encode HashMap::size() as a uint32_t instead of a uint64_t since
+            HashMap::size() returns an 'unsigned int' type. Finally, update the modern
+            decoder to WTFMove(hashMap) when returning. Because the function returns an
+            Optional<HashMap> and not a HashMap, I do not believe we get return value
+            optimization (RVO).
+
+            Do similar changes to String IPC coders.
+
+            * Platform/IPC/ArgumentCoders.cpp:
+            (IPC::decodeStringText):
+            (IPC::ArgumentCoder<String>::decode):
+            * Platform/IPC/ArgumentCoders.h:
+
+2019-07-29  Alan Coon  <alanc...@apple.com>
+
         Cherry-pick r247890. rdar://problem/53647283
 
     Allow more syscalls in the WebContent process' sandbox profile

Modified: branches/safari-608-branch/Source/WebKit/Platform/IPC/ArgumentCoders.cpp (248000 => 248001)


--- branches/safari-608-branch/Source/WebKit/Platform/IPC/ArgumentCoders.cpp	2019-07-30 03:56:52 UTC (rev 248000)
+++ branches/safari-608-branch/Source/WebKit/Platform/IPC/ArgumentCoders.cpp	2019-07-30 03:56:54 UTC (rev 248001)
@@ -132,44 +132,22 @@
 }
 
 template <typename CharacterType>
-static inline bool decodeStringText(Decoder& decoder, uint32_t length, String& result)
+static inline Optional<String> decodeStringText(Decoder& decoder, uint32_t length)
 {
     // Before allocating the string, make sure that the decoder buffer is big enough.
     if (!decoder.bufferIsLargeEnoughToContain<CharacterType>(length)) {
         decoder.markInvalid();
-        return false;
+        return WTF::nullopt;
     }
     
     CharacterType* buffer;
     String string = String::createUninitialized(length, buffer);
     if (!decoder.decodeFixedLengthData(reinterpret_cast<uint8_t*>(buffer), length * sizeof(CharacterType), alignof(CharacterType)))
-        return false;
+        return WTF::nullopt;
     
-    result = string;
-    return true;    
+    return WTFMove(string);
 }
 
-bool ArgumentCoder<String>::decode(Decoder& decoder, String& result)
-{
-    uint32_t length;
-    if (!decoder.decode(length))
-        return false;
-
-    if (length == std::numeric_limits<uint32_t>::max()) {
-        // This is the null string.
-        result = String();
-        return true;
-    }
-
-    bool is8Bit;
-    if (!decoder.decode(is8Bit))
-        return false;
-
-    if (is8Bit)
-        return decodeStringText<LChar>(decoder, length, result);
-    return decodeStringText<UChar>(decoder, length, result);
-}
-
 Optional<String> ArgumentCoder<String>::decode(Decoder& decoder)
 {
     uint32_t length;
@@ -185,17 +163,21 @@
     if (!decoder.decode(is8Bit))
         return WTF::nullopt;
     
-    String result;
-    if (is8Bit) {
-        if (!decodeStringText<LChar>(decoder, length, result))
-            return WTF::nullopt;
-        return result;
-    }
-    if (!decodeStringText<UChar>(decoder, length, result))
-        return WTF::nullopt;
-    return result;
+    if (is8Bit)
+        return decodeStringText<LChar>(decoder, length);
+    return decodeStringText<UChar>(decoder, length);
 }
 
+bool ArgumentCoder<String>::decode(Decoder& decoder, String& result)
+{
+    Optional<String> string;
+    decoder >> string;
+    if (!string)
+        return false;
+    result = WTFMove(*string);
+    return true;
+}
+
 void ArgumentCoder<SHA1::Digest>::encode(Encoder& encoder, const SHA1::Digest& digest)
 {
     encoder.encodeFixedLengthData(digest.data(), sizeof(digest), 1);

Modified: branches/safari-608-branch/Source/WebKit/Platform/IPC/ArgumentCoders.h (248000 => 248001)


--- branches/safari-608-branch/Source/WebKit/Platform/IPC/ArgumentCoders.h	2019-07-30 03:56:52 UTC (rev 248000)
+++ branches/safari-608-branch/Source/WebKit/Platform/IPC/ArgumentCoders.h	2019-07-30 03:56:54 UTC (rev 248001)
@@ -366,56 +366,30 @@
 
     static void encode(Encoder& encoder, const HashMapType& hashMap)
     {
-        encoder << static_cast<uint64_t>(hashMap.size());
+        encoder << static_cast<uint32_t>(hashMap.size());
         for (typename HashMapType::const_iterator it = hashMap.begin(), end = hashMap.end(); it != end; ++it)
             encoder << *it;
     }
 
-    static bool decode(Decoder& decoder, HashMapType& hashMap)
-    {
-        uint64_t hashMapSize;
-        if (!decoder.decode(hashMapSize))
-            return false;
-
-        HashMapType tempHashMap;
-        for (uint64_t i = 0; i < hashMapSize; ++i) {
-            KeyArg key;
-            MappedArg value;
-            if (!decoder.decode(key))
-                return false;
-            if (!decoder.decode(value))
-                return false;
-
-            if (!tempHashMap.add(key, value).isNewEntry) {
-                // The hash map already has the specified key, bail.
-                decoder.markInvalid();
-                return false;
-            }
-        }
-
-        hashMap.swap(tempHashMap);
-        return true;
-    }
-
     static Optional<HashMapType> decode(Decoder& decoder)
     {
-        uint64_t hashMapSize;
+        uint32_t hashMapSize;
         if (!decoder.decode(hashMapSize))
             return WTF::nullopt;
 
         HashMapType hashMap;
-        for (uint64_t i = 0; i < hashMapSize; ++i) {
+        for (uint32_t i = 0; i < hashMapSize; ++i) {
             Optional<KeyArg> key;
             decoder >> key;
-            if (!key)
+            if (UNLIKELY(!key))
                 return WTF::nullopt;
 
             Optional<MappedArg> value;
             decoder >> value;
-            if (!value)
+            if (UNLIKELY(!value))
                 return WTF::nullopt;
 
-            if (!hashMap.add(WTFMove(key.value()), WTFMove(value.value())).isNewEntry) {
+            if (UNLIKELY(!hashMap.add(WTFMove(*key), WTFMove(*value)).isNewEntry)) {
                 // The hash map already has the specified key, bail.
                 decoder.markInvalid();
                 return WTF::nullopt;
@@ -422,8 +396,18 @@
             }
         }
 
-        return hashMap;
+        return WTFMove(hashMap);
     }
+
+    static bool decode(Decoder& decoder, HashMapType& hashMap)
+    {
+        Optional<HashMapType> tempHashMap;
+        decoder >> tempHashMap;
+        if (!tempHashMap)
+            return false;
+        hashMap.swap(*tempHashMap);
+        return true;
+    }
 };
 
 template<typename KeyArg, typename HashArg, typename KeyTraitsArg> struct ArgumentCoder<HashSet<KeyArg, HashArg, KeyTraitsArg>> {
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to