⚠ Archived content — this site is no longer maintained.   Current WebKit documentation is at docs.webkit.org.

Changeset 264293 in webkit


Ignore:
Timestamp:
Jul 12, 2020, 11:27:19 PM (6 years ago)
Author:
ysuzuki@apple.com
Message:

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

Reviewed by Darin Adler.

Source/JavaScriptCore:

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:
Location:
trunk/Source
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/ChangeLog

    r264287 r264293  
     12020-07-12  Yusuke Suzuki  <ysuzuki@apple.com>
     2
     3        [JSC] String.protoytpe.toLocaleLowerCase's availableLocales HashSet is inefficient
     4        https://bugs.webkit.org/show_bug.cgi?id=213158
     5
     6        Reviewed by Darin Adler.
     7
     8        Currently, we are always creating the same HashSet every time String.protoytpe.toLocaleLowerCase is called.
     9        We changed bestAvailableLocale to take predicate function. And we pass a predicate which returns true for
     10        case-sensitive locales.
     11
     12        * runtime/IntlObject.cpp:
     13        (JSC::bestAvailableLocale):
     14        * runtime/IntlObject.h:
     15        * runtime/IntlObjectInlines.h:
     16        (JSC::bestAvailableLocale):
     17        * runtime/StringPrototype.cpp:
     18        (JSC::computeTwoCharacters16Code):
     19        (JSC::toLocaleCase):
     20
    1212020-07-12  Yusuke Suzuki  <ysuzuki@apple.com>
    222
  • trunk/Source/JavaScriptCore/runtime/IntlObject.cpp

    r263227 r264293  
    477477String bestAvailableLocale(const HashSet<String>& availableLocales, const String& locale)
    478478{
    479     // BestAvailableLocale (availableLocales, locale)
    480     // https://tc39.github.io/ecma402/#sec-bestavailablelocale
    481 
    482     String candidate = locale;
    483     while (!candidate.isEmpty()) {
    484         if (availableLocales.contains(candidate))
    485             return candidate;
    486 
    487         size_t pos = candidate.reverseFind('-');
    488         if (pos == notFound)
    489             return String();
    490 
    491         if (pos >= 2 && candidate[pos - 2] == '-')
    492             pos -= 2;
    493 
    494         candidate = candidate.substring(0, pos);
    495     }
    496 
    497     return String();
     479    return bestAvailableLocale(locale, [&](const String& candidate) {
     480        return availableLocales.contains(candidate);
     481    });
    498482}
    499483
  • trunk/Source/JavaScriptCore/runtime/IntlObject.h

    r261215 r264293  
    7676String removeUnicodeLocaleExtension(const String& locale);
    7777String bestAvailableLocale(const HashSet<String>& availableLocales, const String& requestedLocale);
     78template<typename Predicate> String bestAvailableLocale(const String& requestedLocale, Predicate);
    7879Vector<String> numberingSystemsForLocale(const String& locale);
    7980
  • trunk/Source/JavaScriptCore/runtime/IntlObjectInlines.h

    r259767 r264293  
    3333namespace JSC {
    3434
     35template<typename Predicate> String bestAvailableLocale(const String& locale, Predicate predicate)
     36{
     37    // BestAvailableLocale (availableLocales, locale)
     38    // https://tc39.github.io/ecma402/#sec-bestavailablelocale
     39
     40    String candidate = locale;
     41    while (!candidate.isEmpty()) {
     42        if (predicate(candidate))
     43            return candidate;
     44
     45        size_t pos = candidate.reverseFind('-');
     46        if (pos == notFound)
     47            return String();
     48
     49        if (pos >= 2 && candidate[pos - 2] == '-')
     50            pos -= 2;
     51
     52        candidate = candidate.substring(0, pos);
     53    }
     54
     55    return String();
     56}
     57
    3558template<typename IntlInstance, typename Constructor, typename Factory>
    3659JSValue constructIntlInstanceWithWorkaroundForLegacyIntlConstructor(JSGlobalObject* globalObject, JSValue thisValue, Constructor* callee, Factory factory)
  • trunk/Source/JavaScriptCore/runtime/StringPrototype.cpp

    r263905 r264293  
    3131#include "InterpreterInlines.h"
    3232#include "IntlCollator.h"
    33 #include "IntlObject.h"
     33#include "IntlObjectInlines.h"
    3434#include "JITCodeInlines.h"
    3535#include "JSArray.h"
    … …  
    15421542}
    15431543
     1544template<typename StringType>
     1545static constexpr uint32_t computeTwoCharacters16Code(const StringType& string)
     1546{
     1547    return static_cast<uint16_t>(string.characterAt(0)) | (static_cast<uint32_t>(static_cast<uint16_t>(string.characterAt(1))) << 16);
     1548}
     1549
    15441550enum class CaseConversionMode {
    15451551    Upper,
    … …  
    15891595    // 10. Let availableLocales be a List with the language tags of the languages for which the Unicode character database contains language sensitive case mappings.
    15901596    // Note 1: As of Unicode 5.1, the availableLocales list contains the elements "az", "el", "lt", and "tr".
    1591     // FIXME: Creating HashSet for these 4 locales every time is inefficient.
    1592     // https://bugs.webkit.org/show_bug.cgi?id=213158
    1593     const HashSet<String> availableLocales({ "az"_s, "el"_s, "lt"_s, "tr"_s });
    1594 
    15951597    // 11. Let locale be BestAvailableLocale(availableLocales, noExtensionsLocale).
    1596     String locale = bestAvailableLocale(availableLocales, noExtensionsLocale);
     1598    String locale = bestAvailableLocale(noExtensionsLocale, [](const String& candidate) {
     1599        if (candidate.length() != 2)
     1600            return false;
     1601        switch (computeTwoCharacters16Code(candidate)) {
     1602        case computeTwoCharacters16Code("az"_s):
     1603        case computeTwoCharacters16Code("el"_s):
     1604        case computeTwoCharacters16Code("lt"_s):
     1605        case computeTwoCharacters16Code("tr"_s):
     1606            return true;
     1607        default:
     1608            return false;
     1609        }
     1610    });
    15971611
    15981612    // 12. If locale is undefined, let locale be "und".
  • trunk/Source/WTF/ChangeLog

    r264290 r264293  
     12020-07-12  Yusuke Suzuki  <ysuzuki@apple.com>
     2
     3        [JSC] String.protoytpe.toLocaleLowerCase's availableLocales HashSet is inefficient
     4        https://bugs.webkit.org/show_bug.cgi?id=213158
     5
     6        Reviewed by Darin Adler.
     7
     8        Add characterAt method to ASCIILiteral.
     9
     10        * wtf/text/ASCIILiteral.h:
     11
    1122020-07-12  Darin Adler  <darin@apple.com>
    213
  • trunk/Source/WTF/wtf/text/ASCIILiteral.h

    r263665 r264293  
    5353    size_t length() const { return strlen(m_characters); }
    5454
     55    constexpr char characterAt(unsigned index) const { return m_characters[index]; }
     56
    5557private:
    5658    constexpr explicit ASCIILiteral(const char* characters) : m_characters(characters) { }
Note: See TracChangeset for help on using the changeset viewer.