Skip to content

Add runtime detection for toResizableBuffer#27096

Open
sbc100 wants to merge 2 commits into
emscripten-core:mainfrom
sbc100:toResizableBuffer
Open

Add runtime detection for toResizableBuffer#27096
sbc100 wants to merge 2 commits into
emscripten-core:mainfrom
sbc100:toResizableBuffer

Conversation

@sbc100

@sbc100 sbc100 commented Jun 11, 2026

Copy link
Copy Markdown
Collaborator

Since this is now shipping in the current version of all major browsers
we want to take advantage of it whenever possible.

I am leaving the GROWABLE_ARRAYBUFFERS setting in place for now since
enabling it unconditionally can still save a little on codesize but we
may want to to just remove that at some point.

As a side effect this change also works around #27084.

@kleisauke

Copy link
Copy Markdown
Collaborator

I am leaving the GROWABLE_ARRAYBUFFERS setting in place for now since
enabling it unconditionally can still save a little on codesize but we
may want to to just remove that at some point.

A nice thing about -sGROWABLE_ARRAYBUFFERS is that it also avoids the emscripten_notify_memory_growth(0) call that would otherwise be triggered each time Wasm memory grows.

#if !defined(EMSCRIPTEN_PURE_WASI)
// Success, update JS (see https://github.com/WebAssembly/WASI/issues/82)
emscripten_notify_memory_growth(0);
#endif

It also skips the -sSHARED_MEMORY acorn pass, which adds some overhead too.

// Instrument heap accesses to call growMemViews helper function, which allows
// pthreads + memory growth to work (we check if the memory was grown on another thread
// in each access), see #8365.
function growableHeap(ast) {

Thinking about this more, since -sGROWABLE_ARRAYBUFFERS is a link-time setting only, how about making the -sGROWABLE_ARRAYBUFFERS setting internally and applying it automatically when targeting modern environments? For example, it could be enabled when someone builds with -sENVIRONMENT=node -sMIN_NODE_VERSION=260000 or -sENVIRONMENT=web -sMIN_CHROME_VERSION=144 -sMIN_FIREFOX_VERSION=145 -sMIN_SAFARI_VERSION=260200.

Feature.GROWABLE_ARRAYBUFFERS: {
'chrome': 144,
'firefox': 145,
'safari': 260200,
# Supported with flag --experimental-wasm-rab-integration (TODO: Change
# this to unflagged version of Node.js 260000, see also the comment in
# Feature.WASM_EXCEPTIONS above)
'node': 240000,
},

@sbc100

sbc100 commented Jun 11, 2026

Copy link
Copy Markdown
Collaborator Author

Yes, GROWABLE_ARRAYBUFFERS should certainly be auto-enabled when the feature matrix allows is. If that isn't the case today we should fix that.

@sbc100 sbc100 force-pushed the toResizableBuffer branch 2 times, most recently from 1ef543f to 70630b5 Compare June 11, 2026 17:49
sbc100 added a commit to sbc100/emscripten that referenced this pull request Jun 11, 2026
sbc100 added a commit to sbc100/emscripten that referenced this pull request Jun 11, 2026
@sbc100 sbc100 force-pushed the toResizableBuffer branch from 70630b5 to 7df7165 Compare June 11, 2026 21:11
sbc100 added a commit to sbc100/emscripten that referenced this pull request Jun 11, 2026
@sbc100 sbc100 force-pushed the toResizableBuffer branch from 7df7165 to 2781f78 Compare June 11, 2026 22:07
sbc100 added a commit to sbc100/emscripten that referenced this pull request Jun 11, 2026
@sbc100 sbc100 changed the title Add runtime detection for toResizableBuffer. NFC Add runtime detection for toResizableBuffer Jun 11, 2026
@sbc100 sbc100 force-pushed the toResizableBuffer branch from 2781f78 to e6316ef Compare June 12, 2026 03:49
sbc100 added a commit to sbc100/emscripten that referenced this pull request Jun 12, 2026
sbc100 added a commit to sbc100/emscripten that referenced this pull request Jun 12, 2026
sbc100 added a commit to sbc100/emscripten that referenced this pull request Jun 12, 2026
@sbc100 sbc100 force-pushed the toResizableBuffer branch 2 times, most recently from bda5ef7 to 6b6a0c6 Compare June 12, 2026 20:41
sbc100 added a commit that referenced this pull request Jun 12, 2026
…allow (#27097)

See
#27096 (comment)

Also, remove the experimental status, since this feature is now
available in all browser and we hope
to feature-detect it soon and start using it even without this flag.

Fixes: #26099
@sbc100 sbc100 force-pushed the toResizableBuffer branch 3 times, most recently from 86aa197 to 95d431f Compare June 12, 2026 22:10
@sbc100

sbc100 commented Jun 12, 2026

Copy link
Copy Markdown
Collaborator Author

I think this change should be good to go now. PTAL.

Comment thread src/settings.js
// and Node.js.
// and Node.js. Even when this setting is disabled, Emscripten will still use this
// feature when available, but it include the fall back code for handling
// non-growable memories.

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.

Perhaps explain why? We don't do this with other features afaik?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I think we do do feature detection in some places.

The reason we want both approaches in this case is kind of important. There are two main cases:

  1. For all users or pthreads+memory growth we probably want to use toResizableBuffer when available in all cases. It slightly fast since we don't need to rebuild the views each time the memory grows. There is very little cost to probing for, and using the features if its found.

  2. For users that know that they are only targetting newer browsers they can opt in with -sGROWABLE_ARRAYBUFFERS which means we don't need run the whole growableHeap JS optimizer path (which is the thing that slows down all the HEAP accesses in normal builder). These users see even more of a benefit, but their code won't run on older browsers.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

For example of where we do feature detection you can see many by doing git grep "if .*globalThis" src.

In most cases we don't have setting to opt out of the feature-detection and just assume the feature is present. In some cases we can use the feature matrix to detect when the feature-detection is not needed.

In the long run I think we may just want to delete this setting (or at least make it internal) since it can be driven completely by the feature matrix.

sbc100 added 2 commits June 13, 2026 11:47
Since this is now shipping in the current version of all major browsers
we want to take advantage of it whenever possible.

I am leaving the GROWABLE_ARRAYBUFFERS setting in place for now since
enabling it unconditionally can still save a little on codesize but we
may want to to just remove that at some point.

As a side effect this addresses emscripten-core#27084.
@sbc100 sbc100 force-pushed the toResizableBuffer branch from a65cb5c to 5988ea6 Compare June 13, 2026 19:51
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