Talks

Summarized using AI

An mruby for WebAssembly

Uchio KONDO • May 15, 2024 • Naha, Okinawa, Japan

In this presentation from RubyKaigi 2024, Uchio Kondo discusses an implementation of mruby for WebAssembly. He introduces himself as an infrastructure engineer and translator from Japan, and emphasizes the purpose of utilizing WebAssembly to efficiently run Ruby code in various environments, including browsers.

Key points outlined in the talk include:

- Introduction to mruby and WebAssembly: Kondo explains that Ruby can be compiled into a binary format suitable for execution in web browsers and presents mruby as a specialized version for WebAssembly use cases.

  • Compiling with mruby: The presenter demonstrates how to compile Ruby files into WebAssembly using two components: the mruby compiler and the mrb command. Kondo outlines how to prepare the necessary RBS files for compiling into a single WebAssembly file that exports functions.

  • Execution example: A practical example is provided where a WebAssembly file exports a Fibonacci function, which can be verified through the 'wasm object dump' command. Kondo confirms that the generated WebAssembly file can run in a browser through an HTML setup that invokes the Fibonacci function.

  • Overview of WebAssembly: Kondo elaborates on the versatility of WebAssembly, describing it as a stack-based virtual machine that is used not only in browsers but also in diverse environments such as cloud-native settings, load balancers, and various applications.

  • Integration with Ruby: He discusses the importance of specifying export and import functions when compiling Ruby files, offering an example of a simple Ruby addition operation that is successfully executed via WebAssembly.

  • Understanding WASM structure: Kondo shares insights into the binary format of WebAssembly, including its structure consisting of different sections for function declarations, memory, globals, and more. This allows developers to inspect and validate function signatures using WASM object analysis tools.

  • Efficient runtime: Kondo introduces the implementation of mbh, designed to enable efficient code management for both mruby and WebAssembly. He compares the size of mruby with its dependencies to mruby for WebAssembly, highlighting the latter's compactness and efficiency. For instance, while standard mruby with dependencies may reach 18MB, a Fibonacci function example compiled with mbh is only 174KB.

  • Future potential: The talk concludes with an encouraging note on the growth potential of mruby integrated with WebAssembly, offering improvements in performance and efficiency for Ruby applications on the web.

Overall, Kondo's presentation emphasizes the integration of mruby with WebAssembly as a promising approach for developing lightweight and efficient Ruby binaries suitable for web environments.

An mruby for WebAssembly
Uchio KONDO • May 15, 2024 • Naha, Okinawa, Japan

RubyKaigi 2024

00:00:08.639 Hi, good afternoon! Are you enjoying Okinawa?
00:00:14.040 Today, I'm going to introduce my product, an mruby for WebAssembly.
00:00:22.920 First, let me introduce myself. I am Uchio Kondo from a company in Japan.
00:00:28.199 I work as an infrastructure engineer, and my company is one of the largest live streaming and gaming apps in Japan.
00:00:43.640 Additionally, I am a translator for the book "Learning eBPF." It's now available in Japan, so please let me know if you get a chance to read it.
00:00:59.320 So today, I'm going to talk about Ruby and WebAssembly.
00:01:06.030 I will provide several examples in various programming languages. I changed the background for convenience, and this side is multilingual, so please be mindful.
00:01:27.000 Regarding Ruby for WebAssembly, I think you already know that Ruby is a language that can be compiled into a binary format. You know that C is language-based, and you can compile it into a format that runs in browsers.
00:02:01.119 But I am going to show you another approach. This is mruby, which is specialized for WebAssembly use cases. This system consists of two components: the mruby compiler and the mrb command. You can install mruby using the cargo command.
00:02:41.680 You may understand this function, which you might refer to as the f number calculus. You must prepare an RBS file for this function.
00:03:06.879 If you have these two files, you can compile them into one WebAssembly file.
00:03:17.960 This WASM file exports a function named 'fib,' which you can check using 'wasm object dump' command. You can run this using various runtimes, like VM H.
00:03:44.159 The calculated Fibonacci number may be correct. Now, you may wonder whether this WebAssembly file can run in a browser. The answer is yes.
00:04:13.599 You need to prepare an HTML file that includes the necessary JavaScript code. This HTML will invoke the Fibonacci function and compute its value.
00:04:55.880 Using mruby for WebAssembly, you can work both server-side and browser-side, producing one binary. This allows you to export specific functions. Additionally, we can specify functions to import for the Ruby program.
00:05:28.039 To understand this system, we need to grasp two technologies: WebAssembly and mruby. Let's begin with WebAssembly.
00:06:01.600 First, how many of you know about WebAssembly? I believe you might think of it as something magical that allows languages like Ruby or Python to run in the browser.
00:06:24.880 You might have heard that Google uses WebAssembly, especially for image and video encoding.
00:06:54.680 WebAssembly is widely utilized across technology stacks. It is essentially a stack-based virtual machine that executes its instructions on browsers, servers, and other environments.
00:07:25.960 For instance, it can be incorporated into load balancers, log analyzers, and even containers.
00:08:11.560 In fact, in cloud-native environments, WebAssembly can be used for configuration management. Man, it is almost everywhere!
00:08:54.520 Now you know about the export and import functionalities available in WebAssembly. Next, let’s look into how this works with Ruby.
00:09:15.279 Here, I will show you a Ruby Answer method that internally specifies what functions to export and their signatures. You will also specify import signatures.
00:09:38.399 By compiling a Ruby file, you can leverage inversor as input. The result from a simple addition such as 21 + 42 will display as 63.
00:10:04.920 This showcases the basic functionality of WebAssembly, but let's dive a step deeper.
00:10:37.680 WebAssembly is essentially a binary format that contains a magic number, version information, and various sections.
00:10:57.960 Now, the sections consist of multiple types such as function declarations, memory declarations, globals, exports, and more. You can inspect these sections and confirm function signatures with tools that analyze WASM objects.
00:11:54.640 Import and export functionality is crucial. The interface allows functionality for system aspects like file access, networking, randomness, and threading.
00:12:57.199 For example, functions like 'random get' can serve similar purposes to system calls you may know.
00:13:36.480 WebAssembly, therefore, gives developers a great array of input functions. This means that you can generate code to work seamlessly across different runtimes.
00:14:27.559 This brings me to mbh, which is the project’s implementation for managing code for both the mruby VM and WebAssembly.
00:14:49.919 The goal is to offer a reliable runtime for mruby that is efficient in size. For instance, mruby with its dependencies can take up 18MB while the output using mruby for WebAssembly can be just 8MB.
00:15:33.120 But if I create an example Fibonacci function using mbh, it is only 174KB, showcasing its capacity to handle the basic functionalities of mruby.
00:16:34.600 Furthermore, compiling Ruby with this system allows for the efficient calculation of Fibonacci numbers while being lightweight.
00:17:15.640 Many developers have expressed interest in the potential for growth and performance improvements of these runtimes.
00:18:07.920 Now, let’s summarize the connection between mruby, WebAssembly, and their integration for development.
00:18:39.680 Overall, the integration is still actively developed and has great potential to enhance your Ruby code on the web by building compact and efficient binaries.
00:19:53.400 Thank you for your attention and I hope you're looking forward to experimenting with these technologies.
Explore all talks recorded at RubyKaigi 2024
+51