Skip to content

[JSC] Array profiles should not turn ordinary arrays into ArrayStorage - #75376

Open
sosukesuzuki wants to merge 1 commit into
WebKit:mainfrom
sosukesuzuki:eng/array-profiles-should-not-select-array-storage
Open

sosukesuzuki wants to merge 1 commit into
WebKit:mainfrom
sosukesuzuki:eng/array-profiles-should-not-select-array-storage

Conversation

@sosukesuzuki

@sosukesuzuki sosukesuzuki commented Sep 30, 2026 •

Copy link
Copy Markdown
Member

67fc02a

[JSC] Array profiles should not turn ordinary arrays into `ArrayStorage`
https://bugs.webkit.org/show_bug.cgi?id=325781

Reviewed by NOBODY (OOPS!).

Freezing an array turns it into ArrayStorage. After that, two profiles
start to turn ordinary arrays into ArrayStorage, and those arrays miss most
of the array fast paths.

1. ArrayMode::fromObserved picks ArrayStorage with Array::Convert for a site
   that has seen both Contiguous and ArrayStorage. Arrayify then converts
   every array that comes to the site.
2. ArrayAllocationProfile picks ArrayStorage for a site when the last array
   allocated there has become ArrayStorage.

The array builtins share their profiles in a realm. Passing one frozen array
to `every`, or freezing one result of `map`, affects every caller.

This patch stops that.

1. ArrayMode::fromObserved ignores ArrayStorage when the site has seen
   Undecided, Int32, Double or Contiguous too. If an ArrayStorage array then
   fails the check in optimized code, ArrayProfile remembers it and the next
   compilation uses Array::Generic.
2. ArrayAllocationProfile ignores the last array when it has become
   ArrayStorage. The site keeps allocating arrays as before, and only the
   arrays that need ArrayStorage are converted later.
3. Arrayify exits with SparseIndex instead of Uncountable when the index is
   sparse, and ArrayMode::refine uses Array::Generic for the site in the
   next compilation. A site that writes to a sparse index did not reach this
   exit when Arrayify converted the array to ArrayStorage first. The other
   sites already kept exiting here.

Both profiles still pick SlowPutArrayStorage as before. Every array has it
when having a bad time.

Some JetStream 3 tests hit this. Counts in one run, before -> after:

                      Arrayify to ArrayStorage  Allocated as ArrayStorage
    proxy-mobx                      23306 -> 0                219027 -> 0
    babel-wtb                       53886 -> 0                 21265 -> 0
    jsdom-d3-startup                 3011 -> 0                 20834 -> 0

These tests were neutral when I ran them locally.

Tests: JSTests/stress/array-allocation-profile-should-not-select-array-storage.js
       JSTests/stress/array-should-not-be-converted-to-array-storage-by-frequent-frozen-arrays.js
       JSTests/stress/array-should-not-be-converted-to-array-storage-by-frozen-array.js
       JSTests/stress/array-without-elements-should-not-be-converted-to-array-storage-by-frozen-array.js
       JSTests/stress/arrayify-should-not-keep-exiting-for-sparse-index.js

* JSTests/stress/array-allocation-profile-should-not-select-array-storage.js: Added.
(shouldBe):
(createWithPush):
(createWithLiteral):
(createWithNewArray):
(createWithMap):
(createDoubleWithPush):
(createInt32WithNewArray):
(createInt32WithConstantLiteral):
(createEmpty):
(test):
* JSTests/stress/array-should-not-be-converted-to-array-storage-by-frequent-frozen-arrays.js: Added.
(shouldBe):
(create):
(createFrozen):
(isObject):
(every):
(some):
(filter):
(forEach):
(at):
(indexed):
(forOf):
(destructure):
(store):
(push):
(test):
* JSTests/stress/array-should-not-be-converted-to-array-storage-by-frozen-array.js: Added.
(shouldBe):
(create):
(createFrozen):
(isObject):
(every):
(some):
(filter):
(forEach):
(at):
(indexed):
(forOf):
(destructure):
(store):
(push):
(test):
* JSTests/stress/array-without-elements-should-not-be-converted-to-array-storage-by-frozen-array.js: Added.
(shouldBe):
(shouldNotBeCompiledRepeatedly):
(first):
(indexed):
(forOf):
(destructure):
(store):
(push):
(test):
* JSTests/stress/arrayify-should-not-keep-exiting-for-sparse-index.js: Added.
(shouldBe):
(shouldNotBeCompiledRepeatedly):
(putAfterOptimized):
(putOncePerArray):
(putToArrayBecomingSparse):
(read):
* Source/JavaScriptCore/bytecode/ArrayAllocationProfile.cpp:
(JSC::ArrayAllocationProfile::updateProfile):
* Source/JavaScriptCore/bytecode/ArrayProfile.cpp:
(JSC::ArrayProfile::computeUpdatedPrediction):
* Source/JavaScriptCore/bytecode/ArrayProfile.h:
(JSC::ArrayProfile::removeObservedArrayModes):
(JSC::ArrayProfile::speculationFailedOnArrayStorage const):
* Source/JavaScriptCore/bytecode/ExitKind.h:
* Source/JavaScriptCore/dfg/DFGArrayMode.cpp:
(JSC::DFG::ArrayMode::fromObserved):
(JSC::DFG::ArrayMode::refine const):
* Source/JavaScriptCore/dfg/DFGArrayifySlowPathGenerator.h:
* Source/JavaScriptCore/dfg/DFGByteCodeParser.cpp:
(JSC::DFG::ByteCodeParser::handleIteratorNext):
* Source/JavaScriptCore/ftl/FTLLowerDFGToB3.cpp:
(JSC::FTL::DFG::LowerDFGToB3::compileArrayify):

67fc02a

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

@sosukesuzuki
sosukesuzuki requested a review from a team as a code owner September 30, 2026 07:36
@sosukesuzuki sosukesuzuki self-assigned this Sep 30, 2026
@sosukesuzuki sosukesuzuki added the JavaScriptCore For bugs in JavaScriptCore, the JS engine used by WebKit, other than kxmlcore issues. label Sep 30, 2026
@webkit-ews-buildbot webkit-ews-buildbot added the merging-blocked Applied to prevent a change from being merged label Sep 30, 2026
@sosukesuzuki
sosukesuzuki marked this pull request as draft October 1, 2026 13:18
https://bugs.webkit.org/show_bug.cgi?id=325781

Reviewed by NOBODY (OOPS!).

Freezing an array turns it into ArrayStorage. After that, two profiles
start to turn ordinary arrays into ArrayStorage, and those arrays miss most
of the array fast paths.

1. ArrayMode::fromObserved picks ArrayStorage with Array::Convert for a site
   that has seen both Contiguous and ArrayStorage. Arrayify then converts
   every array that comes to the site.
2. ArrayAllocationProfile picks ArrayStorage for a site when the last array
   allocated there has become ArrayStorage.

The array builtins share their profiles in a realm. Passing one frozen array
to `every`, or freezing one result of `map`, affects every caller.

This patch stops that.

1. ArrayMode::fromObserved ignores ArrayStorage when the site has seen
   Undecided, Int32, Double or Contiguous too. If an ArrayStorage array then
   fails the check in optimized code, ArrayProfile remembers it and the next
   compilation uses Array::Generic.
2. ArrayAllocationProfile ignores the last array when it has become
   ArrayStorage. The site keeps allocating arrays as before, and only the
   arrays that need ArrayStorage are converted later.
3. Arrayify exits with SparseIndex instead of Uncountable when the index is
   sparse, and ArrayMode::refine uses Array::Generic for the site in the
   next compilation. A site that writes to a sparse index did not reach this
   exit when Arrayify converted the array to ArrayStorage first. The other
   sites already kept exiting here.

Both profiles still pick SlowPutArrayStorage as before. Every array has it
when having a bad time.

Some JetStream 3 tests hit this. Counts in one run, before -> after:

                      Arrayify to ArrayStorage  Allocated as ArrayStorage
    proxy-mobx                      23306 -> 0                219027 -> 0
    babel-wtb                       53886 -> 0                 21265 -> 0
    jsdom-d3-startup                 3011 -> 0                 20834 -> 0

These tests were neutral when I ran them locally.

Tests: JSTests/stress/array-allocation-profile-should-not-select-array-storage.js
       JSTests/stress/array-should-not-be-converted-to-array-storage-by-frequent-frozen-arrays.js
       JSTests/stress/array-should-not-be-converted-to-array-storage-by-frozen-array.js
       JSTests/stress/array-without-elements-should-not-be-converted-to-array-storage-by-frozen-array.js
       JSTests/stress/arrayify-should-not-keep-exiting-for-sparse-index.js

* JSTests/stress/array-allocation-profile-should-not-select-array-storage.js: Added.
(shouldBe):
(createWithPush):
(createWithLiteral):
(createWithNewArray):
(createWithMap):
(createDoubleWithPush):
(createInt32WithNewArray):
(createInt32WithConstantLiteral):
(createEmpty):
(test):
* JSTests/stress/array-should-not-be-converted-to-array-storage-by-frequent-frozen-arrays.js: Added.
(shouldBe):
(create):
(createFrozen):
(isObject):
(every):
(some):
(filter):
(forEach):
(at):
(indexed):
(forOf):
(destructure):
(store):
(push):
(test):
* JSTests/stress/array-should-not-be-converted-to-array-storage-by-frozen-array.js: Added.
(shouldBe):
(create):
(createFrozen):
(isObject):
(every):
(some):
(filter):
(forEach):
(at):
(indexed):
(forOf):
(destructure):
(store):
(push):
(test):
* JSTests/stress/array-without-elements-should-not-be-converted-to-array-storage-by-frozen-array.js: Added.
(shouldBe):
(shouldNotBeCompiledRepeatedly):
(first):
(indexed):
(forOf):
(destructure):
(store):
(push):
(test):
* JSTests/stress/arrayify-should-not-keep-exiting-for-sparse-index.js: Added.
(shouldBe):
(shouldNotBeCompiledRepeatedly):
(putAfterOptimized):
(putOncePerArray):
(putToArrayBecomingSparse):
(read):
* Source/JavaScriptCore/bytecode/ArrayAllocationProfile.cpp:
(JSC::ArrayAllocationProfile::updateProfile):
* Source/JavaScriptCore/bytecode/ArrayProfile.cpp:
(JSC::ArrayProfile::computeUpdatedPrediction):
* Source/JavaScriptCore/bytecode/ArrayProfile.h:
(JSC::ArrayProfile::removeObservedArrayModes):
(JSC::ArrayProfile::speculationFailedOnArrayStorage const):
* Source/JavaScriptCore/bytecode/ExitKind.h:
* Source/JavaScriptCore/dfg/DFGArrayMode.cpp:
(JSC::DFG::ArrayMode::fromObserved):
(JSC::DFG::ArrayMode::refine const):
* Source/JavaScriptCore/dfg/DFGArrayifySlowPathGenerator.h:
* Source/JavaScriptCore/dfg/DFGByteCodeParser.cpp:
(JSC::DFG::ByteCodeParser::handleIteratorNext):
* Source/JavaScriptCore/ftl/FTLLowerDFGToB3.cpp:
(JSC::FTL::DFG::LowerDFGToB3::compileArrayify):
@sosukesuzuki sosukesuzuki removed the merging-blocked Applied to prevent a change from being merged label Oct 1, 2026
@sosukesuzuki
sosukesuzuki force-pushed the eng/array-profiles-should-not-select-array-storage branch from 8fef23c to 67fc02a Compare October 1, 2026 13:56
@webkit-early-warning-system

Copy link
Copy Markdown
Collaborator

Starting EWS tests for 67fc02a. Live statuses available at the PR page, #75376

@sosukesuzuki
sosukesuzuki marked this pull request as ready for review October 3, 2026 04:04
sosukesuzuki added a commit to oven-sh/WebKit that referenced this pull request Oct 14, 2026
…kit-75376

[JSC] Array profiles should not turn ordinary arrays into `ArrayStorage` (cherry-pick WebKit#75376)
Jarred-Sumner added a commit to oven-sh/bun that referenced this pull request Oct 15, 2026
…rchy exists beside it (#45272)

### What does this PR do?

Bumps WebKit to `79f27e1945be`, the merge commit of oven-sh/WebKit#808.

#### Bun did not see the limits of cgroup v2 on a host that also has a
cgroup v1 hierarchy

`navigator.hardwareConcurrency`, `os.availableParallelism()`, the number
of threads that Bun starts (`bun fmt`, `bun lint`, `bun check`, `bun
install`, the thread pool), `process.constrainedMemory()` and the memory
that JavaScriptCore sizes its heap by were those of the whole machine.

Whether a host has v1 or v2 was decided by the first line of
`/proc/self/cgroup`. The kernel lists v1 hierarchies first, so one of
them was enough, even a named one that has no controllers:

```
1:name=systemd:/
0::/system.slice/a.service
```

Under `systemd-run --scope -p CPUQuota=200% -p MemoryMax=1G`, on a
machine with 64 cores and such a file:

| | before | after |
|---|---|---|
| `navigator.hardwareConcurrency` | 64 | 2 |
| `os.availableParallelism()` | 64 | 2 |
| `process.constrainedMemory()` | 265981812736 | 1073741824 |

Each controller is now read from v2 if there is a `0::/` line and no v1
line names it. A host with only v2, with only v1, or with systemd's
hybrid layout gets the same answers as before.

A v1 host where memory is mounted together with another controller
(`3:cpu,cpuacct,memory:/a`) did not find the limit of its group either,
and does now.

#### What else the bump brings

One change that is on the main branch of oven-sh/WebKit and not in the
build that Bun pins: oven-sh/WebKit#785, "[JSC] Array profiles should
not turn ordinary arrays into `ArrayStorage`", a cherry-pick of
WebKit/WebKit#75376.

### How did you verify your code works?

- New cases next to `process.constrainedMemory()` in
`test/js/node/process/process.test.js`: six layouts, each in a user and
mount namespace of its own, where a made-up directory is
`/sys/fs/cgroup` and a made-up file is `/proc/self/cgroup`. They need no
root, and are skipped where the host does not allow such a namespace.
Before the bump the four layouts that always worked pass and the two
above fail. In CI they run on Debian 13 (x64, aarch64, ASAN) and Alpine
3.23 (x64, aarch64), and are skipped on Ubuntu 25.04.
- The numbers in the table, with real limits from systemd. Without the
two properties it is 64, 64 and 265981812736 before and after.
`os.cpus().length` is 64 in every case.
- The rest of `process.test.js` gives the same results as on main.
- oven-sh/WebKit#808 has a table of thirteen layouts for the file
compiled on its own.
Comment on lines +59 to +60
// An array that has become ArrayStorage does not tell us which type it had, so we learn nothing from it.
if (!lastArray || hasArrayStorage(lastArray->indexingType()))

@chicoxyzzy chicoxyzzy Oct 15, 2026 •

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.

hasArrayStorage is only ArrayStorageShape. A slow-put array is a higher shape, and leastUpperBoundOfIndexingTypes is std::max, so that sample still replaces an Int32, Double, or Contiguous profile. hasAnyArrayStorage covers both.

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

Labels

JavaScriptCore For bugs in JavaScriptCore, the JS engine used by WebKit, other than kxmlcore issues.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants