Title: [264293] trunk/Source
Revision
264293
Author
[email protected]
Date
2020-07-12 23:27:19 -0700 (Sun, 12 Jul 2020)

Log Message

[JSC] String.protoytpe.toLocaleLowerCase's availableLocales HashSet is inefficient
https://bugs.webkit.org/show_bug.cgi?id=213158

Reviewed by Darin Adler.

Source/_javascript_Core:

Currently, we are always creating the same HashSet every time String.protoytpe.toLocaleLowerCase is called.
We changed bestAvailableLocale to take predicate function. And we pass a predicate which returns true for
case-sensitive locales.

* runtime/IntlObject.cpp:
(JSC::bestAvailableLocale):
* runtime/IntlObject.h:
* runtime/IntlObjectInlines.h:
(JSC::bestAvailableLocale):
* runtime/StringPrototype.cpp:
(JSC::computeTwoCharacters16Code):
(JSC::toLocaleCase):

Source/WTF:

Add characterAt method to ASCIILiteral.

* wtf/text/ASCIILiteral.h:

Modified Paths

Diff

Modified: trunk/Source/_javascript_Core/ChangeLog (264292 => 264293)


--- trunk/Source/_javascript_Core/ChangeLog	2020-07-13 06:20:01 UTC (rev 264292)
+++ trunk/Source/_javascript_Core/ChangeLog	2020-07-13 06:27:19 UTC (rev 264293)
@@ -1,5 +1,25 @@
 2020-07-12  Yusuke Suzuki  <[email protected]>
 
+        [JSC] String.protoytpe.toLocaleLowerCase's availableLocales HashSet is inefficient
+        https://bugs.webkit.org/show_bug.cgi?id=213158
+
+        Reviewed by Darin Adler.
+
+        Currently, we are always creating the same HashSet every time String.protoytpe.toLocaleLowerCase is called.
+        We changed bestAvailableLocale to take predicate function. And we pass a predicate which returns true for
+        case-sensitive locales.
+
+        * runtime/IntlObject.cpp:
+        (JSC::bestAvailableLocale):
+        * runtime/IntlObject.h:
+        * runtime/IntlObjectInlines.h:
+        (JSC::bestAvailableLocale):
+        * runtime/StringPrototype.cpp:
+        (JSC::computeTwoCharacters16Code):
+        (JSC::toLocaleCase):
+
+2020-07-12  Yusuke Suzuki  <[email protected]>
+
         [JSC] We should keep unaligned access feature in certain architectures in macro-assembler
         https://bugs.webkit.org/show_bug.cgi?id=214243
 

Modified: trunk/Source/_javascript_Core/runtime/IntlObject.cpp (264292 => 264293)


--- trunk/Source/_javascript_Core/runtime/IntlObject.cpp	2020-07-13 06:20:01 UTC (rev 264292)
+++ trunk/Source/_javascript_Core/runtime/IntlObject.cpp	2020-07-13 06:27:19 UTC (rev 264293)
@@ -476,25 +476,9 @@
 
 String bestAvailableLocale(const HashSet<String>& availableLocales, const String& locale)
 {
-    // BestAvailableLocale (availableLocales, locale)
-    // https://tc39.github.io/ecma402/#sec-bestavailablelocale
-
-    String candidate = locale;
-    while (!candidate.isEmpty()) {
-        if (availableLocales.contains(candidate))
-            return candidate;
-
-        size_t pos = candidate.reverseFind('-');
-        if (pos == notFound)
-            return String();
-
-        if (pos >= 2 && candidate[pos - 2] == '-')
-            pos -= 2;
-
-        candidate = candidate.substring(0, pos);
-    }
-
-    return String();
+    return bestAvailableLocale(locale, [&](const String& candidate) {
+        return availableLocales.contains(candidate);
+    });
 }
 
 String defaultLocale(JSGlobalObject* globalObject)

Modified: trunk/Source/_javascript_Core/runtime/IntlObject.h (264292 => 264293)


--- trunk/Source/_javascript_Core/runtime/IntlObject.h	2020-07-13 06:20:01 UTC (rev 264292)
+++ trunk/Source/_javascript_Core/runtime/IntlObject.h	2020-07-13 06:27:19 UTC (rev 264293)
@@ -75,6 +75,7 @@
 JSValue supportedLocales(JSGlobalObject*, const HashSet<String>& availableLocales, const Vector<String>& requestedLocales, JSValue options);
 String removeUnicodeLocaleExtension(const String& locale);
 String bestAvailableLocale(const HashSet<String>& availableLocales, const String& requestedLocale);
+template<typename Predicate> String bestAvailableLocale(const String& requestedLocale, Predicate);
 Vector<String> numberingSystemsForLocale(const String& locale);
 
 bool isUnicodeLocaleIdentifierType(StringView);

Modified: trunk/Source/_javascript_Core/runtime/IntlObjectInlines.h (264292 => 264293)


--- trunk/Source/_javascript_Core/runtime/IntlObjectInlines.h	2020-07-13 06:20:01 UTC (rev 264292)
+++ trunk/Source/_javascript_Core/runtime/IntlObjectInlines.h	2020-07-13 06:27:19 UTC (rev 264293)
@@ -32,6 +32,29 @@
 
 namespace JSC {
 
+template<typename Predicate> String bestAvailableLocale(const String& locale, Predicate predicate)
+{
+    // BestAvailableLocale (availableLocales, locale)
+    // https://tc39.github.io/ecma402/#sec-bestavailablelocale
+
+    String candidate = locale;
+    while (!candidate.isEmpty()) {
+        if (predicate(candidate))
+            return candidate;
+
+        size_t pos = candidate.reverseFind('-');
+        if (pos == notFound)
+            return String();
+
+        if (pos >= 2 && candidate[pos - 2] == '-')
+            pos -= 2;
+
+        candidate = candidate.substring(0, pos);
+    }
+
+    return String();
+}
+
 template<typename IntlInstance, typename Constructor, typename Factory>
 JSValue constructIntlInstanceWithWorkaroundForLegacyIntlConstructor(JSGlobalObject* globalObject, JSValue thisValue, Constructor* callee, Factory factory)
 {

Modified: trunk/Source/_javascript_Core/runtime/StringPrototype.cpp (264292 => 264293)


--- trunk/Source/_javascript_Core/runtime/StringPrototype.cpp	2020-07-13 06:20:01 UTC (rev 264292)
+++ trunk/Source/_javascript_Core/runtime/StringPrototype.cpp	2020-07-13 06:27:19 UTC (rev 264293)
@@ -30,7 +30,7 @@
 #include "IntegrityInlines.h"
 #include "InterpreterInlines.h"
 #include "IntlCollator.h"
-#include "IntlObject.h"
+#include "IntlObjectInlines.h"
 #include "JITCodeInlines.h"
 #include "JSArray.h"
 #include "JSCInlines.h"
@@ -1541,6 +1541,12 @@
     RELEASE_AND_RETURN(scope, JSValue::encode(collator->compareStrings(globalObject, string, that)));
 }
 
+template<typename StringType>
+static constexpr uint32_t computeTwoCharacters16Code(const StringType& string)
+{
+    return static_cast<uint16_t>(string.characterAt(0)) | (static_cast<uint32_t>(static_cast<uint16_t>(string.characterAt(1))) << 16);
+}
+
 enum class CaseConversionMode {
     Upper,
     Lower,
@@ -1588,12 +1594,20 @@
 
     // 10. Let availableLocales be a List with the language tags of the languages for which the Unicode character database contains language sensitive case mappings.
     // Note 1: As of Unicode 5.1, the availableLocales list contains the elements "az", "el", "lt", and "tr".
-    // FIXME: Creating HashSet for these 4 locales every time is inefficient.
-    // https://bugs.webkit.org/show_bug.cgi?id=213158
-    const HashSet<String> availableLocales({ "az"_s, "el"_s, "lt"_s, "tr"_s });
-
     // 11. Let locale be BestAvailableLocale(availableLocales, noExtensionsLocale).
-    String locale = bestAvailableLocale(availableLocales, noExtensionsLocale);
+    String locale = bestAvailableLocale(noExtensionsLocale, [](const String& candidate) {
+        if (candidate.length() != 2)
+            return false;
+        switch (computeTwoCharacters16Code(candidate)) {
+        case computeTwoCharacters16Code("az"_s):
+        case computeTwoCharacters16Code("el"_s):
+        case computeTwoCharacters16Code("lt"_s):
+        case computeTwoCharacters16Code("tr"_s):
+            return true;
+        default:
+            return false;
+        }
+    });
 
     // 12. If locale is undefined, let locale be "und".
     if (locale.isNull())

Modified: trunk/Source/WTF/ChangeLog (264292 => 264293)


--- trunk/Source/WTF/ChangeLog	2020-07-13 06:20:01 UTC (rev 264292)
+++ trunk/Source/WTF/ChangeLog	2020-07-13 06:27:19 UTC (rev 264293)
@@ -1,3 +1,14 @@
+2020-07-12  Yusuke Suzuki  <[email protected]>
+
+        [JSC] String.protoytpe.toLocaleLowerCase's availableLocales HashSet is inefficient
+        https://bugs.webkit.org/show_bug.cgi?id=213158
+
+        Reviewed by Darin Adler.
+
+        Add characterAt method to ASCIILiteral.
+
+        * wtf/text/ASCIILiteral.h:
+
 2020-07-12  Darin Adler  <[email protected]>
 
         Some further streamlining of Gradient handling code

Modified: trunk/Source/WTF/wtf/text/ASCIILiteral.h (264292 => 264293)


--- trunk/Source/WTF/wtf/text/ASCIILiteral.h	2020-07-13 06:20:01 UTC (rev 264292)
+++ trunk/Source/WTF/wtf/text/ASCIILiteral.h	2020-07-13 06:27:19 UTC (rev 264293)
@@ -52,6 +52,8 @@
     const LChar* characters8() const { return bitwise_cast<const LChar*>(m_characters); }
     size_t length() const { return strlen(m_characters); }
 
+    constexpr char characterAt(unsigned index) const { return m_characters[index]; }
+
 private:
     constexpr explicit ASCIILiteral(const char* characters) : m_characters(characters) { }
 
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to