Skip to content

[WTF] toLowerCase should not call ICU for ASCII and caseless characters - #79713

Open
sosukesuzuki wants to merge 1 commit into
WebKit:mainfrom
sosukesuzuki:eng/WTF-toLowerCase-should-not-call-ICU-for-ASCII-and-caseless-characters
Open

sosukesuzuki wants to merge 1 commit into
WebKit:mainfrom
sosukesuzuki:eng/WTF-toLowerCase-should-not-call-ICU-for-ASCII-and-caseless-characters

Conversation

@sosukesuzuki

@sosukesuzuki sosukesuzuki commented Nov 5, 2026 •

Copy link
Copy Markdown
Member

7887d7b

[WTF] `toLowerCase` should not call ICU for ASCII and caseless characters
https://bugs.webkit.org/show_bug.cgi?id=330241

Reviewed by NOBODY (OOPS!).

Lowercasing a 16-bit string calls ICU when the string has any non-ASCII
character, even when that character never changes, as in Japanese, Chinese and
Korean text or in English text with a curly quote.

This patch skips ICU when every character is ASCII or in a small table of ranges
with Changes_When_Lowercased=No [1]. The string itself is returned when it has no
ASCII uppercase letter. The table does not have to be complete, since any other
character still goes to ICU.

                                        Baseline                  Patched

string-to-lower-case-caseless       89.7924+-6.0371     ^     51.8034+-4.8645        ^ definitely 1.7333x faster

[1]: https://www.unicode.org/Public/UCD/latest/ucd/DerivedCoreProperties.txt

Tests: JSTests/microbenchmarks/string-to-lower-case-caseless.js
       JSTests/stress/to-lower-case-caseless.js

* JSTests/microbenchmarks/string-to-lower-case-caseless.js: Added.
(test):
* JSTests/stress/to-lower-case-caseless.js: Added.
(shouldBe):
(toLowerCaseWithICU):
* Source/WTF/wtf/text/StringImpl.cpp:
(WTF::containsOnlyASCIIOrCaseless):
(WTF::StringImpl::convertToLowercaseWithoutLocaleStartingAtFailingIndex16Bit):

7887d7b

Misc iOS, visionOS, tvOS & watchOS macOS Linux Windows
✅ 🧪 style ✅ 🛠 ios ✅ 🛠 mac ✅ 🛠 wpe ✅ 🛠 win
✅ 🛠 ios-sim ✅ 🛠 mac-cmake-debug ✅ 🧪 wpe-wk2 ✅ 🧪 win-tests
✅ 🧪 webkitperl ✅ 🧪 ios-wk2 ✅ 🧪 api-mac ✅ 🧪 api-wpe
✅ 🧪 ios-wk2-wpt ✅ 🧪 api-mac-cmake-debug ✅ 🧪 jsc-wpe
✅ 🧪 jsc-x86-64 ✅ 🧪 api-ios ✅ 🧪 mac-wk2 ✅ 🛠 gtk3-gcc
✅ 🛠 🧪 jsc-debug-arm64 ✅ 🛠 ios-safer-cpp ✅ 🧪 mac-cmake-debug-wk2 ✅ 🛠 gtk
✅ 🛠 vision ✅ 🧪 gtk-wk2
✅ 🛠 vision-sim ✅ 🧪 mac-intel-wk2 ✅ 🧪 api-gtk
✅ 🧪 vision-wk2 ✅ 🛠 mac-safer-cpp ✅ 🛠 playstation
✅ 🛠 tv ✅ 🧪 mac-site-isolation
✅ 🛠 tv-sim
✅ 🛠 watch
✅ 🛠 watch-sim

…ters

https://bugs.webkit.org/show_bug.cgi?id=330241

Reviewed by NOBODY (OOPS!).

Lowercasing a 16-bit string calls ICU when the string has any non-ASCII
character, even when that character never changes, as in Japanese, Chinese and
Korean text or in English text with a curly quote.

This patch skips ICU when every character is ASCII or in a small table of ranges
with Changes_When_Lowercased=No [1]. The string itself is returned when it has no
ASCII uppercase letter. The table does not have to be complete, since any other
character still goes to ICU.

                                        Baseline                  Patched

string-to-lower-case-caseless       89.7924+-6.0371     ^     51.8034+-4.8645        ^ definitely 1.7333x faster

[1]: https://www.unicode.org/Public/UCD/latest/ucd/DerivedCoreProperties.txt

Tests: JSTests/microbenchmarks/string-to-lower-case-caseless.js
       JSTests/stress/to-lower-case-caseless.js

* JSTests/microbenchmarks/string-to-lower-case-caseless.js: Added.
(test):
* JSTests/stress/to-lower-case-caseless.js: Added.
(shouldBe):
(toLowerCaseWithICU):
* Source/WTF/wtf/text/StringImpl.cpp:
(WTF::containsOnlyASCIIOrCaseless):
(WTF::StringImpl::convertToLowercaseWithoutLocaleStartingAtFailingIndex16Bit):
@sosukesuzuki
sosukesuzuki requested a review from a team as a code owner November 5, 2026 02:00
@sosukesuzuki sosukesuzuki self-assigned this Nov 5, 2026
{ 0xAC00, 0xD7A3 }, // Hangul Syllables
{ 0xE000, 0xFAFF }, // Private Use Area and CJK Compatibility Ideographs
{ 0xFF01, 0xFF20 }, // Fullwidth ASCII punctuation and digits
} };

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a code testing this ranges are correct with ICU when ASSERT_ENABLED is true? We would like to guard the logic against future Unicode changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants