RESOLVED FIXED 213158
[JSC] String.protoytpe.toLocaleLowerCase's availableLocales HashSet is inefficient
https://bugs.webkit.org/show_bug.cgi?id=213158
Summary [JSC] String.protoytpe.toLocaleLowerCase's availableLocales HashSet is ineffi...
Yusuke Suzuki
Reported 2020-06-12 17:25:51 PDT
There are only 4 elements and all of them are two characters strings. We should just have `std::array<ASCIILiteral>` and iterate it, it is much efficient than this.
Attachments
Patch (3.17 KB, patch)
2020-07-12 17:14 PDT, Yusuke Suzuki
no flags
Patch (8.19 KB, patch)
2020-07-12 17:54 PDT, Yusuke Suzuki
no flags
Patch (8.22 KB, patch)
2020-07-12 17:57 PDT, Yusuke Suzuki
no flags
Patch (8.25 KB, patch)
2020-07-12 18:03 PDT, Yusuke Suzuki
no flags
Yusuke Suzuki
Comment 1 2020-07-12 17:12:47 PDT
Currently, bestAvailableLocale requires HashSet and it is used in the other code. Maybe, just creating static HashSet would be nice.
Yusuke Suzuki
Comment 2 2020-07-12 17:14:35 PDT
Darin Adler
Comment 3 2020-07-12 17:23:08 PDT
Comment on attachment 404121 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=404121&action=review > Source/JavaScriptCore/runtime/StringPrototype.cpp:1548 > + static std::once_flag onceKey; > + std::call_once(onceKey, [&] { Since String doesn’t have a thread-safe reference count, using a thread-safe run-once algorithm here isn’t enough to make access to this global shared object thread-safe, so I think this should not be using call_once. Unless StaticStringImpl is enough to make access to a string thread-safe, in which case, OK! > Source/JavaScriptCore/runtime/StringPrototype.cpp:1557 > + static StringImpl::StaticStringImpl az("az"); > + static StringImpl::StaticStringImpl el("el"); > + static StringImpl::StaticStringImpl lt("lt"); > + static StringImpl::StaticStringImpl tr("tr"); > + availableLocales.construct(); > + availableLocales->add(&static_cast<StringImpl&>(az)); > + availableLocales->add(&static_cast<StringImpl&>(el)); > + availableLocales->add(&static_cast<StringImpl&>(lt)); > + availableLocales->add(&static_cast<StringImpl&>(tr)); I really wish we had a better idiom for this. The old code was inefficient, but readable. It would be nice if the efficient version was also readable. Even if we had to put some helper functions somewhere. It seems to me that this might not be the only constant HashSet of strings we ever want to create. Also, can the StringImpl::StaticStringImpl be const? Finally, does this need to be a HashSet. For a list of 4 strings I think another kind of set could be efficient, like a constant array that we use binary_search with. Maybe even something that first checks that the string is 2 characters long and then converts the string to a uint16_t and compares against 4 constant values.
Yusuke Suzuki
Comment 4 2020-07-12 17:34:45 PDT
Comment on attachment 404121 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=404121&action=review I'll try refactoring bestAvailableLocale. >> Source/JavaScriptCore/runtime/StringPrototype.cpp:1548 >> + std::call_once(onceKey, [&] { > > Since String doesn’t have a thread-safe reference count, using a thread-safe run-once algorithm here isn’t enough to make access to this global shared object thread-safe, so I think this should not be using call_once. > > Unless StaticStringImpl is enough to make access to a string thread-safe, in which case, OK! Yes, that's why I used StaticStringImpl! This generates static StringImpl, which can be usable in a thread-safe manner (special ref-count is used and it will be never destroyed).
Yusuke Suzuki
Comment 5 2020-07-12 17:54:03 PDT
Yusuke Suzuki
Comment 6 2020-07-12 17:57:44 PDT
Yusuke Suzuki
Comment 7 2020-07-12 18:03:29 PDT
Darin Adler
Comment 8 2020-07-12 18:33:31 PDT
Comment on attachment 404124 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=404124&action=review > Source/JavaScriptCore/runtime/IntlObjectInlines.h:40 > + String candidate = locale; This would be more efficient if it used StringView; no memory allocation when substring is called.
Yusuke Suzuki
Comment 9 2020-07-12 23:04:42 PDT
Comment on attachment 404124 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=404124&action=review >> Source/JavaScriptCore/runtime/IntlObjectInlines.h:40 >> + String candidate = locale; > > This would be more efficient if it used StringView; no memory allocation when substring is called. Yeah, we should optimize these things in different patches I think. We have a lot of opportunities.
EWS
Comment 10 2020-07-12 23:27:24 PDT
Committed r264293: <https://trac.webkit.org/changeset/264293> All reviewed patches have been landed. Closing bug and clearing flags on attachment 404124 [details].
Radar WebKit Bug Importer
Comment 11 2020-07-12 23:28:14 PDT
Darin Adler
Comment 12 2020-07-13 08:28:01 PDT
Comment on attachment 404124 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=404124&action=review >>> Source/JavaScriptCore/runtime/IntlObjectInlines.h:40 >>> + String candidate = locale; >> >> This would be more efficient if it used StringView; no memory allocation when substring is called. > > Yeah, we should optimize these things in different patches I think. We have a lot of opportunities. I commented because the code was *new*.
Note You need to log in before you can comment on or make changes to this bug.