Skip to content

Feature Request: Module Preprocessing for Resource-Constrained Targets #1637

@FedorSmirnov89

Description

@FedorSmirnov89

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:

  1. Preprocessing Phase: Parse and validate WebAssembly modules on a development machine or build server
  2. Serialization: Convert the parsed module to a compact, platform-independent binary format
  3. Lightweight Runtime: Deploy only the deserialization-capable Wasmi build to the target device
  4. 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 modules
  • deserialization feature for lightweight runtime builds
  • parser 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

  1. Does this approach align with Wasmi's architecture and design goals?
  2. Are there concerns about maintaining backward compatibility?
  3. What level of feature completeness should be targeted for the initial implementation?
  4. 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 :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions