Skip to content

Refactor String usage in wasmtime_environ::Module#12565

Merged
fitzgen merged 3 commits intobytecodealliance:mainfrom
fitzgen:use-oom-handling-string-in-env-module
Feb 12, 2026
Merged

Refactor String usage in wasmtime_environ::Module#12565
fitzgen merged 3 commits intobytecodealliance:mainfrom
fitzgen:use-oom-handling-string-in-env-module

Conversation

@fitzgen
Copy link
Member

@fitzgen fitzgen commented Feb 10, 2026

Do not hold regular Strings; instead use our own OOM-handling
wasmtime_core::alloc::String or, even better, an interned Atom from the
StringPool.

Also avoid IndexMap, as it doesn't handle OOMs.

Depends on

@fitzgen fitzgen requested review from a team as code owners February 10, 2026 22:18
@fitzgen fitzgen requested review from cfallin and removed request for a team February 10, 2026 22:18
@alexcrichton
Copy link
Member

Knee-jerk reaction before you have too many more PRs that depend on this -- IndexMap is a relatively fundamental dependency especially for components, so I'm not sure we'll be able to escape making an OOM friendly version. Or at least I recall my impression being that we have quite a lot of IndexMaps throughout compilation/etc. Is the usage pretty localized to just one or two locations though?

@fitzgen
Copy link
Member Author

fitzgen commented Feb 10, 2026

Knee-jerk reaction before you have too many more PRs that depend on this -- IndexMap is a relatively fundamental dependency especially for components, so I'm not sure we'll be able to escape making an OOM friendly version. Or at least I recall my impression being that we have quite a lot of IndexMaps throughout compilation/etc. Is the usage pretty localized to just one or two locations though?

IndexMap is either index_map::IndexMap or a BTreeMap depending on wasmparser configuration:

pub use wasmparser::collections::{IndexMap, IndexSet};

std/alloc does not provide a way to fallibly reserve/insert/allocate for BTreeMaps.

And index_map::IndexMap does not provide a try_reserve method either. Additionally, index_map::IndexMap's Deserialize implementation requires that the key be Clone, which our OOM-handling Strings are most definitely not.

So it seems like continuing to use IndexMap is going to be an uphill battle.

We could make our own IndexMap that is a full implementation, rather than simply a newtype wrapper over either of those existing implementations. But that also seems pretty annoying and like more of a maintenance burden than is ideal... But maybe not, given that what I did with exports here is basically inlining/hard-coding one specific instance of that?

Thoughts?

@alexcrichton
Copy link
Member

One possible fix might be to use indexmap::IndexMap (directly from the crate, not wasmparser). That should support no_std and it looks like it's got a try_reserve to work for us as well. For Deserialize wouldn't we be writing custom impls as opposed to using any built-in ones too?

I guess what I'm saying is that my leaning is that we should have an indexmap::IndexMap wrapper the same way we have a Vec wrapper and then replace the usage of wasmparser::IndexMap with that fallible IndexMap.

@github-actions github-actions bot added cranelift Issues related to the Cranelift code generator wasmtime:api Related to the API of the `wasmtime` crate itself labels Feb 11, 2026
@fitzgen fitzgen force-pushed the use-oom-handling-string-in-env-module branch from 17f2a45 to fa502c5 Compare February 12, 2026 19:50
@fitzgen
Copy link
Member Author

fitzgen commented Feb 12, 2026

I guess what I'm saying is that my leaning is that we should have an indexmap::IndexMap wrapper the same way we have a Vec wrapper and then replace the usage of wasmparser::IndexMap with that fallible IndexMap.

Okay, that has been done and I've pushed a new commit that switches this back to using IndexMap instead of multiple {Primary,Secondary}Maps.

Also this depends on these other PRs now:

Do not hold regular `String`s; instead use our own OOM-handling
`wasmtime_core::alloc::String` or, even better, an interned `Atom` from the
`StringPool`.

Also avoid `IndexMap`, as it doesn't handle OOMs.
@fitzgen fitzgen force-pushed the use-oom-handling-string-in-env-module branch from fa502c5 to a766277 Compare February 12, 2026 21:12
@fitzgen fitzgen enabled auto-merge February 12, 2026 21:13
@fitzgen fitzgen added this pull request to the merge queue Feb 12, 2026
Merged via the queue into bytecodealliance:main with commit 1f16b28 Feb 12, 2026
45 checks passed
@fitzgen fitzgen deleted the use-oom-handling-string-in-env-module branch February 12, 2026 22:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cranelift Issues related to the Cranelift code generator wasmtime:api Related to the API of the `wasmtime` crate itself

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants