Skip to content

Lower constant array.new_data in OptimizeInstructions - #9248

Open
gkdn wants to merge 1 commit into
WebAssembly:mainfrom
gkdn:lower-constant-array-new-data
Open

gkdn wants to merge 1 commit into
WebAssembly:mainfrom
gkdn:lower-constant-array-new-data

Conversation

@gkdn

@gkdn gkdn commented Oct 10, 2026

Copy link
Copy Markdown
Contributor

array.new_data is not a constant expression, which blocks optimizations
such as hoisting array literals into immutable globals, and passes like
Precompute, Heap2Local, GUFA and SimplifyGlobals don't understand it.

When the offset and size are constant and in bounds of a passive
segment, lower it to:

  • array.new_default, if all elements are zero
  • array.new, if all elements are equal
  • array.new_fixed, if there are at most 64 elements

Non-empty slices are only lowered with --traps-never-happen, since
array.new_data traps if the segment was dropped. An empty slice at
offset 0 can never trap, so it is always lowered.

The 64-element limit exists because array.new_fixed costs a full
*.const per element, while a data segment stores raw bytes. On large
J2Wasm applications this removes over 99.7% of passive data segments and
reduces binary size by ~1.8% (~1.3% gzipped).

array.new_data is not a constant expression, which blocks optimizations
such as hoisting array literals into immutable globals, and passes like
Precompute, Heap2Local, GUFA and SimplifyGlobals don't understand it.

When the offset and size are constant and in bounds of a passive
segment, lower it to:

  * array.new_default, if all elements are zero
  * array.new, if all elements are equal
  * array.new_fixed, if there are at most 64 elements

Non-empty slices are only lowered with --traps-never-happen, since
array.new_data traps if the segment was dropped. An empty slice at
offset 0 can never trap, so it is always lowered.

The 64-element limit exists because array.new_fixed costs a full
*.const per element, while a data segment stores raw bytes. On large
J2Wasm applications this removes over 99.7% of passive data segments and
reduces binary size by ~1.8% (~1.3% gzipped).
@gkdn
gkdn requested a review from a team as a code owner October 10, 2026 02:57
@gkdn
gkdn requested review from tlively and removed request for a team October 10, 2026 02:57

This branch has not been deployed

No deployments
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