Spinel: Ruby AOT Native Compiler

(github.com)

111 points | by dluan 4 hours ago

12 comments

  • dluan 3 hours ago

    For some context, just presented by Matz at RubyKaigi 2026. It’s experimental but he built it with help from Claude in about a month. Successful live demo.

    It’s named after his new cat, which is named after a cat in Card Captor Sakura, which is the partner to another character named Ruby.

    • whizzter 3 hours ago

      If it wasn't built by Matz I'd have severe doubts, but it's clearly defined and I presume he knows all limitations of the Ruby semantics well.

      My thesis work (back when EcmaScript 5 was new) was an AOT JS compiler, it worked but there was limitations with regards to input data that made me abandon it after that since JS developers overall didn't seem to aware of how to restrict oneself properly (JSON.parse is inherently unknown, today with TypeScript it's probably more feasible).

      The limitations are clear also, the general lambda calculus points to limits in the type-inference system (there's plenty of good papers from f.ex. Matt Might on the subject) as well as the Shed-skin Python people.

      eval, send, method_missing, define_method , as a non-rubyist how common are these in real-world code? And how is untyped parsing done (ie JSON ingestion?).

      • vidarh 2 hours ago

        > If it wasn't built by Matz I'd have severe doubts, but it's clearly defined and I presume he knows all limitations of the Ruby semantics well.

        It's a very pragmatic design: Uses Prism - parsing Ruby is almost harder than the actual translation - and generates C. Basic Ruby semantics are not all that hard to implement.

        On the other extreme, I have a long-languishing, buggy, pure-Ruby AOT compiler for Ruby, and I made things massively harder for myself (on purpose) by insisting on it being written to be self-hosting, and using its own parser. It'll get there one day (maybe...).

        But one of the things I learned early on from that is that you can half-ass the first 80% and a lot of Ruby code will run. The "second 80%" are largely in things Matz has omitted from this (and from Mruby), like encodings, and all kinds of fringe features (I wish Ruby would deprecate some of them - there are quite a few things in Ruby I've never, ever seen in the wild).

        > eval, send, method_missing, define_method , as a non-rubyist how common are these in real-world code? And how is untyped parsing done (ie JSON ingestion?).

        They are pervasive. The limitations are similar to those of mruby, though, which has its uses.

        Supporting send, method_missing, and define_method is pretty easy.

        Supporting eval() is a massive, massive pain, but with the giant caveat that a huge proportion of eval() use in Ruby can be statically reduced to the block version of instance_eval, which can be AOT compiled relatively easily. E.g. if you can statically determine the string eval() is called with, or can split it up, as a lot of the uses are unnecessary or workaround for relatively simple introspection that you can statically check for and handle. For my own compiler, if/when I get to a point where that is a blocking issue, that's my intended first step.

      • pantulis 2 hours ago

        > eval, send, method_missing, define_method , as a non-rubyist how common are these in real-world code?

        Quite a lot, that's what allows you to build something like Rails with magic sprinkled all around. I'm not 100% sure, but probably the untyped JSON ingestion example uses those.

        Remove that, and you have a very compact and readable language that is less strongly typed than Crystal but less metaprogrammable than official Ruby. So I think it has quite a lot of potential but time will tell.

        • shevy-java 3 hours ago

          > eval, send, method_missing, define_method , as a non-rubyist how common are these in real-world code

          This depends on the individual writing code. Some use it more than others.

          I can only give my use case.

          .send() I use a lot. I feel that it is simple to understand - you simply invoke a specific method here. Of course people can just use .method_name() instead (usually without the () in ruby), but sometimes you may autogenerate methods and then need to call something dynamically.

          .define_method() I use sometimes, when I batch create methods. For instance I use the HTML colour names, steelblue, darkgreen and so forth, and often I then batch-generate the methods for this, e. g. via the correct RGB code. And similar use cases. But, from about 50 of my main projects in ruby, at best only ... 20 or so use it, whereas about 40 may use .send (or, both a bit lower than that).

          eval() I try to avoid; in a few cases I use them or the variants. For instance, in a simple but stupid calculator, I use eval() to calculate the expression (I sanitize it before). It's not ideal but simple. I use instance_eval and class_eval more often, usually for aliases (my brain is bad so I need aliases to remember, and sometimes it helps to think properly about a problem).

          method_missing I almost never use anymore. There are a few use cases when it is nice to have, but I found that whenever I would use it, the code became more complex and harder to understand, and I kind of got tired of that. So I try to avoid it. It is not always possible to avoid it, but I try to avoid it when possible.

          So, to answer your second question, to me personally I would only think of .send() as very important; the others are sometimes but not that important to me. Real-world code may differ, the rails ecosystem is super-weird to me. They even came up with HashWithIndifferentAccess, and while I understand why they came up with it, it also shows a lack of UNDERSTANDING. This is a really big problem with the rails ecosystem - many rails people really did not or do not know ruby. It is strange.

          "untyped parsing" I don't understand why that would ever be a problem. I guess only people whose brain is tied to types think about this as a problem. Types are not a problem to me. I know others disagree but it really is not a problem anywhere. It's interesting to see that some people can only operate when there is a type system in place. Usually in ruby you check for behaviour and capabilities, or, if you are lazy, like me, you use .is_a?() which I also do since it is so simple. I actually often prefer it over .respond_to?() as it is shorter to type. And often the checks I use are simple, e. g. "object, are you a string, hash or array" - that covers perhaps 95% of my use cases already. I would not know why types are needed here or fit in anywhere. They may give additional security (perhaps) but they are not necessary IMO.

        • Someone 21 minutes ago

          Limitations

          - No eval: eval, instance_eval, class_eval

          - No metaprogramming: send, method_missing, define_method (dynamic)

          - No threads: Thread, Mutex (Fiber is supported)

          - No encoding: assumes UTF-8/ASCII

          - No general lambda calculus: deeply nested -> x { } with [] calls

          Assuming UTF-8/ASCII isn’t, IMO, a huge limitation, but some of the others likely are, for quite a few programs. Removing them also will require serious work, I think.

          • the-lazy-guy 51 minutes ago

            While obviously super-impressive, it is clearly not maintanable without AI agent. It has spinel_codegen.rb is 21k lines of code with up to 15 levels of nesting in some methods.

            Compilers code was never pretty, but even by those standard, I feel like it is a very-very hard to maintain code by humans.

            • nu11ptr 2 hours ago

              Wow, written in just over a month. Say what you will about AI, but it has enabled serious speedups in the hands of a talented coder.

              • matltc 1 hour ago

                Rest of industry: OK we need to set up our agent harness, write our SOUL.md, config permissions, skills, mcps, hooks, env...

                Matz: gem env|info and find should do

                • lab14 1 hour ago

                  "plansturbation" is a real industry, there are tons of successful YouTubers that sell millions of dollars in tutorials, courses, books, etc on how to setup your productivity harness

              • nona 3 hours ago

                This is really cool, I've been looking for an AOT compiler for ruby for a long time.

                The lack of eval/meta-programming fallbacks is a shame though, but I guess they kept the focus on a small, performant subset.

                It would be nice to have gems compiled with this AOT compiler that can interact well with MRI.

                When it comes to packaging/bundling more standard ruby (including gems) we'll still need tebako, kompo, ocran – and then there's a bunch of older projects that did similar things too like ruby-packer, traveling ruby, jruby warbler etc.

                It's nice to have another option, but still, I'm hoping for a more definitive solution with better developer UX.

                • densh 2 hours ago

                  As someone who spent a few years working on a compiler in this space — it’s tough, but are the results of instant startup and immediately decent performance without warm-up satisfying to use in practice. I really hope this takes off and yet another language can break free from dominant interpreter + jit compiler monoculture that we currently have for higher-level programming languages.

                  • Maledictus 3 hours ago

                    wow, I wanted to have this for a long time. I looked at Crystal, but it never sat right with me.

                    I think some of the limitations can still be implemented (definitely Threads and Mutex), and I'd prefer it to compile to LLVM-IR or something, not C, but overall I think it is great to see Matz playing around with AOT compiling.

                    • xz18r 3 hours ago

                      Given it's built by Matz, how realistic is it that this becomes a core part of Ruby? And if so, how threatening is that for Crystal?

                      • jaen 3 hours ago

                        Crystal has an explicit static type system and is actually optimized at the language level for AOT compilation. These features are pretty much required for compiling and maintaining large programs.

                        This is for a limited subset of Ruby - almost no popular Ruby gems would run under it. It's more like PreScheme [1] (ie. a subset of a language oriented at C compilation).

                        I don't think these compete in the same niches right now. Full Ruby almost certainly requires a JIT.

                        [1]: https://prescheme.org/

                        • vidarh 2 hours ago

                          It's a similar subset to mruby, and it might well end up influencing mruby, which does have its users. But it's almost a different language in some ways.

                        • pjmlp 43 minutes ago

                          This should be seen in another perspective, we will eventually reach the point where LLMs can vomit the formal specification in whatever language we feel like.

                          The revenge of Rational Unified Process, Enterprise Architect and many other tools.

                          Instead of UML diagrams it is markdown files.

                        • zhisme 1 hour ago

                          Even Matz now uses claude code...

                          • Asmod4n 1 hour ago

                            He has been using it for quite a long time for mruby already.

                            For Ruby and c development it’s the best llm we got right now, the others lag behind by a lot sadly.

                            With openAI it’s so bad for my usecase it’s basically unusable.

                          • shevy-java 3 hours ago

                            I find the current documentation difficult to understand.

                            This is a problem I see with many ruby projects. How would I reword this?

                            Well, first thing, after stating what spinel is, I would show a simple example. Ideally a standalone .rb file or something like that, that can be downloaded (or whatever other format). Yes, the README shows this, but believe it or not, I have realised that I am usually below average when trying to understand something that is now. I even manage to make copy/paste mistakes. This is why I think one or two standalone as-is examples would be best.

                            And then I would explain use cases.

                            The current structure of the document is strange. Has that been written with AI? If AI replaces the human individual, why is it then expected that real people should read that? So many questions here ...

                            Also, I would really like for the ruby ecosystem to not be split up into different entities. I understand that mruby does not have the same goals as MRI ruby, but still, there is fragmentation. Now there is spinel - how does it relate to other parts of ruby? Why are truffleruby and jruby separate? (I know why, so I am not objecting to the rationale; I am pointing out that for a USER it would be better if things would be more unified here in general.)

                            Ruby really needs to focus on its inner core. The base should be solid. Even more so when it is harder to attract genuinely new developers.

                            • pelasaco 1 hour ago

                              Maybe AI generated? I mostly suffer to read AI generated documentation.

                            • claudiug 3 hours ago

                              is this done by matz and claude? :)