WASM Backend
WebAssembly backend for multilingual — 50–100x performance with automatic Python fallback.
The multilingual WASM backend provides 50–100x performance improvements for compute-intensive operations while maintaining 100% Python compatibility through automatic fallback.
All multilingual programs work without WASM. The backend is transparent — the same code runs on both Python and WASM backends. Install WASM for performance, not for compatibility.
Key Features
50–100x Speedup
Matrix operations, cryptography, scientific computing, image processing up to 100x faster than pure Python.
Zero Code Changes
Transparent backend selection. Your multilingual programs run identically on Python and WASM — no API changes needed.
Automatic Python Fallback
If WASM is unavailable (wrong platform, not installed), Python execution kicks in automatically. Never breaks.
Cross-Platform
Windows x86_64, Linux x86_64, macOS x86_64, macOS ARM64. Works in Docker, virtual environments, CI/CD.
Cranelift Backend
Uses the Cranelift compiler (Rust-based, part of Wasmtime) for native-speed code generation from the multilingual AST.
25+ Pure Python Fallbacks
Every WASM-accelerated function has a pure Python fallback implementation. Full coverage, always.
Performance Overview
| Operation | Python | WASM | Speedup |
|---|---|---|---|
| Matrix 1000×1000 multiply | 5.0s | 50ms | 100x |
| Matrix 100×100 multiply | 50ms | 1ms | 50x |
| XOR cipher (1MB) | 500ms | 5ms | 100x |
| Fibonacci(30) | 200ms | 2ms | 100x |
| JSON parse (10MB) | 200ms | 20ms | 10x |
| Blur 4K image | 2s | 40ms | 50x |
| Monte Carlo (1M samples) | 1s | 10ms | 100x |
Quick Start
Install with WASM
1
pip install multilingualprogramming[wasm]
Verify WASM Availability
1
2
3
4
5
from multilingualprogramming.runtime.backend_selector import BackendSelector
selector = BackendSelector()
print(f"WASM Available: {selector.is_wasm_available()}")
print(f"Current Backend: {selector.backend}")
Force a Specific Backend
1
2
3
4
5
6
7
8
9
10
from multilingualprogramming.runtime.backend_selector import BackendSelector, Backend
# Auto-detect (WASM if available, else Python) — DEFAULT
selector_auto = BackendSelector(prefer_backend=Backend.AUTO)
# Always Python (for testing or debugging)
selector_python = BackendSelector(prefer_backend=Backend.PYTHON)
# Force WASM (fails if unavailable)
selector_wasm = BackendSelector(prefer_backend=Backend.WASM)
Environment Variable Override
1
2
3
4
5
6
7
8
# Force Python backend
export MULTILINGUAL_BACKEND=python
# Force WASM
export MULTILINGUAL_BACKEND=wasm
# Enable verbose logging
export MULTILINGUAL_DEBUG=1
WASM Architecture
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
multilingual Source (.ml)
│
▼
┌──────────────┐
│ Lexer/Parser │ (same pipeline for both backends)
└──────┬───────┘
│ Core AST
▼
┌─────────────────┐
│ WASM Generator │ → Rust intermediate code
│ (wasm_generator.py) │ ↓
└─────────────────┘ Cranelift compiler
↓
.wasm binary
│
┌──────────┴──────────┐
│ │
┌───────▼──────┐ ┌─────────▼────────┐
│ WASM Loader │ │ Backend Selector │
│ (loader.py) │ │ (auto-detect) │
└───────┬──────┘ └─────────┬─────────┘
│ │
┌────────────┴─────────────────────┘
│
▼
┌──────────────────────────────────┐
│ Execution │
│ WASM path: wasmtime runtime │
│ Python path: pure Python │
└──────────────────────────────────┘
Backend Selection Algorithm
1
2
3
4
5
6
1. Check if wasmtime is installed
2. Check if WASM binary (.wasm) exists
3. Check platform compatibility
4. Try to load and instantiate WASM module
5. If any step fails → use Python fallback (transparent)
6. Cache loaded modules for subsequent calls
Supported Operations
All 17 languages support both Python and WASM backends. The WASM backend accelerates:
- Matrix operations — multiply, transpose, determinant, inverse
- Cryptography — XOR, AES-like operations, hashing
- Scientific computing — Monte Carlo, numerical integration, FFT
- Image processing — blur, sharpen, edge detection
- String operations — pattern matching, encoding
- JSON parsing — large document processing
- Fibonacci and numeric recursion — optimized integer arithmetic
Installation Options
| Option | Command | Size | Features |
|---|---|---|---|
| Minimal | pip install multilingualprogramming |
~50MB | Python only |
| Recommended | pip install multilingualprogramming[wasm] |
~150MB | WASM + Python fallback |
| Performance | pip install multilingualprogramming[performance] |
~250MB | WASM + NumPy + Python |