ForgeCurves Future Extension: AOT Compilation

Ahead-of-Time (AOT) Notebook Compilation

Transforming interactive scientific notebook cells into compiled WebAssembly binaries. Transpiles custom Octave procedural loops and algorithms into optimized Rust crates, achieving 15x to 60x speedups with native machine-level efficiency.

1. The Problem: Interpreted Loop Overhead

In standard scientific notebooks, execution speed splits into two distinct performance tiers:

Pre-Compiled Math (Near-Metal)

Matrix multiplication (GEMM), SVD, QR, and Parquet decoding run via compiled WebAssembly SIMD kernels at native machine speeds.

Custom Procedural Loops (Slow)

User-authored loops (e.g., for i = 1:100000 ... end), custom differential equations, and dynamic array indexing execute through JavaScript AST interpretation.

Scalar Boxing & V8 Churn

Every scalar variable lookup and assignment inside an interpreted loop creates temporary JS numbers and scope objects, triggering garbage collection stalls.

No Vectorizing Compiler Passes

Interpreted code cannot benefit from LLVM optimization passes such as loop unrolling, register allocation, auto-vectorization, or dead code elimination.

2. The AOT Compilation Pipeline

ForgeCurves introduces an end-to-end 1-click Ahead-of-Time compilation pipeline for active notebook cells:

1. AST Parsing & Type Inference

The client-side transpiler inspects cell code, extracts variable dependency graphs, and infers static array shapes and floating-point types from the active workspace.

2. Rust Source Generation

Generates an idiomatic, memory-safe Rust crate (notebook_crate) containing type-annotated variables, SIMD loops, and dedicated entry points (run_cell_0, run_cell_1).

3. WASM Compilation & Linking

Compiles the Rust crate into a dedicated notebook_<id>.wasm binary module through an authenticated cloud build service or a local compiler bridge.

4. Dynamic Hot-Swapping

The active Web Worker kernel dynamically instantiates the generated WASM binary, executing subsequent cell evaluations at native machine speed.

// Generated Rust code from an Octave numerical simulation cell #[no_mangle] pub extern "C" fn run_cell_2(state: &mut NotebookState) { let n = 100_000usize; let mut acc = 0.0f64; for i in 0..n { acc += (i as f64 * 0.001).sin() * (i as f64 * 0.002).cos(); } state.simulation_result = acc; }

3. Cross-Cell Memory Model (NotebookState)

In standard Octave, cells share global variables across sequential executions. The AOT compiler preserves this semantics by mapping the notebook workspace into a unified WebAssembly NotebookState struct:

Contiguous Linear Memory

Matrices and vectors are allocated in contiguous linear memory blocks, eliminating pointer chasing and allowing zero-copy sharing between compiled cells.

Workspace Bi-directional Sync

When a compiled cell finishes executing, updated variable definitions automatically synchronize with the notebook variable inspector (whos) and plotting engine.

4. Empirical Performance Multipliers

Projected and measured execution speedups comparing interpreted JavaScript execution against Ahead-of-Time compiled WebAssembly routines:

Algorithm Workload Interpreted Time (ms) AOT Compiled Time (ms) Speedup Multiplier Optimization Factor
Iterative Mandelbrot Fractal (100k points) 1,480.20 ms 24.60 ms 60.17x FASTER Loop Unrolling
Monte Carlo Option Pricing (500k paths) 3,840.50 ms 82.30 ms 46.66x FASTER Register Inlining
Euler ODE Numerical Solver (200k steps) 890.10 ms 28.40 ms 31.34x FASTER Branchless Math
Recursive Fibonacci / Tree Search 520.40 ms 22.10 ms 23.55x FASTER Call Stack Inlining
Coordinate-wise Matrix Update Loop 1,250.80 ms 68.50 ms 18.26x FASTER Bounds Check Removal

5. Cell Compilation Lifecycle & Status Badges

Each notebook cell dynamically tracks its compilation state with visual indicator badges in the editor gutter:

⚪ Interpreted

Standard uncompiled cell. Evaluated via the Web Worker JavaScript evaluation loop with pre-compiled BLAS math acceleration.

⏳ Compiling

Transient state while the transpiler analyzes the cell AST and the background build pipeline produces the WASM binary.

⚡ WASM Compiled

Cell logic is compiled into native WebAssembly bytecode. Execution dispatches directly to the compiled entry point at near-metal speed.

🟡 Modified (Interpreted)

Cell source was edited after compilation. Rerunning this cell falls back to interpreted mode automatically until the next compile pass.