Fix WASM memory allocation failure (#4989)#7887
Conversation
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.
📝 WalkthroughWalkthroughThis PR optimizes RustPython VM for WebAssembly targets by adding a dedicated ChangesWebAssembly Optimization
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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.
Built for teams:
One agent for your entire SDLC. Right inside Slack. 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. Comment |
|
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! |
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
.cargo/config.toml): Capped max memory at 64MB, stack at 1MB forwasm32-*targets.crates/vm/src/datastack.rs): ReducedMIN_CHUNK_SIZEto 4KB onwasm32targets.Cargo.toml): Addedwasm-releasewithopt-level="s", LTO, strip.Testing
without_stdlib) forwasm32-wasiwithwasm-releaseprofile.a = 1successfully under bothwasmtimeandwasmi— no memory errors, allocation stays under 64MB.Closes #4989
Summary by CodeRabbit