From Rust to beyond

At my work, I had an opportunity to start an experiment: Writing a single parser implementation in Rust for the new Gutenberg blogpost format, and use it on many platforms and environments, like JavaScript (via WebAssembly and ASM.js), C, PHP… An existing stack using PEG.js and PEG.php was used, but it was quickly showing its limitations: slow to parse, consuming too much memory… Let's see how Rust compares the current solution, and let's learn how to use Rust in all these environments!

Episodes

Episode 1 – Prelude

Let's start by explaining what Gutenberg is, how our own Rust works, and what we need to do.

Episode 2 – The WebAssembly galaxy

In this episode, we explore the WebAssembly galaxy.

We first off present WebAssembly. Then we see how to write a Rust program tailored for WebAssembly. We continue by explaining how to run this WebAssembly module on a JavaScript host. It brings some complications, like how to flatten an Abstract Syntax Tree to get fast memory accesses between Rust and JavaScript. We continue by reducing the size of the WebAssembly module to make it as small as possible, smaller than a tiny image. Finally, we compare performance of this Rust to WebAssembly to JavaScript design versus the original PEG.js solution: is it faster? Oh yeah… it is!

Episode 3 – The ASM.js galaxy

In this episode, we explore the ASM.js galaxy, as a fallback to WebAssembly when the JavaScript host doesn't support it. We end by a benchmark comparing the Rust to WebAssembly to ASM.js to JavaScript design versus the PEG.js solution: is it faster? Is it still worth it after the conversions to ASM.js? Oh bloody… still yeah!

Episode 4 – The C galaxy

C is the lingua franca of many programming languages. In this episode, we will approach the famous Foreign Function Interface (FFI) by generating a C API in Rust. We will continue by automatically generating C headers so that our interfaces are never outdated. Finally, we will write a small C programs just to test everything works as expected. After all, C is a trampoline to reach other galaxies.

Episode 5 – The PHP galaxy

This episode is the end of our experimental, and so of our journey. With the C API we have defined, we can now explore the PHP galaxy. First off, we need to discover how to write a PHP extension. Once it's done, we can write improve our skeleton extension to use our Rust parser via C. As usual, we will compare our Rust to C to PHP design versus the current PEG.php solution. Is it faster? Once again, it's amazing how faster it is!

This series is complete!