-
Notifications
You must be signed in to change notification settings - Fork 315
Description
Overview
I'm requesting a feature to add module preparsing capabilities to Wasmi, enabling the creation of lightweight builds that can run on resource-constrained embedded systems without requiring the full parsing and validation overhead.
Use Case
I'm working on deploying Wasmi on an nRF53 (Nordic's dual-core Cortex-M33 chip) alongside Embassy async runtime and the BLE stack. The target system has approximately 1MB of total memory, and the current Wasmi implementation consumes around 480 KiB when combined with Embassy.
With a proof-of-concept implementation of the proposed preprocessing feature (see #1636 ), this footprint can be reduced to approximately 250 KiB - roughly a 48% reduction in memory usage.
Proposed Solution
The feature would add module serialization and deserialization capabilities to Wasmi, allowing:
- Preprocessing Phase: Parse and validate WebAssembly modules on a development machine or build server
- Serialization: Convert the parsed module to a compact, platform-independent binary format
- Lightweight Runtime: Deploy only the deserialization-capable Wasmi build to the target device
- Runtime Loading: Load preprocessed modules directly without parsing overhead
Benefits
- Memory Efficiency: Significant reduction in runtime memory footprint, making it possible to run wasmi on smaller devices
- Cross-Platform: Serialized modules can be created on one (resource-rich) platform and deployed on another, more constrained platform
- Feature-Gated: Can be enabled only when needed, maintaining Wasmi's modular architecture
Implementation Approach
I propose implementing this as a feature-gated part of the main Wasmi crate, providing:
serialization
feature for creating serialized modulesdeserialization
feature for lightweight runtime buildsparser
feature which is a default feature. By excluding this feature, we can create a wasmi build without the parsing/validation/translation machinery- Integration with Wasmi's existing module system
Current Status
I have implemented a proof-of-concept version that demonstrates the concept and provides concrete performance measurements (#1636 ). The implementation includes:
- Module serialization/deserialization using Postcard format
- Comprehensive test coverage
- Error handling for various failure modes
- Feature-gated compilation
Next Steps
I would be happy to work with the Wasmi maintainers to implement this feature properly, starting from scratch if needed. The current implementation is primarily intended to demonstrate the concept and gather feedback on the approach.
Example Implementation
I've created an example repository demonstrating the usage and performance benefits of this feature: Preparsing Example. The repository shows:
- How to use the serialization/deserialization APIs
- Memory footprint measurements
- Integration with embedded Rust applications
Questions for Discussion
- Does this approach align with Wasmi's architecture and design goals?
- Are there concerns about maintaining backward compatibility?
- What level of feature completeness should be targeted for the initial implementation?
- How should this integrate with Wasmi's existing feature flags and compilation modes?
I'm open to feedback and willing to adjust the approach based on maintainer guidance and community input. Thank you :)