# Lightstorm: minimalistic Ruby compiler

DevFeed: [Lightstorm: minimalistic Ruby compiler](<https://devfeed.tech/articles/lightstorm-minimalistic-ruby-compiler-43111.md>)

Original publisher: [Read original article](<https://blog.llvm.org/posts/2024-12-03-minimalistic-ruby-compiler/>)

Author: Alex Denisov, Amir Rajan

Published: 2024-12-09T00:00:00Z

Content type: article

Language: en

Sources: [LLVM Project](<https://devfeed.tech/sources/the-llvm-project-blog.md>)

Topics: [Compiler](<https://devfeed.tech/topics/compiler.md>), [Ruby](<https://devfeed.tech/topics/ruby.md>), [mlir](<https://devfeed.tech/topics/mlir.md>), [Code generation](<https://devfeed.tech/topics/code-generation.md>), [C](<https://devfeed.tech/topics/c.md>), [cross-platform](<https://devfeed.tech/topics/cross-platform.md>), [mruby](<https://devfeed.tech/topics/mruby.md>), [Game Development](<https://devfeed.tech/topics/game-development.md>)

Tags: [c](<https://devfeed.tech/tags/c.md>), [codegen](<https://devfeed.tech/tags/codegen.md>), [compilation](<https://devfeed.tech/tags/compilation.md>), [compiler](<https://devfeed.tech/tags/compiler.md>), [cross-platform](<https://devfeed.tech/tags/cross-platform.md>), [dragonruby](<https://devfeed.tech/tags/dragonruby.md>), [game-development](<https://devfeed.tech/tags/game-development.md>), [interpreter](<https://devfeed.tech/tags/interpreter.md>), [llvm](<https://devfeed.tech/tags/llvm.md>), [mlir](<https://devfeed.tech/tags/mlir.md>), [ruby](<https://devfeed.tech/tags/ruby.md>)

## AI overview

This article describes Lightstorm, a minimalistic ahead-of-time Ruby compiler. It explains how the project uses MLIR and EmitC to translate Ruby VM bytecode into C, with the goal of simplifying cross-platform support and evaluating whether precompiled Ruby code can improve performance in an indie-focused game engine.

## Source excerpt

Some time ago I was talking about an ahead-of-time Ruby compiler.We started the project with certain goals and hypotheses in mind, and while the original compiler is at nearly 90% completion, there are still those other 90% that needs to be done. In the meantime, we decided to strip it down to a bare minimum and implement just enough features to validate the hypotheses. Just like the original compiler we use MLIR to bridge the gap between Ruby VM's bytecode and the codegen, but instead of targeting LLVM IR directly, we go through EmitC dialect and targeting C language, as it significantly simplifies OS/CPU support. We go into a bit more details later. The source code of our minimalistic Ruby compiler is here: https://github.com/dragonruby/lightstorm. The rest of the article covers why we decided to build it, how we approached the problem, and what we discovered along the way. Motivation and the use case Our use case is pretty straightforward: we are building a cross-platform game engine that's indie-focused, productive, and easy to use. The game engine itself is written in a mix of C and Ruby, but the main user-interface is the Ruby itself. As soon as the game development is done and the game is ready for "deployment," the code is more or less static and so we asked ourselves if we can pre-compile it into machine code to make it run faster. But given all the dynamism of Ruby, why would a compilation make it faster? So here comes our hypothesis. But first let's look at some high-level implementation details to see where the hypothesis comes from. Compilers vs Interpreters While a language itself cannot be strictly qualified as compiled or interpreted, the typical implementations certainly are. In case of a "compiled" language the compiler would take the whole program, analyze it and produce the machine code targeting a specific hardware (real or virtual), while an interpreter would take the program and execute it right away, one "instruction" at a time. The definitio