<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://johnwick.cc/index.php?action=history&amp;feed=atom&amp;title=Accelerating_Python_with_WebAssembly_and_Rust_Extensions</id>
	<title>Accelerating Python with WebAssembly and Rust Extensions - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://johnwick.cc/index.php?action=history&amp;feed=atom&amp;title=Accelerating_Python_with_WebAssembly_and_Rust_Extensions"/>
	<link rel="alternate" type="text/html" href="https://johnwick.cc/index.php?title=Accelerating_Python_with_WebAssembly_and_Rust_Extensions&amp;action=history"/>
	<updated>2026-05-06T23:58:31Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.44.1</generator>
	<entry>
		<id>https://johnwick.cc/index.php?title=Accelerating_Python_with_WebAssembly_and_Rust_Extensions&amp;diff=746&amp;oldid=prev</id>
		<title>PC: Created page with &quot;500px  For years, I’ve loved Python for its expressiveness but hated its performance bottlenecks. Anyone who has tried crunching large datasets, running tight loops, or building high-performance services knows the feeling: Python is elegant, but slow. That’s when I started experimenting with two powerful accelerators: WebAssembly (WASM) and Rust extensions. Both unlock insane performance gains while letting me kee...&quot;</title>
		<link rel="alternate" type="text/html" href="https://johnwick.cc/index.php?title=Accelerating_Python_with_WebAssembly_and_Rust_Extensions&amp;diff=746&amp;oldid=prev"/>
		<updated>2025-11-21T04:14:33Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;&lt;a href=&quot;/index.php?title=File:Accelerating_Python_with_WebAssembly.jpg&quot; title=&quot;File:Accelerating Python with WebAssembly.jpg&quot;&gt;500px&lt;/a&gt;  For years, I’ve loved Python for its expressiveness but hated its performance bottlenecks. Anyone who has tried crunching large datasets, running tight loops, or building high-performance services knows the feeling: Python is elegant, but slow. That’s when I started experimenting with two powerful accelerators: WebAssembly (WASM) and Rust extensions. Both unlock insane performance gains while letting me kee...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;[[file:Accelerating_Python_with_WebAssembly.jpg|500px]]&lt;br /&gt;
&lt;br /&gt;
For years, I’ve loved Python for its expressiveness but hated its performance bottlenecks. Anyone who has tried crunching large datasets, running tight loops, or building high-performance services knows the feeling: Python is elegant, but slow.&lt;br /&gt;
That’s when I started experimenting with two powerful accelerators: WebAssembly (WASM) and Rust extensions. Both unlock insane performance gains while letting me keep Python as the “glue” language. This post is my field report.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
1. Why Python Alone Isn’t Enough&lt;br /&gt;
Python’s biggest weakness is its Global Interpreter Lock (GIL) and reliance on CPython. Tight loops and CPU-heavy tasks choke quickly.&lt;br /&gt;
# Naive Fibonacci in Python&lt;br /&gt;
def fib(n):&lt;br /&gt;
    if n &amp;lt;= 1:&lt;br /&gt;
        return n&lt;br /&gt;
    return fib(n - 1) + fib(n - 2)&lt;br /&gt;
&lt;br /&gt;
print(fib(35))  # painfully slow&lt;br /&gt;
This call to fib(35) takes seconds in Python. In Rust or compiled WASM, it runs in milliseconds.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
2. Enter WebAssembly (WASM)&lt;br /&gt;
WASM was designed for the web but has grown into a universal runtime. It runs fast, secure, and portable. Embedding WASM into Python means I can write performance-sensitive logic in a compiled language but call it like a Python function.&lt;br /&gt;
Let’s compile a Rust function into WASM and use it in Python.&lt;br /&gt;
// fib.rs&lt;br /&gt;
#[no_mangle]&lt;br /&gt;
pub extern &amp;quot;C&amp;quot; fn fib(n: u32) -&amp;gt; u32 {&lt;br /&gt;
    match n {&lt;br /&gt;
        0 =&amp;gt; 0,&lt;br /&gt;
        1 =&amp;gt; 1,&lt;br /&gt;
        _ =&amp;gt; fib(n - 1) + fib(n - 2),&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
Compile to WASM:&lt;br /&gt;
rustc --target wasm32-unknown-unknown -O fib.rs -o fib.wasm&lt;br /&gt;
Load in Python:&lt;br /&gt;
import wasmtime&lt;br /&gt;
&lt;br /&gt;
store = wasmtime.Store()&lt;br /&gt;
module = wasmtime.Module.from_file(store.engine, &amp;quot;fib.wasm&amp;quot;)&lt;br /&gt;
instance = wasmtime.Instance(store, module, [])&lt;br /&gt;
&lt;br /&gt;
fib = instance.exports(store)[&amp;quot;fib&amp;quot;]&lt;br /&gt;
print(fib(store, 35))  # runs way faster&lt;br /&gt;
Suddenly, Python isn’t slow anymore.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
3. Rust Extensions: The Direct Power Boost&lt;br /&gt;
If WASM is about portability, Rust extensions are about raw power. With PyO3 or maturin, you can build native Rust modules that import directly into Python.&lt;br /&gt;
// lib.rs&lt;br /&gt;
use pyo3::prelude::*;&lt;br /&gt;
&lt;br /&gt;
#[pyfunction]&lt;br /&gt;
fn fib(n: u32) -&amp;gt; u32 {&lt;br /&gt;
    match n {&lt;br /&gt;
        0 =&amp;gt; 0,&lt;br /&gt;
        1 =&amp;gt; 1,&lt;br /&gt;
        _ =&amp;gt; fib(n - 1) + fib(n - 2),&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
#[pymodule]&lt;br /&gt;
fn fastfib(_py: Python, m: &amp;amp;PyModule) -&amp;gt; PyResult&amp;lt;()&amp;gt; {&lt;br /&gt;
    m.add_function(wrap_pyfunction!(fib, m)?)?;&lt;br /&gt;
    Ok(())&lt;br /&gt;
}&lt;br /&gt;
Build with maturin:&lt;br /&gt;
maturin build --release&lt;br /&gt;
Then in Python:&lt;br /&gt;
import fastfib&lt;br /&gt;
print(fastfib.fib(35))  # blazingly fast&lt;br /&gt;
This approach gave me near-C performance without leaving Python behind.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
4. When to Use WASM vs Rust Extensions&lt;br /&gt;
Through trial and error, I discovered this simple rule of thumb:&lt;br /&gt;
* 		Use WASM → when you need portability (run the same binary on web, server, and edge).&lt;br /&gt;
* 		Use Rust extensions → when you just need Python to run faster on your local or server environment.&lt;br /&gt;
Both approaches can coexist in the same codebase.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
5. Data Processing at Scale&lt;br /&gt;
To stress-test, I ported a text processing function from Python to Rust and benchmarked.&lt;br /&gt;
Python:&lt;br /&gt;
def word_count(texts):&lt;br /&gt;
    counts = {}&lt;br /&gt;
    for text in texts:&lt;br /&gt;
        for word in text.split():&lt;br /&gt;
            counts[word] = counts.get(word, 0) + 1&lt;br /&gt;
    return counts&lt;br /&gt;
Rust:&lt;br /&gt;
use std::collections::HashMap;&lt;br /&gt;
#[pyfunction]&lt;br /&gt;
fn word_count(texts: Vec&amp;lt;String&amp;gt;) -&amp;gt; HashMap&amp;lt;String, u32&amp;gt; {&lt;br /&gt;
    let mut counts = HashMap::new();&lt;br /&gt;
    for text in texts {&lt;br /&gt;
        for word in text.split_whitespace() {&lt;br /&gt;
            *counts.entry(word.to_string()).or_insert(0) += 1;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    counts&lt;br /&gt;
}&lt;br /&gt;
Rust crushed Python by more than 50x on my test dataset of 10M sentences.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
6. Combining WASM and Rust in a Python Workflow&lt;br /&gt;
My favorite setup: heavy compute in Rust → compiled to WASM → orchestrated in Python. This way, I can run the same logic everywhere, even inside a browser UI.&lt;br /&gt;
from wasmtime import Store, Module, Instance&lt;br /&gt;
&lt;br /&gt;
store = Store()&lt;br /&gt;
module = Module.from_file(store.engine, &amp;quot;wordcount.wasm&amp;quot;)&lt;br /&gt;
instance = Instance(store, module, [])&lt;br /&gt;
&lt;br /&gt;
word_count = instance.exports(store)[&amp;quot;word_count&amp;quot;]&lt;br /&gt;
result = word_count(store, &amp;quot;Python Rust WASM Python&amp;quot;)&lt;br /&gt;
print(result)&lt;br /&gt;
This flexibility is a game changer for hybrid web + backend projects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
7. Error Handling and Debugging&lt;br /&gt;
Debugging WASM and Rust extensions took some time. Here’s my pattern:&lt;br /&gt;
* 		Write tests in Rust before exposing functions.&lt;br /&gt;
* 		Add fallbacks in Python in case the extension crashes.&lt;br /&gt;
try:&lt;br /&gt;
    result = fastfib.fib(100000)&lt;br /&gt;
except Exception as e:&lt;br /&gt;
    print(f&amp;quot;Fallback to Python: {e}&amp;quot;)&lt;br /&gt;
    result = fib_py(100000)&lt;br /&gt;
This way, the system never completely breaks.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
8. Deploying in Production&lt;br /&gt;
For production, I containerized my Python+Rust hybrid app.&lt;br /&gt;
FROM python:3.11-slim&lt;br /&gt;
&lt;br /&gt;
RUN apt-get update &amp;amp;&amp;amp; apt-get install -y build-essential rustc cargo&lt;br /&gt;
&lt;br /&gt;
WORKDIR /app&lt;br /&gt;
COPY . .&lt;br /&gt;
RUN pip install maturin &amp;amp;&amp;amp; maturin build --release&lt;br /&gt;
&lt;br /&gt;
CMD [&amp;quot;python&amp;quot;, &amp;quot;main.py&amp;quot;]&lt;br /&gt;
This allowed my services to run at near-Rust speed but still integrate seamlessly with Python frameworks like FastAPI and Pandas.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
9. Final Thoughts&lt;br /&gt;
Python is never going to match C or Rust in raw performance. But with WASM and Rust extensions, it doesn’t need to. You get the best of both worlds: Python’s elegance + Rust’s power.&lt;br /&gt;
For me, this hybrid approach turned Python from a bottleneck into a performance powerhouse.&lt;br /&gt;
As Donald Knuth said: “Premature optimization is the root of all evil.” But with Rust and WASM in Python, it doesn’t feel like premature optimization — it feels like superpowers.&lt;br /&gt;
&lt;br /&gt;
Read the full article here: https://medium.com/@maximilianoliver25/accelerating-python-with-webassembly-and-rust-extensions-f9b1182c152b&lt;/div&gt;</summary>
		<author><name>PC</name></author>
	</entry>
</feed>