Skip to content

[JSC] Make the set of available locales when a locale is first looked up in it - #1087

Open
robobun wants to merge 2 commits into
mainfrom
robobun/63841efd/intl-available-locales-at-first-lookup
Open

robobun wants to merge 2 commits into
mainfrom
robobun/63841efd/intl-available-locales-at-first-lookup

Conversation

@robobun

@robobun robobun commented Nov 21, 2026

Copy link
Copy Markdown
Collaborator

Problem

  • A process's first Intl constructor or supportedLocalesOf() converts every locale ID of an ICU list to a language tag. That is 906 uloc_toLanguageTag calls and 12 million instructions, or 146 and 1.4 million for the collator.
  • A call that requests no locale (new Intl.Segmenter(), localeCompare()) pays that and looks nothing up. The segmenter converts the list again.
  • The cause is intlAvailableLocales() (runtime/IntlObject.cpp:446) and its two copies.

Fix

  • Commit 1: IntlAvailableLocales holds the function that makes the set and calls it at the first lookup.
  • Commit 2: the segmenter shares the formatters' set when ICU's two lists are the same strings.
  • First new Intl.Segmenter() in bun: 12.6 million instructions to 0.42 million.
  • Verified: 1 new JSTest, 2,492 Intl JSTest runs, 90,893 answers identical to main, bun's Intl tests under LeakSanitizer. Self-reviewed: 27 concerns raised, 24 addressed.

Background

  • ICU names a locale by a locale ID (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.
  • Considered an index of ICU's IDs that converts nothing: 11.5 million instructions fewer on a first lookup, for 213 more source lines and a spelling rule.

Downsides

Notes

For a maintainer

  • Nothing in this change needs Bun. It is behind USE(BUN_JSC_ADDITIONS) and has a stress/bun-* test, in the fork's form. Should it go to WebKit/WebKit first, or stay here?
  • Commit 2 compares ICU's two lists when the segmenter first looks a locale up. An alias (intlSegmenterAvailableLocales() returns intlAvailableLocales()) 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.
  • Each commit lands alone. The test of commit 1 expects the segmenter's own set, and commit 2 changes that expectation.

What changes

  • LocaleSet, the type that the 18 callers pass on (9 initialize*, 9 supportedLocalesOf), is IntlAvailableLocales where Bun's additions are on. It has one operation, contains(), which bestAvailableLocale() calls. No caller is edited.
  • The object holds a pointer to the function that makes the set, and a pointer to the set once it is made. The three functions are intlAvailableLocales(), intlCollatorAvailableLocales() and intlSegmenterAvailableLocales() of main under new names. In their bodies only the name of the set's type changes.
  • Each of those functions makes its set under std::call_once, as before. Two threads that find no pointer at the same time both call the function and store the same pointer.
  • In ICU, ubrk_getAvailable(i) returns uloc_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.
  • A build with assertions runs IntlCollator::checkICULocaleInvariants when the collator's set is made. That is now the collator's first lookup, not its first use. In bun's debug build a first new Intl.Collator() takes 1 ms, and a first new 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).
  • The change adds 13 #if USE(BUN_JSC_ADDITIONS). On main IntlObject.cpp has 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.
  • Two counters are in every build, and only $vm reads 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).
  • A handle with no atomic was built too: a function for each list, with a function-local static. It costs 13 instructions for each tag that is looked up, where this costs 2 (24 tags: 37,382 against 37,085).

How the numbers were made

  • Linux x64, Debian 13, ICU 79.1 as bundled. jsc and 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.
  • Instructions are user instructions of the whole process (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 for jsc, 3,300,983 and 804 for bun. Each script is one statement.
  • Calls of ICU functions are gdb breakpoint counts. Instructions of one call are single steps from the entry of a function to its return, on the third identical call, with the JIT and ASLR off.
  • A count moves with the addresses of the process, also for code that did not change: glibc's strcmp takes 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.
  • Nothing was run on macOS or Windows. macOS links the system's ICU.
  • icu: replace the C++ libraries with the parts that Bun uses, in Rust bun#49369 replaces ICU4C's code with src/icu on Linux, Windows, FreeBSD and Android. Its uloc_toLanguageTag takes 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. Its ubrk_getAvailable also returns the strings of uloc_getAvailable.

First use

jsc, instructions / page faults over the empty script:

first Intl use of the process main this PR
Intl.DateTimeFormat.supportedLocalesOf([]) 12,270,283 / 62 30,161 / 20
new Intl.NumberFormat() 12,671,446 / 70 470,837 / 40
new Intl.Segmenter() 12,441,061 / 70 463,482 / 61
new Intl.Collator() 2,183,325 / 65 753,103 / 61
"a".localeCompare("b") 2,167,920 / 67 747,505 / 63
(1234.5).toLocaleString() 13,304,782 / 89 1,204,641 / 77
new Intl.DateTimeFormat() 61,782,877 / 586 49,731,129 / 567
new Date(0).toLocaleString() 61,867,777 / 590 49,851,219 / 570
Intl.DateTimeFormat.supportedLocalesOf("!!"), which throws 12,096,544 / 70 53,188 / 30
Intl.DateTimeFormat.supportedLocalesOf("en") 12,097,210 / 68 12,445,186 / 68
Intl.Segmenter.supportedLocalesOf("en") 12,148,421 / 63 12,500,469 / 66
Intl.Collator.supportedLocalesOf("en") 1,568,189 / 58 1,574,176 / 59
new Intl.NumberFormat("en") 12,529,603 / 74 12,877,802 / 79
new Intl.DateTimeFormat("en") 61,016,107 / 584 61,202,490 / 589
(1234.5).toLocaleString("en-US") 13,312,474 / 94 13,626,286 / 97
supportedLocalesOf("en") of a formatter, then of the segmenter 23,669,577 / 78 12,706,945 / 69
the same of a formatter, the segmenter and the collator 25,359,120 / 80 13,995,631 / 75
  • Three repeats of the table agree within 0.6 million on the rows where a set is made, and within 0.02 million on the other rows of this PR.
  • With commit 1 alone the last two rows are main's: the segmenter's lookup after a formatter's is 12.2 million instructions. With commit 2 it is 0.17 million.
  • A first lookup was counted function by function in one layout: Intl.DateTimeFormat.supportedLocalesOf("en") is 12,208,241 instructions on main and 12,625,539 with this PR. strcmp is 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.
  • strcmp moves 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 whole jsc process 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:

first Intl use of the process main this PR
Intl.DateTimeFormat.supportedLocalesOf([]) 12,557,248 / 88 88,278 / 60
new Intl.Segmenter() 12,629,630 / 94 424,379 / 77
new Intl.NumberFormat() 12,763,361 / 94 495,874 / 67
new Intl.Collator() 2,183,944 / 82 755,366 / 74
"a".localeCompare("b") 2,192,212 / 79 755,273 / 72
(1234.5).toLocaleString() 13,360,779 / 111 1,121,172 / 95
new Intl.DateTimeFormat() 58,666,096 / 630 46,682,782 / 619
new Date(0).toLocaleString() 59,160,837 / 632 46,784,010 / 619
Intl.DateTimeFormat.supportedLocalesOf("en") 12,250,343 / 90 12,355,018 / 87
new Intl.NumberFormat("en") 12,787,312 / 98 12,682,019 / 97
Intl.Collator.supportedLocalesOf("en") 1,591,239 / 72 1,593,817 / 72
supportedLocalesOf("en") of a formatter, then of the segmenter 24,108,520 / 99 12,379,904 / 90
the same of a formatter, the segmenter and the collator 25,772,575 / 96 13,825,827 / 88

Time of the first call inside bun (performance.now() around it, median of 201 interleaved runs, main measured twice):

first call main this PR
new Intl.Segmenter() 936 and 938 µs 172 µs
"a".localeCompare("b") 292 and 297 µs 199 µs
new Date(0).toLocaleString() 4,698 and 4,730 µs 3,996 µs
Intl.DateTimeFormat.supportedLocalesOf("en") (401 runs) 858 and 859 µs 857 and 853 µs
  • string-width 7 calls new Intl.Segmenter() at the top of its module.
  • Other engines, the same 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_toLanguageTag calls static strings made
Intl.DateTimeFormat.supportedLocalesOf([]) 906 to 0 950 to 0
new Intl.NumberFormat(), new Intl.Segmenter() 907 to 1 950 to 0
new Intl.Collator(), "a".localeCompare("b") 147 to 1 155 to 0
Intl.DateTimeFormat.supportedLocalesOf("en") 907 to 907 950 to 950
formatter, then segmenter 1,813 to 907 1,900 to 950
formatter, segmenter and collator 1,959 to 1,053 2,055 to 1,105

The one call that is left with no requested locale converts the default locale.

The date rows

Per call, after the first use

  • The accessor (intlAvailableLocales() and the other two) is 5 instructions. On main it is 32: libstdc++'s std::call_once has 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, Collator with ja-JP, Segmenter with xx, 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 en up in the list of the service, so that both builds have made one set:

third identical call main this PR
new Intl.DateTimeFormat("en") 1,135 1,135
new Intl.DateTimeFormat() 1,084 1,084
new Intl.DateTimeFormat("en", { dateStyle: "short" }) 276,549 276,758
new Intl.DateTimeFormat(undefined, { dateStyle: "short" }) 283,780 283,947
new Intl.NumberFormat("en") 25,399 25,374
new Intl.NumberFormat() 22,959 22,932
new Intl.Collator("en") 14,634 14,609
new Intl.Collator() 12,442 12,415
new Intl.Segmenter("en") 33,906 33,892
new Intl.Segmenter() 33,506 33,490
  • new Intl.DateTimeFormat("en") is answered from the cache of formatters and reaches neither the accessor nor a lookup.
  • The third row was counted function by function. JavaScriptCore and WTF are 25 fewer: 27 fewer in the accessor with pthread_once, 2 more in the lookup. strcmp is 191 more, for the reason under "How the numbers were made", and the clock of the vDSO takes one more turn of its loop.
  • For the segmenter the set is another object than on main, with another seed of its hash table, so its lookups differ by a few instructions.

Memory and size

  • A process that looks no locale up keeps no set. Main keeps 951 blocks, about 32 KB, for the list of locales, and the same again for the segmenter.
  • Each of the three objects is 16 bytes of .data.
  • jsc: .text 31,647,790 to 31,648,430 (+640), .rodata +64, .data +48, .bss the same.
  • bun: .text 121,825,612 to 121,825,804 (+192), .data +48, .rodata and .bss the same. The file is 152,819,656 bytes in both.

Same results

  • A corpus of 90,893 answers (resolvedOptions().locale of the nine services and supportedLocalesOf() with lookup and best 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 to de-DE, zh-TW, sr-Latn-RS, ja-JP, ar-EG and xx-YY ($vm.setUserPreferredLanguages), which changes 30,804 of the answers.
  • Eleven outputs for each of the 906 locales (dates, numbers, names, lists, plural rules, sorting, segments, durations) are the same as main's.
  • The Intl part of the JSTests (--filter on intl, locale, collat, segmenter, toLocale, localeCompare): 2,492 runs, 0 failures. No testaot was built.

What the test catches

stress/bun-intl-makes-no-set-of-locales-before-a-locale-is-looked-up.js reads 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:

change message
main's behaviour: each accessor makes its set, the segmenter has its own locale IDs put into a set, before a locale is looked up: expected 0, got 1958
the accessor of the list of locales makes the set ... before a locale is looked up: expected 0, got 906
the accessor of the collator makes the set ... before a locale is looked up: expected 0, got 146
the accessor of the segmenter makes the set ... before a locale is looked up: expected 0, got 906
the Intl object makes two sets when it is made ... before the first use of Intl: expected 0, got 1052
the collator's set is made when Intl.Collator is first read ... before a locale is looked up: expected 0, got 146
the first lookup in the list of locales also makes the collator's set the list of locales: 1052 locale IDs were put into a set, expected 906 with ICU 79
the first lookup in the list of locales also makes a set for the segmenter the list of locales: 1812 locale IDs were put into a set, expected 906 with ICU 79
the segmenter makes a set of its own locale IDs put into a set, after the lookups of the segmenter: expected 906, got 1812
the pointer to the set is never stored lookups that found no set, after the lookups of the services of ICU's list of locales: expected 1, got 81
the segmenter has the collator's set Segmenter: en-GB: expected en-GB, got en
the collator has the set of the list of locales Collator: en-GB: expected en, got en-GB

The test passes in bun's debug build too, which has assertions.

LeakSanitizer and trimmed executables

  • bun's debug build with AddressSanitizer and this WebKit, run as scripts/runner.node.ts runs a test under LeakSanitizer: no report in test/js/web/intl/intl.test.ts, temporal.test.ts, bun-jsc.test.ts, v8-date-parser.test.js and a test with 6 workers. No report either without the three intl lines of test/leaksan.supp, which still match: the functions that make the sets keep the names in those lines as a prefix.
  • One test of 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.txt of 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 (localeCompare needs 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:

how a tag is made of an ID first Intl.DateTimeFormat.supportedLocalesOf("en") uloc_toLanguageTag calls new Intl.Segmenter() without this PR with this PR
main: ICU converts every ID 12.2 million 907 12.6 million 0.46 million
#1066's languageTagForAvailableLocale(), which remembers the subtags that ICU left alone 7.7 million 541 8.0 million 0.46 million
a rule: an ID that is a language, a script and a region has the tag with - for _, and ICU converts every other ID 1.5 million 2 1.9 million 0.47 million
an index of the IDs in one block of characters, with the same rule, and no string or set 0.74 million 2 0.46 million

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

… 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.
@robobun

robobun commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator Author

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):

  • jsc runs new Intl.Segmenter(), and an empty script. The first one takes 12.4 million more user instructions (perf_event_open, median of 9 runs). In bun it is 12.6 million more, and the call takes 0.94 ms.
  • gdb counts in that run: uloc_toLanguageTag 907, StringImpl::createStaticStringImpl 950. For "a".localeCompare("b") they are 147 and 155.

With this PR: 0.46 million instructions in jsc and 0.42 million in bun, 0.17 ms for the call, 1 call of uloc_toLanguageTag, no static string. A first call that looks a locale up costs what it cost: 12.2 million. The answers of the nine services to 90,893 questions are identical to main's.

Not run here: macOS and Windows. The lanes of this PR compile both.

@github-actions

Copy link
Copy Markdown

Preview build of 03a0af7: autobuild-preview-pr-1087-03a0af7a

@robobun
robobun marked this pull request as ready for review November 21, 2026 23:52

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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 first new Intl.Collator() or localeCompare() 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 in intlCollatorAvailableLocalesSet() (IntlObject.cpp:629-652), reached only through contains() (IntlObject.h:110-116). With an empty requestedLocales no lookup occurs, so the check never runs.

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.

1 participant