Repository navigation
Conversation
… up in it Every Intl constructor and every supportedLocalesOf() takes the available locales of its service, also when no locale is requested. To make that set, ICU converts every locale ID of a list to a language tag. With ICU 79 that is 906 calls of uloc_toLanguageTag and 12 million instructions, or 146 calls and 1.4 million for the collator's list. A call that requests no locale paid for the set and looked nothing up in it. IntlAvailableLocales holds the function that makes the set, and calls it when a language tag is first looked up. The three functions are the ones that were there, under new names. First `new Intl.NumberFormat()` of a process, user instructions over an empty script: 12.7 million before, 0.5 million now. A first use that looks a locale up makes the set as before. $vm.intlLocaleIDsPutIntoSets() and $vm.intlLookupsThatFoundNoSet() count what the test holds: the locale IDs that were put into a set, and the lookups that found no set.
…rvices ICU's ubrk_getAvailable() returns what uloc_getAvailable() returns. So the segmenter's set of available locales was a second set of the same language tags: 906 more calls of uloc_toLanguageTag with ICU 79. The segmenter's first lookup compares ICU's two lists, pointer by pointer. When they are the same strings, it takes the set of the other services. When they are not, it makes its own set as before. First lookup of a formatter and then of the segmenter, user instructions over an empty script: 23.7 million on main, 12.7 million now.
|
Status: ready for review. CI is green on 03a0af7: 86 checks pass, and the 7 tested lanes ran the JavaScriptCore tests with 0 failures. Notes has two questions for a maintainer, and #1066 has one about the same loops. How the problem was reproduced (Linux x64, main at 499846f, ICU 79.1):
With this PR: 0.46 million instructions in Not run here: macOS and Windows. The lanes of this PR compile both. |
|
Preview build of 03a0af7: |
There was a problem hiding this comment.
Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.
Beyond the inline finding, I also checked the concurrent first-lookup path in IntlAvailableLocales::contains (Source/JavaScriptCore/runtime/IntlObject.h) — the set itself is still built under std::call_once and the handle is published with release/acquire, so two threads racing on an empty handle get the same set; and the segmenter's pointer-wise ubrk/uloc list comparison (IntlObject.cpp) falls back to building its own set whenever the lists differ, so an ICU with a distinct break-iterator list keeps main's behavior. The new stress test was read, not run.
Extended reasoning...
The change is confined to USE(BUN_JSC_ADDITIONS) arms of the JSC Intl locale-set plumbing (IntlObject.h/.cpp, IntlCollator.h/.cpp), two $vm test counters, one new stress test and its BUN-TEST-DIFFERENCES.md entry; the 18 initialize*/supportedLocalesOf callers are untouched because bestAvailableLocale only uses contains(). It touches no auth, injection or data-exposure surface. One inline finding (debug-only invariant check now deferred to the first locale lookup) is posted separately; the two ruled-out candidates were the concurrent first-lookup race and the shared-counter semantics, both examined from the code.
Additional findings (outside the current diff — GitHub can't attach inline comments there):
-
🟡
Source/JavaScriptCore/runtime/IntlObject.cpp— Debug builds lose the only check that the release ASCII collation fast path agrees with the bundled ICU, for every collator made without a locale. On the base branch the firstnew Intl.Collator()orlocaleCompare()ran IntlCollator::checkICULocaleInvariants (IntlObject.cpp:649) over all 146 collator locales; now that call sits inside intlCollatorAvailableLocalesSet, which only runs when a tag is looked up (IntlObject.h:110-114), so locale-less collators never trigger it. Fix: in ASSERT_ENABLED builds, run the invariant check on the first collator construction (or the first compare) regardless of whether a locale is requested, e.g. call intlCollatorAvailableLocales().contains("en"_s) or the set maker from IntlCollator::initializeCollator under ASSERT_ENABLED.Why this was flagged
Trigger: a debug build whose first collator use requests no locale, e.g.
"a".localeCompare("b"), through IntlCollator::initializeCollator at IntlCollator.cpp:181. Base: intlCollatorAvailableLocales() built the set under call_once and ran IntlCollator::checkICULocaleInvariants (IntlCollator.cpp:440-479), which compares ucol_strcoll against compareASCIIWithUCADUCET for every ASCII pair in every collator locale. Now intlCollatorAvailableLocales() at IntlObject.cpp:655-658 returns a constinit handle; the maker with the check (IntlObject.cpp:629-652) runs only when a tag is looked up (IntlObject.h:110-114), and lookupMatcher never calls contains() with an empty requestedLocales (IntlObject.cpp:1049). The release fast path updateCanDoASCIIUCADUCETComparison (IntlCollator.cpp:412-437) is therefore used by the default collator with no debug validation against ICU data. Remedy: run the invariant check on first collator construction in ASSERT_ENABLED builds independent of lookup.Verification: On base, IntlCollator::initializeCollator (IntlCollator.cpp:181) ran
IntlCollator::checkICULocaleInvariants(Source/JavaScriptCore/runtime/IntlObject.cpp:649) on every first collator use. After the PR, that call lives inintlCollatorAvailableLocalesSet()(IntlObject.cpp:629-652), reached only throughcontains()(IntlObject.h:110-116). With an emptyrequestedLocalesno lookup occurs, so the check never runs.
Problem
supportedLocalesOf()converts every locale ID of an ICU list to a language tag. That is 906uloc_toLanguageTagcalls and 12 million instructions, or 146 and 1.4 million for the collator.new Intl.Segmenter(),localeCompare()) pays that and looks nothing up. The segmenter converts the list again.intlAvailableLocales()(runtime/IntlObject.cpp:446) and its two copies.Fix
IntlAvailableLocalesholds the function that makes the set and calls it at the first lookup.new Intl.Segmenter()in bun: 12.6 million instructions to 0.42 million.Background
zh_Hant_TW). JavaScript uses a language tag (zh-Hant-TW). A service looks a requested tag up in the set of ICU's tags.Downsides
Date#toLocaleString()keeps 80%: 61.9 million to 49.8. [JSC] Name one time zone without listing every zone, and hold it as ICU's zone object #1086 removes 23 million of that.Notes
For a maintainer
USE(BUN_JSC_ADDITIONS)and has astress/bun-*test, in the fork's form. Should it go to WebKit/WebKit first, or stay here?intlSegmenterAvailableLocales()returnsintlAvailableLocales()) is shorter, and is wrong only on an ICU that has a list of its own for break iterators. Say if you prefer the alias.What changes
LocaleSet, the type that the 18 callers pass on (9initialize*, 9supportedLocalesOf), isIntlAvailableLocaleswhere Bun's additions are on. It has one operation,contains(), whichbestAvailableLocale()calls. No caller is edited.intlAvailableLocales(),intlCollatorAvailableLocales()andintlSegmenterAvailableLocales()of main under new names. In their bodies only the name of the set's type changes.std::call_once, as before. Two threads that find no pointer at the same time both call the function and store the same pointer.ubrk_getAvailable(i)returnsuloc_getAvailable(i)(common/ubrk.cpp). With commit 2 the segmenter's first lookup compares the two lists, pointer by pointer: 0.12 million instructions, once. When they are the same it takes the set of the list of locales. When they differ it makes its own set, as main does. Upstream said in 223609@main that the collator should be the only class with a list of its own.IntlCollator::checkICULocaleInvariantswhen the collator's set is made. That is now the collator's first lookup, not its first use. In bun's debug build a firstnew Intl.Collator()takes 1 ms, and a firstnew Intl.Collator("de")0.3 s. A debug build of bun from September takes 0.3 s for both (test(intl): run the locale children concurrently and assert the sweep once per tree bun#40961 measured 0.43 s).#if USE(BUN_JSC_ADDITIONS). On mainIntlObject.cpphas one. The other arm is main's code. It was built once, by hand: it gives main's answers and main's 906 conversions. No lane builds it.$vmreads them. One is written for each locale ID while a set is made (906 times for the list of locales). The other is written when a lookup finds no set (3 times in a process at most, but for a race).How the numbers were made
jscand bun are release builds without LTO, built twice in one tree: main is 499846f, bun is 98eae327f2. The branch is on 499846f and merges with main 59e584e without a conflict.perf_event_open, all threads), median of 9 runs, less the same count for an empty script: 4,844,888 and 949 minor page faults forjsc, 3,300,983 and 804 for bun. Each script is one statement.strcmptakes a longer path when one of its operands is near the end of a page. So a count is given for one address layout, or as the mean over the 256 positions that the stack can have in its page.src/icuon Linux, Windows, FreeBSD and Android. Itsuloc_toLanguageTagtakes 2,029 instructions for an ID of the list, where ICU4C takes about 12,400 (a harness with the crate of ff971d3766, not a bun). The pass that this PR skips is then about 3.4 million instructions, not 12: 1.8 million of conversions, and the 1.5 million that the rest of the pass takes. Itsubrk_getAvailablealso returns the strings ofuloc_getAvailable.First use
jsc, instructions / page faults over the empty script:Intl.DateTimeFormat.supportedLocalesOf([])new Intl.NumberFormat()new Intl.Segmenter()new Intl.Collator()"a".localeCompare("b")(1234.5).toLocaleString()new Intl.DateTimeFormat()new Date(0).toLocaleString()Intl.DateTimeFormat.supportedLocalesOf("!!"), which throwsIntl.DateTimeFormat.supportedLocalesOf("en")Intl.Segmenter.supportedLocalesOf("en")Intl.Collator.supportedLocalesOf("en")new Intl.NumberFormat("en")new Intl.DateTimeFormat("en")(1234.5).toLocaleString("en-US")supportedLocalesOf("en")of a formatter, then of the segmenterIntl.DateTimeFormat.supportedLocalesOf("en")is 12,208,241 instructions on main and 12,625,539 with this PR.strcmpis 417,346 more. The rest is 48 fewer: this PR's own code adds about 1,000 (the 906 writes of the counter, and the first call through the pointer), and the hash table of the set takes about 950 fewer at its other address.strcmpmoves with the layout. ICU's converter calls it in linear searches of its tables of codes, and this link put ICU's string constants at other addresses. As the mean over the 256 positions of the stack, the wholejscprocess is 17,056,398 instructions on main and 17,245,994 with this PR. In bun the two means are 15,726,875 and 15,721,247.bun, the same way:
Intl.DateTimeFormat.supportedLocalesOf([])new Intl.Segmenter()new Intl.NumberFormat()new Intl.Collator()"a".localeCompare("b")(1234.5).toLocaleString()new Intl.DateTimeFormat()new Date(0).toLocaleString()Intl.DateTimeFormat.supportedLocalesOf("en")new Intl.NumberFormat("en")Intl.Collator.supportedLocalesOf("en")supportedLocalesOf("en")of a formatter, then of the segmenterTime of the first call inside bun (
performance.now()around it, median of 201 interleaved runs, main measured twice):new Intl.Segmenter()"a".localeCompare("b")new Date(0).toLocaleString()Intl.DateTimeFormat.supportedLocalesOf("en")(401 runs)string-width7 callsnew Intl.Segmenter()at the top of its module.new Intl.Segmenter()on this machine: Node 26.3.0 is 92.8 million instructions over its empty script and 6.6 ms for the call. Deno 2.9.8 is 101.1 million.What ICU is asked, main to this PR:
uloc_toLanguageTagcallsIntl.DateTimeFormat.supportedLocalesOf([])new Intl.NumberFormat(),new Intl.Segmenter()new Intl.Collator(),"a".localeCompare("b")Intl.DateTimeFormat.supportedLocalesOf("en")The one call that is left with no requested locale converts the default locale.
The date rows
new Intl.DateTimeFormat()orDate#toLocaleString()with no locale keeps 49.7 million instructions of 61.8. About 23 million make the table of every time zone, which [JSC] Name one time zone without listing every zone, and hold it as ICU's zone object #1086 removes. 22,461,960 are ICU's load of its likely-subtags data (LikelySubtags::initLikelySubtags). The formatter itself is the rest.new Intl.DateTimeFormat(): 39.1 million with [JSC] Name one time zone without listing every zone, and hold it as ICU's zone object #1086 alone, 26.6 million with both.new Date(0).toLocaleString(): 39.0 and 26.7.(1234.5).toLocaleString(): 13.3 and 1.2. The tests of both pass on that build.Per call, after the first use
intlAvailableLocales()and the other two) is 5 instructions. On main it is 32: libstdc++'sstd::call_oncehas no inline check that the call was made.bestAvailableLocale()is 2 instructions more for each tag that it looks up: the load of the pointer to the set, and the test of it.supportedLocalesOf(tag)of 30 tags on the three lists (en,en-US,en-XX,zh-Hans-HK,Collatorwithja-JP,Segmenterwithxx, and 24 more): accessor and lookup together are 23 to 25 instructions fewer than on main for each.supportedLocales()for a list, with the accessor: 1 tag 2,687 to 2,662. 8 tags (11 lookups) 14,531 to 14,526. 24 tags (27 lookups) 37,058 to 37,085.Whole constructors, from the entry of the host function to its return. Each process first looks
enup in the list of the service, so that both builds have made one set:new Intl.DateTimeFormat("en")new Intl.DateTimeFormat()new Intl.DateTimeFormat("en", { dateStyle: "short" })new Intl.DateTimeFormat(undefined, { dateStyle: "short" })new Intl.NumberFormat("en")new Intl.NumberFormat()new Intl.Collator("en")new Intl.Collator()new Intl.Segmenter("en")new Intl.Segmenter()new Intl.DateTimeFormat("en")is answered from the cache of formatters and reaches neither the accessor nor a lookup.pthread_once, 2 more in the lookup.strcmpis 191 more, for the reason under "How the numbers were made", and the clock of the vDSO takes one more turn of its loop.Memory and size
.data.jsc:.text31,647,790 to 31,648,430 (+640),.rodata+64,.data+48,.bssthe same..text121,825,612 to 121,825,804 (+192),.data+48,.rodataand.bssthe same. The file is 152,819,656 bytes in both.Same results
resolvedOptions().localeof the nine services andsupportedLocalesOf()withlookupandbest fit, for every tag of ICU's three lists, with and without script and region, with extensions, and for tags that are not available) is the same as main's, byte for byte. Also with the default locale set tode-DE,zh-TW,sr-Latn-RS,ja-JP,ar-EGandxx-YY($vm.setUserPreferredLanguages), which changes 30,804 of the answers.--filteronintl,locale,collat,segmenter,toLocale,localeCompare): 2,492 runs, 0 failures. Notestaotwas built.What the test catches
stress/bun-intl-makes-no-set-of-locales-before-a-locale-is-looked-up.jsreads two counts at five points of one process:$vm.intlLocaleIDsPutIntoSets()and$vm.intlLookupsThatFoundNoSet(). They are 0 and 0 until a locale is looked up, then 906 and 1, 906 and 2 after the segmenter, 1,052 and 3 after the collator. The sizes are ICU 79's. With another ICU the test checks that a list has some IDs. It runs in 19 ms. On main it fails because the functions are missing. Each change below was built, and the test fails with the message given:locale IDs put into a set, before a locale is looked up: expected 0, got 1958... before a locale is looked up: expected 0, got 906... before a locale is looked up: expected 0, got 146... before a locale is looked up: expected 0, got 906... before the first use of Intl: expected 0, got 1052Intl.Collatoris first read... before a locale is looked up: expected 0, got 146the list of locales: 1052 locale IDs were put into a set, expected 906 with ICU 79the list of locales: 1812 locale IDs were put into a set, expected 906 with ICU 79locale IDs put into a set, after the lookups of the segmenter: expected 906, got 1812lookups that found no set, after the lookups of the services of ICU's list of locales: expected 1, got 81Segmenter: en-GB: expected en-GB, got enCollator: en-GB: expected en, got en-GBThe test passes in bun's debug build too, which has assertions.
LeakSanitizer and trimmed executables
scripts/runner.node.tsruns a test under LeakSanitizer: no report intest/js/web/intl/intl.test.ts,temporal.test.ts,bun-jsc.test.ts,v8-date-parser.test.jsand a test with 6 workers. No report either without the threeintllines oftest/leaksan.supp, which still match: the functions that make the sets keep the names in those lines as a prefix.intl.test.ts("default locale under C.UTF-8") takes about 5 s in that build on this machine and has a limit of 5 s. It fails in some runs, and with main's WebKit too.test/bundler/trim-runtime-icu/intl-services.test.ts: 17 pass. The sections that every trimmed executable keeps (retained.txtof the trimmable link) are 1,122 bytes more, 1,024 of them the trim table. ICU's list of collator locales is among them on main (localeCompareneeds it) and stays there. The other two lists are among them on neither.What is left: the first lookup of a locale
A first call that looks a locale up makes the set as main does: 906 conversions. About a third of those 12 million instructions are
strcmp, in the linear searches of ICU's converter. Ways to make the set cheaper, each built on this machine. User instructions over an empty script:Intl.DateTimeFormat.supportedLocalesOf("en")uloc_toLanguageTagcallsnew Intl.Segmenter()without this PRlanguageTagForAvailableLocale(), which remembers the subtags that ICU left alone-for_, and ICU converts every other IDIntlObject.cpp, this PR gives main's answers and passes its test. A formatter's lookup and then the segmenter's is 8.9 million with AOT: large programs that run in several realms, and smaller runtime fixes #1066 alone and 7.8 million with both: beside the memo, commit 2 saves 1.1 million.-for_until 203205@main (2018), which moved touloc_toLanguageTag.robobun/63841efd/intl-available-locales-respell(15206c7). It is one commit on top of this PR, with a test that asks ICU about every listed ID and fails with the IDs that differ.robobun/63841efd/intl-available-locales-index(ee56a70), an earlier form of this change. It is 213 source lines more than this PR and 0.78 million instructions ahead of the rule, once per process. A self-review of it said that it does not pay for its lines.Self-review
The 3 concerns that are not addressed: main's cost on macOS was not timed, for lack of a machine. The two ways under "Not built here" have no number from me.
Other pull requests
git merge-treeof the two heads has no conflict.bestAvailableLocale(intlCollatorAvailableLocales(), tag). It needs no edit and merges without a conflict.mainhas the same three functions, without the lines of the fork's AOT: per-global program state, mockable exports, heap-allocated generator frames, bug fixes #942.