Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News Shopify Open-Sources Ruvy, a Toolchain to Create Wasm Modules from Ruby Code

Shopify Open-Sources Ruvy, a Toolchain to Create Wasm Modules from Ruby Code

This item in japanese

Recently open-sourced by Spotify, Ruvy is a WebAssembly toolchain capable to translate Ruby code into Wasm modules. Based on ruby.wasm, Ruvy is implemented in Rust and includes some optimizations to improve performance and to simplify Wasm module execution.

Ruvy leverages the Ruby interpreter module provided by ruby.wasm and uses wasi-vfs (WASI virtual file system) to package it into a single module along with all specified Ruby source files. wasi-vfs creates an virtual file system embedded within the .wasm binary and provides a popular mechanism to make Ruby source files readable to ruby.wasm without enabling access to the host filesystem in the WebAssembly runtime.

Here is where Ruvy brings improved performance by pre-initializing the Ruby VM when the Wasm module is built, as opposite to when it is loaded into memory for execution, as it happens by default with ruby.wasm/wasi-vfs. This improves runtime performance by 20%, explains Shopify senior Wasm developer Jeff Charles.

Additionally, explains Charles, Ruvy shrinks down to 30% the time required to compile from Wasm to native code using the Cranelift compiler.

Another advantage of using Ruvy, thanks to the fact it packages all required files together, is it simplifies execution syntax by not requiring a file path as a WASI argument. This is particularly relevant for example for various edge computing services, which do not allow to provide additional WASI arguments to the main endpoint function.

At the moment, Ruvy is only available as a source distribution, which means you need to build it before using it. The process is quite simple, in principle, once you have installed all required dependencies, which include rustup, wasm32-wasi, cmake, and Rosetta 2 for macOS hosts. After that, you just run make and then execute Ruvy like in the following example to create an index.wasm module from the ruby_examples/hello_world.rb source:

$ cargo run --package=cli ruby_examples/hello_world.rb -o index.wasm

As mentioned above, you can use the --preload command line flag to specify a directory containing Ruby files that should be passed into the Ruby VM.

Charles stresses out the fact that Ruvy is only able to run simple Ruby programs at the moment. Specifically, it cannot yet require Ruby gems, which makes large parts of Ruby standard library unavailable. In future, they plan to enable loading code from the standard library as well as non-native third-party gems to remove this limitation.

About the Author

Rate this Article