Skip to content

Fix WASM memory allocation failure (#4989)#7887

Open
alok-108 wants to merge 1 commit into
RustPython:mainfrom
alok-108:fix-wasm-memory-usage
Open

Fix WASM memory allocation failure (#4989)#7887
alok-108 wants to merge 1 commit into
RustPython:mainfrom
alok-108:fix-wasm-memory-usage

Conversation

@alok-108
Copy link
Copy Markdown
Contributor

@alok-108 alok-108 commented May 16, 2026

Problem

Even a minimal script (a = 1) caused a memory allocation failure (~79MB) in WASM builds on constrained runtimes (wasmi). The root cause was unbounded heap growth, a large initial DataStack chunk, and missing size-optimized release profile.

Fixes

  1. Linker memory limits (.cargo/config.toml): Capped max memory at 64MB, stack at 1MB for wasm32-* targets.
  2. DataStack optimization (crates/vm/src/datastack.rs): Reduced MIN_CHUNK_SIZE to 4KB on wasm32 targets.
  3. WASM release profile (Cargo.toml): Added wasm-release with opt-level="s", LTO, strip.

Testing

  • Built minimal interpreter (without_stdlib) for wasm32-wasi with wasm-release profile.
  • Executed a = 1 successfully under both wasmtime and wasmi — no memory errors, allocation stays under 64MB.
  • All existing VM tests pass without regression.

Closes #4989

Summary by CodeRabbit

  • Chores
    • Optimized WebAssembly build configuration with enhanced linker settings for WASI targets.
    • Added optimized build profile for WebAssembly with size and performance tuning.
    • Improved memory allocation efficiency for WebAssembly builds.

Review Change Stack

This commit addresses the unbounded memory allocation issue on WASM targets that caused minimal programs (like � = 1) to fail with ~79MB allocation errors in constrained runtimes like wasmi.

The fixes include:
1. Capping the max memory limit at 64MB and restricting stack size to 1MB via linker flags in .cargo/config.toml.
2. Reducing the minimum chunk size of the DataStack from 16KB to 4KB on WASM targets to shrink the initial memory footprint.
3. Adding a wasm-release profile in the root Cargo.toml optimized for size with opt-level = s, LTO enabled, and symbols stripped.
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 16, 2026

📝 Walkthrough

Walkthrough

This PR optimizes RustPython VM for WebAssembly targets by adding a dedicated wasm-release build profile, configuring linker flags for WASI targets to constrain memory and stack, and reducing the minimum data-stack chunk size for wasm32 architecture to fit tighter memory constraints.

Changes

WebAssembly Optimization

Layer / File(s) Summary
Build profile for WASM
Cargo.toml
New wasm-release profile inherits from release and tunes opt-level to "s", enables lto = true, sets codegen-units = 1, enables strip = true, and configures panic = "abort" for efficient wasm binaries.
Linker configuration for WASI targets
.cargo/config.toml
New [target.wasm32-wasip1] and [target.wasm32-wasip2] sections pass linker arguments via rustflags to cap maximum memory and configure stack size limits.
Runtime memory allocation for wasm32
crates/vm/src/datastack.rs
MIN_CHUNK_SIZE constant is conditionally set to 4 * 1024 for wasm32 targets and 16 * 1024 for other targets, reducing per-chunk memory overhead in constrained wasm environments.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐰 A rabbit hops through config lands,
Where wasm optimization stands,
Stack sizes shrink, profiles take flight,
WebAssembly shines so bright!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main objective: fixing WASM memory allocation failures through linker flags, reduced chunk sizes, and optimized build profile.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Tip

💬 Introducing Slack Agent: The best way for teams to turn conversations into code.

Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@alok-108
Copy link
Copy Markdown
Contributor Author

Hi @youknowone, this PR fixes the old WASM memory issue (#4989). All 26 CI checks passed including wasm-wasi tests. Could you please take a look when you have time? Thanks!

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.

wasi build takes unexpectedly huge memory

1 participant