Title: [251736] trunk
Revision
251736
Author
[email protected]
Date
2019-10-29 14:43:30 -0700 (Tue, 29 Oct 2019)

Log Message

[JSC] Add fast path for String#localeCompare
https://bugs.webkit.org/show_bug.cgi?id=202676

Reviewed by Mark Lam.

JSTests:

* stress/locale-compare-bits.js: Added.
(shouldBe):

Source/_javascript_Core:

When String#localeCompare is invoked, we are setting up UCharIterator to iterate code points.
But this is too slow since its implementation is invoking function pointer for each code point
to get next code point. Strings have many code points typically. Invoking function pointer so many times
takes too much time just for locale-aware comparison.

This patch revises the implementation by adding 2 fast path and 1 slow path. The slow path requires extra memory,
but it is soon released (not GC-managed).

1. If both strings are ASCII (not Latin1), we use ucol_strcollUTF8.
2. If both strings are 16-bit, we use ucol_strcoll.
3. Otherwise, we convert strings to 16-bit strings, and then we use ucol_strcoll.

JetStream2/cdjs is improved from 56 to 85 on iMac Pro (50%).

* runtime/IntlCollator.cpp:
(JSC::IntlCollator::compareStrings):
* tools/JSDollarVM.cpp:
(JSC::functionMake16BitStringIfPossible):
(JSC::JSDollarVM::finishCreation):

Source/WTF:

* wtf/text/StringView.h:
(WTF::StringView::isAllASCII const):

Tools:

* TestWebKitAPI/Tests/WTF/StringView.cpp:
(TestWebKitAPI::TEST):

Modified Paths

Added Paths

Diff

Modified: trunk/JSTests/ChangeLog (251735 => 251736)


--- trunk/JSTests/ChangeLog	2019-10-29 21:41:09 UTC (rev 251735)
+++ trunk/JSTests/ChangeLog	2019-10-29 21:43:30 UTC (rev 251736)
@@ -1,3 +1,13 @@
+2019-10-29  Yusuke Suzuki  <[email protected]>
+
+        [JSC] Add fast path for String#localeCompare
+        https://bugs.webkit.org/show_bug.cgi?id=202676
+
+        Reviewed by Mark Lam.
+
+        * stress/locale-compare-bits.js: Added.
+        (shouldBe):
+
 2019-10-28  Yusuke Suzuki  <[email protected]>
 
         [JSC] Optimize Promise runtime functions

Added: trunk/JSTests/stress/locale-compare-bits.js (0 => 251736)


--- trunk/JSTests/stress/locale-compare-bits.js	                        (rev 0)
+++ trunk/JSTests/stress/locale-compare-bits.js	2019-10-29 21:43:30 UTC (rev 251736)
@@ -0,0 +1,21 @@
+function shouldBe(actual, expected) {
+    if (actual !== expected)
+        throw new Error('bad value: ' + actual);
+}
+
+var strings = [
+    "Cocoa",
+    "Cappuccino",
+    "Matcha",
+    "Cocoa\u0080",
+    "𠮷野家", // Japanese Beef-bowl shop name including surrogate-pairs.
+    "📱",
+];
+
+for (let lhs of strings) {
+    for (let rhs of strings) {
+        let expected = $vm.make16BitStringIfPossible(lhs).localeCompare($vm.make16BitStringIfPossible(rhs));
+        let result = lhs.localeCompare(rhs);
+        shouldBe(result, expected);
+    }
+}

Modified: trunk/Source/_javascript_Core/ChangeLog (251735 => 251736)


--- trunk/Source/_javascript_Core/ChangeLog	2019-10-29 21:41:09 UTC (rev 251735)
+++ trunk/Source/_javascript_Core/ChangeLog	2019-10-29 21:43:30 UTC (rev 251736)
@@ -1,3 +1,30 @@
+2019-10-29  Yusuke Suzuki  <[email protected]>
+
+        [JSC] Add fast path for String#localeCompare
+        https://bugs.webkit.org/show_bug.cgi?id=202676
+
+        Reviewed by Mark Lam.
+
+        When String#localeCompare is invoked, we are setting up UCharIterator to iterate code points.
+        But this is too slow since its implementation is invoking function pointer for each code point
+        to get next code point. Strings have many code points typically. Invoking function pointer so many times
+        takes too much time just for locale-aware comparison.
+
+        This patch revises the implementation by adding 2 fast path and 1 slow path. The slow path requires extra memory,
+        but it is soon released (not GC-managed).
+
+        1. If both strings are ASCII (not Latin1), we use ucol_strcollUTF8.
+        2. If both strings are 16-bit, we use ucol_strcoll.
+        3. Otherwise, we convert strings to 16-bit strings, and then we use ucol_strcoll.
+
+        JetStream2/cdjs is improved from 56 to 85 on iMac Pro (50%).
+
+        * runtime/IntlCollator.cpp:
+        (JSC::IntlCollator::compareStrings):
+        * tools/JSDollarVM.cpp:
+        (JSC::functionMake16BitStringIfPossible):
+        (JSC::JSDollarVM::finishCreation):
+
 2019-10-28  Yusuke Suzuki  <[email protected]>
 
         [JSC] Remove JSPromiseDeferred

Modified: trunk/Source/_javascript_Core/runtime/IntlCollator.cpp (251735 => 251736)


--- trunk/Source/_javascript_Core/runtime/IntlCollator.cpp	2019-10-29 21:41:09 UTC (rev 251735)
+++ trunk/Source/_javascript_Core/runtime/IntlCollator.cpp	2019-10-29 21:43:30 UTC (rev 251736)
@@ -342,9 +342,24 @@
     }
 
     UErrorCode status = U_ZERO_ERROR;
-    UCharIterator iteratorX = createIterator(x);
-    UCharIterator iteratorY = createIterator(y);
-    auto result = ucol_strcollIter(m_collator.get(), &iteratorX, &iteratorY, &status);
+    UCollationResult result = UCOL_EQUAL;
+    if (x.is8Bit() && y.is8Bit() && x.isAllASCII() && y.isAllASCII())
+        result = ucol_strcollUTF8(m_collator.get(), bitwise_cast<const char*>(x.characters8()), x.length(), bitwise_cast<const char*>(y.characters8()), y.length(), &status);
+    else {
+        auto getCharacters = [&] (const StringView& view, Vector<UChar>& buffer) -> const UChar* {
+            if (!view.is8Bit())
+                return view.characters16();
+            buffer.resize(view.length());
+            StringImpl::copyCharacters(buffer.data(), view.characters8(), view.length());
+            return buffer.data();
+        };
+
+        Vector<UChar> xBuffer;
+        Vector<UChar> yBuffer;
+        const UChar* xCharacters = getCharacters(x, xBuffer);
+        const UChar* yCharacters = getCharacters(y, yBuffer);
+        result = ucol_strcoll(m_collator.get(), xCharacters, x.length(), yCharacters, y.length());
+    }
     if (U_FAILURE(status))
         return throwException(globalObject, scope, createError(globalObject, "Failed to compare strings."_s));
     return jsNumber(result);

Modified: trunk/Source/_javascript_Core/tools/JSDollarVM.cpp (251735 => 251736)


--- trunk/Source/_javascript_Core/tools/JSDollarVM.cpp	2019-10-29 21:41:09 UTC (rev 251735)
+++ trunk/Source/_javascript_Core/tools/JSDollarVM.cpp	2019-10-29 21:43:30 UTC (rev 251736)
@@ -2555,6 +2555,21 @@
 #endif
 }
 
+static EncodedJSValue JSC_HOST_CALL functionMake16BitStringIfPossible(JSGlobalObject* globalObject, CallFrame* callFrame)
+{
+    DollarVMAssertScope assertScope;
+    VM& vm = globalObject->vm();
+    auto scope = DECLARE_THROW_SCOPE(vm);
+    String string = callFrame->argument(0).toWTFString(globalObject);
+    RETURN_IF_EXCEPTION(scope, { });
+    if (!string.is8Bit())
+        return JSValue::encode(jsString(vm, WTFMove(string)));
+    Vector<UChar> buffer;
+    buffer.resize(string.length());
+    StringImpl::copyCharacters(buffer.data(), string.characters8(), string.length());
+    return JSValue::encode(jsString(vm, String::adopt(WTFMove(buffer))));
+}
+
 void JSDollarVM::finishCreation(VM& vm)
 {
     DollarVMAssertScope assertScope;
@@ -2677,6 +2692,7 @@
     addFunction(vm, "parseCount", functionParseCount, 0);
 
     addFunction(vm, "isWasmSupported", functionIsWasmSupported, 0);
+    addFunction(vm, "make16BitStringIfPossible", functionMake16BitStringIfPossible, 1);
 }
 
 void JSDollarVM::addFunction(VM& vm, JSGlobalObject* globalObject, const char* name, NativeFunction function, unsigned arguments)

Modified: trunk/Source/WTF/ChangeLog (251735 => 251736)


--- trunk/Source/WTF/ChangeLog	2019-10-29 21:41:09 UTC (rev 251735)
+++ trunk/Source/WTF/ChangeLog	2019-10-29 21:43:30 UTC (rev 251736)
@@ -1,3 +1,13 @@
+2019-10-29  Yusuke Suzuki  <[email protected]>
+
+        [JSC] Add fast path for String#localeCompare
+        https://bugs.webkit.org/show_bug.cgi?id=202676
+
+        Reviewed by Mark Lam.
+
+        * wtf/text/StringView.h:
+        (WTF::StringView::isAllASCII const):
+
 2019-10-26  Chris Lord  <[email protected]>
 
         Put OffscreenCanvas behind a build flag

Modified: trunk/Source/WTF/wtf/text/StringView.h (251735 => 251736)


--- trunk/Source/WTF/wtf/text/StringView.h	2019-10-29 21:41:09 UTC (rev 251735)
+++ trunk/Source/WTF/wtf/text/StringView.h	2019-10-29 21:43:30 UTC (rev 251736)
@@ -92,6 +92,8 @@
     const LChar* characters8() const;
     const UChar* characters16() const;
 
+    bool isAllASCII() const;
+
     String toString() const;
     String toStringWithoutCopying() const;
     AtomString toAtomString() const;
@@ -403,6 +405,13 @@
     return static_cast<const UChar*>(m_characters);
 }
 
+inline bool StringView::isAllASCII() const
+{
+    if (is8Bit())
+        return charactersAreAllASCII(characters8(), length());
+    return charactersAreAllASCII(characters16(), length());
+}
+
 class StringView::UpconvertedCharacters {
     WTF_MAKE_FAST_ALLOCATED;
 public:

Modified: trunk/Tools/ChangeLog (251735 => 251736)


--- trunk/Tools/ChangeLog	2019-10-29 21:41:09 UTC (rev 251735)
+++ trunk/Tools/ChangeLog	2019-10-29 21:43:30 UTC (rev 251736)
@@ -1,3 +1,13 @@
+2019-10-29  Yusuke Suzuki  <[email protected]>
+
+        [JSC] Add fast path for String#localeCompare
+        https://bugs.webkit.org/show_bug.cgi?id=202676
+
+        Reviewed by Mark Lam.
+
+        * TestWebKitAPI/Tests/WTF/StringView.cpp:
+        (TestWebKitAPI::TEST):
+
 2019-10-27  Wenson Hsieh  <[email protected]>
 
         Add enterkeyhint support

Modified: trunk/Tools/TestWebKitAPI/Tests/WTF/StringView.cpp (251735 => 251736)


--- trunk/Tools/TestWebKitAPI/Tests/WTF/StringView.cpp	2019-10-29 21:41:09 UTC (rev 251735)
+++ trunk/Tools/TestWebKitAPI/Tests/WTF/StringView.cpp	2019-10-29 21:43:30 UTC (rev 251736)
@@ -971,4 +971,13 @@
     EXPECT_TRUE(nullView.stripLeadingAndTrailingMatchedCharacters(isA) == nullView);
 }
 
+TEST(WTF, StringViewIsAllASCII)
+{
+    EXPECT_TRUE(StringView(String("Hello")).isAllASCII());
+    EXPECT_TRUE(StringView(String("Cocoa")).isAllASCII());
+    EXPECT_FALSE(StringView(String("📱")).isAllASCII());
+    EXPECT_FALSE(StringView(String("\u0080")).isAllASCII());
+    EXPECT_TRUE(StringView(String(bitwise_cast<const UChar*>(u"Hello"))).isAllASCII());
+}
+
 } // namespace TestWebKitAPI
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to