# Compiling Ruby code

**URL:** <https://rubytalk.org/t/compiling-ruby-code/12652>\
**Category:** ruby-talk\
**Created:** [19 July 2004 11:57 UTC](https://rubytalk.org/t/compiling-ruby-code/12652 "2004-07-19T11:57:18Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nospam1](https://avatars.discourse-cdn.com/v4/letter/n/82dd89/32.png) [@Nospam1](https://rubytalk.org/u/Nospam1)\
**Post date:** [19 July 2004 11:57 UTC](https://rubytalk.org/t/compiling-ruby-code/12652/1 "2004-07-19T11:57:18Z")

</div>

Hi,

Once in a while the question pops up if it is possible to compile Ruby code to native machine code. The answer has always been no. But I keep wondering how hard it would really be to make this possible.

Ruby is written in C. And when Ruby parses a Ruby script it converts each statement to a C call. Probably the same calls you can use on your own in a Ruby C extension. So why wouldn't it be possible to parse a Ruby script and convert all statements to Ruby C code and put it in a \*.c file (instead of calling the Ruby C statements directly). This \*.c file can then be compiled into machine code with a C compiler like gcc. If each \*.rb file is converted to a C file it could be compiled to a dynamically loadable library which could then be used on require statements (just like regular Ruby C extensions).

What I mean is, this...

class Example  
&nbsp;&nbsp;&nbsp;def example  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Hello World!"  
&nbsp;&nbsp;&nbsp;end  
end

.... can also be written in C using the Ruby C API, am I right? So why wouldn't it be possible to convert all Ruby code to C code using the Ruby C API?

This would probably result in some performance gain (no need to parse the code anymore at run-time), but for some people more important, you can distribute your Ruby applications closed-source. In the future the performance gain maybe could be increased by performing special optimizations during the conversion process.

Am I right on this, or do I forget something important which makes the above quite hard to do?

With kind regards,

Peter

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [19 July 2004 12:07 UTC](https://rubytalk.org/t/compiling-ruby-code/12652/2 "2004-07-19T12:07:09Z")

</div>

"Nospam" \<news.home.nl-1@nospam.no-nonsense.org\> schrieb im Newsbeitrag  
news:cdgcqd$67n$1@news3.tilbu1.nb.home.nl...

> Hi,
> 
> Once in a while the question pops up if it is possible to compile Ruby  
> code to native machine code. The answer has always been no. But I keep  
> wondering how hard it would really be to make this possible.
> 
> Ruby is written in C. And when Ruby parses a Ruby script it converts  
> each statement to a C call. Probably the same calls you can use on your  
> own in a Ruby C extension. So why wouldn't it be possible to parse a  
> Ruby script and convert all statements to Ruby C code and put it in a  
> \*.c file (instead of calling the Ruby C statements directly). This \*.c  
> file can then be compiled into machine code with a C compiler like gcc.  
> If each \*.rb file is converted to a C file it could be compiled to a  
> dynamically loadable library which could then be used on require  
> statements (just like regular Ruby C extensions).
> 
> What I mean is, this...
> 
> class Example  
> &nbsp;&nbsp;&nbsp;def example  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Hello World!"  
> &nbsp;&nbsp;&nbsp;end  
> end
> 
> ... can also be written in C using the Ruby C API, am I right? So why  
> wouldn't it be possible to convert all Ruby code to C code using the  
> Ruby C API?

I guess it \*is\* possible, but then you would have to bundle gcc with the  
Ruby interpreter, because you must deal with dynamic redefinition of  
methods and eval.

> This would probably result in some performance gain (no need to parse  
> the code anymore at run-time),

AFAIK at runtime there is a parse phase and an execute phase. Each line  
of code is only parsed once and then converted into some kind of byte code  
(not the Java flavour), so you probably don't gain much time.

> but for some people more important, you  
> can distribute your Ruby applications closed-source.

That would impose certain restrictions, especially you must forbid this:

# just a stupid example that loads different files  
# depending on some runtime state  
case some\_var  
&nbsp;&nbsp;when "foo"  
&nbsp;&nbsp;&nbsp;&nbsp;require "x-as-foo-impl.rb"  
&nbsp;&nbsp;when "bar"  
&nbsp;&nbsp;&nbsp;&nbsp;require "x-as-bar-impl.rb"  
&nbsp;&nbsp;when /^(\w+)-\w+$/  
&nbsp;&nbsp;&nbsp;&nbsp;require "x-as-#{$1}-impl.rb"  
&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;require "x-default-impl.rb"  
end

> In the future the  
> performance gain maybe could be increased by performing special  
> optimizations during the conversion process.
> 
> Am I right on this, or do I forget something important which makes the  
> above quite hard to do?

I think so, as noted above.

Regards

&nbsp;&nbsp;&nbsp;&nbsp;robert

---

<div class="post-metadata">

**Author:** ![George5](https://avatars.discourse-cdn.com/v4/letter/g/7c8e57/32.png) [@George5](https://rubytalk.org/u/George5)\
**Post date:** [19 July 2004 12:10 UTC](https://rubytalk.org/t/compiling-ruby-code/12652/3 "2004-07-19T12:10:19Z")

</div>

Nospam wrote:

> Hi,
> 
> Once in a while the question pops up if it is possible to compile Ruby code to native machine code. The answer has always been no. But I keep wondering how hard it would really be to make this possible.
> 
> Ruby is written in C. And when Ruby parses a Ruby script it converts each statement to a C call. Probably the same calls you can use on your own in a Ruby C extension. So why wouldn't it be possible to parse a Ruby script and convert all statements to Ruby C code and put it in a \*.c file (instead of calling the Ruby C statements directly). This \*.c file can then be compiled into machine code with a C compiler like gcc. If each \*.rb file is converted to a C file it could be compiled to a dynamically loadable library which could then be used on require statements (just like regular Ruby C extensions).
> 
> What I mean is, this...
> 
> class Example  
> &nbsp;&nbsp;def example  
> &nbsp;&nbsp;&nbsp;&nbsp;puts "Hello World!"  
> &nbsp;&nbsp;end  
> end
> 
> .... can also be written in C using the Ruby C API, am I right? So why wouldn't it be possible to convert all Ruby code to C code using the Ruby C API?
> 
> This would probably result in some performance gain (no need to parse the code anymore at run-time), but for some people more important, you can distribute your Ruby applications closed-source. In the future the performance gain maybe could be increased by performing special optimizations during the conversion process.

I remember that a long time ago, there was a ruby-to-c compiler (was it called r2c?). But IIRC, there was only little performance gain. Remember that you still need a Ruby parser, due to "eval". It would be nice, but I'd even more like to see a bytecode compiler (written in pure Ruby running on top of the bytecode interpreter).

Regards,

&nbsp;&nbsp;&nbsp;Michael

---

<div class="post-metadata">

**Author:** ![Claus\_Spitzer](https://avatars.discourse-cdn.com/v4/letter/c/49beb7/32.png) [@Claus\_Spitzer](https://rubytalk.org/u/Claus_Spitzer)\
**Post date:** [19 July 2004 12:53 UTC](https://rubytalk.org/t/compiling-ruby-code/12652/4 "2004-07-19T12:53:51Z")

</div>

It is generally difficult to create a compiler for interpreted  
languages, specially if such a language is reflection-rich like Ruby.  
If writing commercial applications and performance are your biggest  
concerns, then perhaps what you're looking for is partial evaluation  
and an application generator. Then you would still have Ruby  
interpreted, but a speed possibly equivalent to compiled languages.  
And the source of your program would be embedded in the interpreter  
itself.

> **···**
>
> On Mon, 19 Jul 2004 20:57:18 +0900, Nospam \<news.home.nl-1@nospam.no-nonsense.org\> wrote:
> 
> > Hi,
> > 
> > Once in a while the question pops up if it is possible to compile Ruby  
> > code to native machine code. The answer has always been no. But I keep  
> > wondering how hard it would really be to make this possible.
> > 
> > Ruby is written in C. And when Ruby parses a Ruby script it converts  
> > each statement to a C call. Probably the same calls you can use on your  
> > own in a Ruby C extension. So why wouldn't it be possible to parse a  
> > Ruby script and convert all statements to Ruby C code and put it in a  
> > \*.c file (instead of calling the Ruby C statements directly). This \*.c  
> > file can then be compiled into machine code with a C compiler like gcc.  
> > If each \*.rb file is converted to a C file it could be compiled to a  
> > dynamically loadable library which could then be used on require  
> > statements (just like regular Ruby C extensions).
> > 
> > What I mean is, this...
> > 
> > class Example  
> > &nbsp;&nbsp;&nbsp;def example  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Hello World!"  
> > &nbsp;&nbsp;&nbsp;end  
> > end
> > 
> > .... can also be written in C using the Ruby C API, am I right? So why  
> > wouldn't it be possible to convert all Ruby code to C code using the  
> > Ruby C API?
> > 
> > This would probably result in some performance gain (no need to parse  
> > the code anymore at run-time), but for some people more important, you  
> > can distribute your Ruby applications closed-source. In the future the  
> > performance gain maybe could be increased by performing special  
> > optimizations during the conversion process.
> > 
> > Am I right on this, or do I forget something important which makes the  
> > above quite hard to do?
> > 
> > With kind regards,
> > 
> > Peter

---

<div class="post-metadata">

**Author:** ![Laurent\_Julliard4](https://avatars.discourse-cdn.com/v4/letter/l/ebca7d/32.png) [@Laurent\_Julliard4](https://rubytalk.org/u/Laurent_Julliard4)\
**Post date:** [19 July 2004 13:22 UTC](https://rubytalk.org/t/compiling-ruby-code/12652/5 "2004-07-19T13:22:13Z")

</div>

I agree that this could be done, linking the runtime for dynamic  
behaviour.  
but the performance hit would still be in the method lookup phase, as  
projects like python-to-c converter pointed out.  
You need to do some clever type inference and runtime optimization,  
and that won't be so easy.  
Look out for starkiller (python compiler+type inference) for a  
discussion on this.

> **···**
>
> il Mon, 19 Jul 2004 13:54:53 +0200, Nospam \<[news.home.nl-1@nospam.no-nonsense.org](mailto:news.home.nl-1@nospam.no-nonsense.org)\> ha scritto::

---

<div class="post-metadata">

**Author:** ![Kristof\_Bastiaensen1](https://avatars.discourse-cdn.com/v4/letter/k/9fc29f/32.png) [@Kristof\_Bastiaensen1](https://rubytalk.org/u/Kristof_Bastiaensen1)\
**Post date:** [19 July 2004 14:42 UTC](https://rubytalk.org/t/compiling-ruby-code/12652/6 "2004-07-19T14:42:24Z")

</div>

> Hi,

Hi

> \<snip\>
> 
> What I mean is, this...
> 
> class Example  
> &nbsp;&nbsp;&nbsp;def example  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Hello World!"  
> &nbsp;&nbsp;&nbsp;end  
> end
> 
> ... can also be written in C using the Ruby C API, am I right? So why  
> wouldn't it be possible to convert all Ruby code to C code using the  
> Ruby C API?

Yes, that's possible, but depending on the C code you write, there  
isn't much performance gain. You can write any ruby code in C,  
but if you just convert it litteraly to C, there isn't much performance  
gain. For example when you write  
&nbsp;&nbsp;arr[1] = 2 #arr is an Array  
you could use rb\_ary\_store(arr, 1, INT2FIX(2)), but that wouldn't be  
equivalent to the ruby code. When the Array# method would change, the  
ruby code would see the change, but the C code wouldn't. The solution  
would be to use something like rb\_funcall(arr, rb\_intern(""), 0), but  
then there would be no performance benefit, because ruby still has to look  
up the method. The point is that it is possible to write more efficient  
ruby code in C, but it isn't really ruby code anymore.

> This would probably result in some performance gain (no need to parse  
> the code anymore at run-time), but for some people more important, you  
> can distribute your Ruby applications closed-source. In the future the  
> performance gain maybe could be increased by performing special  
> optimizations during the conversion process.

The parsing fase doesn't take so much performance, since it is only done  
at loading time.

> Am I right on this, or do I forget something important which makes the  
> above quite hard to do?
> 
> With kind regards,
> 
> Peter

Well, I do think it is possible to compile Ruby, but it would be to hard.  
Firstly eval and module\_eval should be thrown away, because they need to  
be able to parse code at runtime.  
Continuations make compiling very messy, since the stack needs to be  
copied. This can also can cause troubles when interfacing with native  
c-calls. (I wonder how the current Ruby-interpreter manages this...)

The best way is IMO to have a different language that resembles Ruby  
as much as possible, but allows it to be compiled easily. (I am sure  
Matz wouldn't allow Ruby to be crippled to allow compilation).  
It would have limitations that Ruby doesn't have (no eval or continuations),  
but it would be a nice alternative for coding in C. In fact code blocks  
and closures aren't that difficult to compile, and I think any decent  
compilable language should have them.

As for Ruby, the best performance gain would be by using a Just-In-Time  
compiler. This way, the compiler can make assumptions about the code, and  
recompile when this assumptions are wrong. Take for example the following  
code:  
&nbsp;&nbsp;5.times do { |i| puts i }  
Here the compiler could inline the code for times and produce very  
efficient code. The same goes for other standard library functions like  
Array#, etc. However it is still possible to redefine Numeric#times  
(though I don't see a good reason to!). If the method would be redefined,  
then the just-in-time compiler could recompile it. The same goes for  
method arguments. When an argument to a method is always from the same  
class, the compiler can call the methods on that object directly (without  
lookup), or even inline them. However when different kinds of objects get  
passed to the method, then the JIT-compiler could recompile the method, so  
that it works with all objects.

In this way the JIT can create well performing code, and at the same time  
keep the dynamic nature of Ruby. When methods are redefined, or added,  
the jit-compiler makes the changes.  
It would be nice to have such an interpreter, but it would be a  
considerable amount of work to implement it, as it would need a  
complete rewrite of the Ruby-interpreter.

Regards,  
Kristof

> **···**
>
> On Mon, 19 Jul 2004 13:54:53 +0200, Nospam wrote:

---

<div class="post-metadata">

**Author:** ![David\_Ross2](https://avatars.discourse-cdn.com/v4/letter/d/ed8c4c/32.png) [@David\_Ross2](https://rubytalk.org/u/David_Ross2)\
**Post date:** [19 July 2004 15:48 UTC](https://rubytalk.org/t/compiling-ruby-code/12652/7 "2004-07-19T15:48:17Z")

</div>

> Once in a while the question pops up if it is  
> possible to compile Ruby  
> code to native machine code. The answer has always  
> been no. But I keep  
> wondering how hard it would really be to make this  
> possible.

The answer is really yes. Why? People say no because  
of the implementation on how it is supposed to work.  
It is very possible to, but it would take time.

> Ruby is written in C. And when Ruby parses a Ruby  
> script it converts  
> each statement to a C call. Probably the same calls  
> you can use on your  
> own in a Ruby C extension. So why wouldn't it be  
> possible to parse a  
> Ruby script and convert all statements to Ruby C  
> code and put it in a  
> \*.c file (instead of calling the Ruby C statements  
> directly). This \*.c  
> file can then be compiled into machine code with a C  
> compiler like gcc.

Evaluated code at runtime needs to be thought of.  
There needs to be a small runtime running on top of  
the compiled program.

> Am I right on this, or do I forget something  
> important which makes the  
> above quite hard to do?

Just very hard to do and no one wants to bother with  
creating it. Which is why I emailed "Ruby  
specification" on the mailing list about creating  
rubycc. --David Ross

> **···**
>
> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> Do you Yahoo!?  
> Vote for the stars of Yahoo!'s next ad campaign!  
> [http://advision.webevents.yahoo.com/yahoo/votelifeengine/](http://advision.webevents.yahoo.com/yahoo/votelifeengine/)

---

<div class="post-metadata">

**Author:** ![Peter\_C\_Verhage1](https://avatars.discourse-cdn.com/v4/letter/p/9fc348/32.png) [@Peter\_C\_Verhage1](https://rubytalk.org/u/Peter_C_Verhage1)\
**Post date:** [19 July 2004 20:47 UTC](https://rubytalk.org/t/compiling-ruby-code/12652/8 "2004-07-19T20:47:16Z")

</div>

Thanks all for the interesting read. I don't need my Ruby code to be compiled to machine code, but I was just interested if it would be possible and what it would mean. I agree with others that a JIT compiler would be the way to go for Ruby, Java already proved that JIT compiled code can sometimes out perform normally compiled C code and I don't really mind that my Ruby program might be theoretically not the best performing program, you can always buy more processing power, but there aren't many other languages which are as nice as Ruby. 🙂

Regards,

Peter (Nospam)

---

<div class="post-metadata">

**Author:** ![Scott\_Rubin1](https://avatars.discourse-cdn.com/v4/letter/s/22d042/32.png) [@Scott\_Rubin1](https://rubytalk.org/u/Scott_Rubin1)\
**Post date:** [19 July 2004 12:52 UTC](https://rubytalk.org/t/compiling-ruby-code/12652/9 "2004-07-19T12:52:09Z")

</div>

Michael Neumann wrote:

> Nospam wrote:
> 
> > Hi,
> > 
> > Once in a while the question pops up if it is possible to compile Ruby code to native machine code. The answer has always been no. But I keep wondering how hard it would really be to make this possible.
> > 
> > Ruby is written in C. And when Ruby parses a Ruby script it converts each statement to a C call. Probably the same calls you can use on your own in a Ruby C extension. So why wouldn't it be possible to parse a Ruby script and convert all statements to Ruby C code and put it in a \*.c file (instead of calling the Ruby C statements directly). This \*.c file can then be compiled into machine code with a C compiler like gcc. If each \*.rb file is converted to a C file it could be compiled to a dynamically loadable library which could then be used on require statements (just like regular Ruby C extensions).
> > 
> > What I mean is, this...
> > 
> > class Example  
> > &nbsp;&nbsp;def example  
> > &nbsp;&nbsp;&nbsp;&nbsp;puts "Hello World!"  
> > &nbsp;&nbsp;end  
> > end
> > 
> > .... can also be written in C using the Ruby C API, am I right? So why wouldn't it be possible to convert all Ruby code to C code using the Ruby C API?
> > 
> > This would probably result in some performance gain (no need to parse the code anymore at run-time), but for some people more important, you can distribute your Ruby applications closed-source. In the future the performance gain maybe could be increased by performing special optimizations during the conversion process.
> 
> I remember that a long time ago, there was a ruby-to-c compiler (was it called r2c?). But IIRC, there was only little performance gain. Remember that you still need a Ruby parser, due to "eval". It would be nice, but I'd even more like to see a bytecode compiler (written in pure Ruby running on top of the bytecode interpreter).
> 
> Regards,
> 
> &nbsp;&nbsp;Michael

Yes, I have to vote for a bytecode compiler. Right now I'm using ruby to develop some software on an embedded arm-linux device. I've found that for this application, which is not very demanding of the system, that ruby can perform pretty comparably to an equivalent C program. And the development time it saves to write in a higher level language is worth its weight in gold. But every time I run a ruby program it takes it a significant and noticable amount of time to start up. This is obviously due to the ruby parser compiling the text into bytecode every time. If it was possible to precompile this bytecode and put that on the target machine it would have significant advantages. Also, it would make it possible to distribute the application without distributing the source code to it. That isn't so important for us, but it may be for others.

2 cents. Scott

---

<div class="post-metadata">

**Author:** ![Mikael\_Brockman1](https://avatars.discourse-cdn.com/v4/letter/m/8baadc/32.png) [@Mikael\_Brockman1](https://rubytalk.org/u/Mikael_Brockman1)\
**Post date:** [19 July 2004 15:00 UTC](https://rubytalk.org/t/compiling-ruby-code/12652/10 "2004-07-19T15:00:22Z")

</div>

Kristof Bastiaensen \<kristof@vleeuwen.org\> writes:

> Well, I do think it is possible to compile Ruby, but it would be to hard.  
> Firstly eval and module\_eval should be thrown away, because they need to  
> be able to parse code at runtime.  
> Continuations make compiling very messy, since the stack needs to be  
> copied.

That's not strictly true. If you convert the code to  
continuation-passing style, reifying continuations doesn't require  
copying the stack. CHICKEN and many other Scheme compilers choose this  
approach.

&nbsp;&nbsp;mikael

---

<div class="post-metadata">

**Author:** ![Gavin\_Sinclair](https://avatars.discourse-cdn.com/v4/letter/g/db5fbb/32.png) [@Gavin\_Sinclair](https://rubytalk.org/u/Gavin_Sinclair)\
**Post date:** [19 July 2004 15:58 UTC](https://rubytalk.org/t/compiling-ruby-code/12652/11 "2004-07-19T15:58:20Z")

</div>

My fearless prediction: no-one will write a Ruby specification if you  
don't. (Unless someone else is working hard on a compiler that we  
don't know, or that I've forgotten.)

What is your rationale (in detail) for not looking at the Ruby source?

Lots of people would like to see a Ruby compiler, so any attempt by  
yourself to create an English specification would almost certainly be  
supported by several people testing it thoroughly for correctness.  
Why not start now?

Cheers,  
Gavin

> **···**
>
> On Tuesday, July 20, 2004, 1:48:17 AM, David wrote:
> 
> > > Am I right on this, or do I forget something  
> > > important which makes the  
> > > above quite hard to do?
> 
> > Just very hard to do and no one wants to bother with  
> > creating it. Which is why I emailed "Ruby  
> > specification" on the mailing list about creating  
> > rubycc.

---

<div class="post-metadata">

**Author:** ![Lothar\_Scholz2](https://avatars.discourse-cdn.com/v4/letter/l/f14d63/32.png) [@Lothar\_Scholz2](https://rubytalk.org/u/Lothar_Scholz2)\
**Post date:** [19 July 2004 16:26 UTC](https://rubytalk.org/t/compiling-ruby-code/12652/12 "2004-07-19T16:26:04Z")

</div>

Hello Michael,

> Nospam wrote:
> 
> > Hi,
> > 
> > Once in a while the question pops up if it is possible to compile Ruby  
> > code to native machine code. The answer has always been no. But I keep  
> > wondering how hard it would really be to make this possible.
> > 
> > Ruby is written in C. And when Ruby parses a Ruby script it converts  
> > each statement to a C call. Probably the same calls you can use on your  
> > own in a Ruby C extension. So why wouldn't it be possible to parse a  
> > Ruby script and convert all statements to Ruby C code and put it in a  
> > \*.c file (instead of calling the Ruby C statements directly). This \*.c  
> > file can then be compiled into machine code with a C compiler like gcc.  
> > If each \*.rb file is converted to a C file it could be compiled to a  
> > dynamically loadable library which could then be used on require  
> > statements (just like regular Ruby C extensions).
> > 
> > What I mean is, this...
> > 
> > class Example  
> > &nbsp;&nbsp;def example  
> > &nbsp;&nbsp;&nbsp;&nbsp;puts "Hello World!"  
> > &nbsp;&nbsp;end  
> > end
> > 
> > .... can also be written in C using the Ruby C API, am I right? So why  
> > wouldn't it be possible to convert all Ruby code to C code using the  
> > Ruby C API?
> > 
> > This would probably result in some performance gain (no need to parse  
> > the code anymore at run-time), but for some people more important, you  
> > can distribute your Ruby applications closed-source. In the future the  
> > performance gain maybe could be increased by performing special  
> > optimizations during the conversion process.

> I remember that a long time ago, there was a ruby-to-c compiler (was it  
> called r2c?). But IIRC, there was only little performance gain. Remember

AFAIK it only packed the ruby code as a C string and called eval. Very  
clever compiler.

> **···**
>
> --  
> Best regards, emailto: scholz at scriptolutions dot com  
> Lothar Scholz [http://www.ruby-ide.com](http://www.ruby-ide.com)  
> CTO Scriptolutions Ruby, PHP, Python IDE 's

---

<div class="post-metadata">

**Author:** ![Kristof\_Bastiaensen1](https://avatars.discourse-cdn.com/v4/letter/k/9fc29f/32.png) [@Kristof\_Bastiaensen1](https://rubytalk.org/u/Kristof_Bastiaensen1)\
**Post date:** [19 July 2004 15:37 UTC](https://rubytalk.org/t/compiling-ruby-code/12652/13 "2004-07-19T15:37:18Z")

</div>

Hi,

> Kristof Bastiaensen \<kristof@vleeuwen.org\> writes:
> 
> > Well, I do think it is possible to compile Ruby, but it would be to hard.  
> > Firstly eval and module\_eval should be thrown away, because they need to  
> > be able to parse code at runtime.  
> > Continuations make compiling very messy, since the stack needs to be  
> > copied.
> 
> That's not strictly true. If you convert the code to  
> continuation-passing style, reifying continuations doesn't require  
> copying the stack. CHICKEN and many other Scheme compilers choose this  
> approach.
> 
> &nbsp;&nbsp;mikael

That's interesting. Could you explain how that works?  
My idea was that when a continuation is saved, it needs  
to keep the information, where it is going to (return  
adresses), and all local bindings. I would think the best  
way is to save the stack, at least for compiled (machine)  
code.

Kristof

> **···**
>
> On Tue, 20 Jul 2004 00:00:22 +0900, Mikael Brockman wrote:

---

<div class="post-metadata">

**Author:** ![David\_Ross2](https://avatars.discourse-cdn.com/v4/letter/d/ed8c4c/32.png) [@David\_Ross2](https://rubytalk.org/u/David_Ross2)\
**Post date:** [19 July 2004 16:11 UTC](https://rubytalk.org/t/compiling-ruby-code/12652/14 "2004-07-19T16:11:50Z")

</div>

> What is your rationale (in detail) for not looking  
> at the Ruby source?

I do not know if I can look at the source to make a  
specification, then start working on a compiler that  
is under a BSD license. Most of the code is copyright  
by Matz and the companies that hired him. I do not  
want any legal complications. --David

> **···**
>
> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> Do you Yahoo!?  
> Vote for the stars of Yahoo!'s next ad campaign!  
> [http://advision.webevents.yahoo.com/yahoo/votelifeengine/](http://advision.webevents.yahoo.com/yahoo/votelifeengine/)

---

<div class="post-metadata">

**Author:** ![Lothar\_Scholz2](https://avatars.discourse-cdn.com/v4/letter/l/f14d63/32.png) [@Lothar\_Scholz2](https://rubytalk.org/u/Lothar_Scholz2)\
**Post date:** [19 July 2004 16:29 UTC](https://rubytalk.org/t/compiling-ruby-code/12652/15 "2004-07-19T16:29:47Z")

</div>

Hello Scott,

> Michael Neumann wrote:
> 
> > Nospam wrote:
> > 
> > > Hi,
> > > 
> > > Once in a while the question pops up if it is possible to compile Ruby  
> > > code to native machine code. The answer has always been no. But I keep  
> > > wondering how hard it would really be to make this possible.
> > > 
> > > Ruby is written in C. And when Ruby parses a Ruby script it converts  
> > > each statement to a C call. Probably the same calls you can use on  
> > > your own in a Ruby C extension. So why wouldn't it be possible to  
> > > parse a Ruby script and convert all statements to Ruby C code and put  
> > > it in a \*.c file (instead of calling the Ruby C statements directly).  
> > > This \*.c file can then be compiled into machine code with a C compiler  
> > > like gcc. If each \*.rb file is converted to a C file it could be  
> > > compiled to a dynamically loadable library which could then be used on  
> > > require statements (just like regular Ruby C extensions).
> > > 
> > > What I mean is, this...
> > > 
> > > class Example  
> > > &nbsp;&nbsp;def example  
> > > &nbsp;&nbsp;&nbsp;&nbsp;puts "Hello World!"  
> > > &nbsp;&nbsp;end  
> > > end
> > > 
> > > .... can also be written in C using the Ruby C API, am I right? So why  
> > > wouldn't it be possible to convert all Ruby code to C code using the  
> > > Ruby C API?
> > > 
> > > This would probably result in some performance gain (no need to parse  
> > > the code anymore at run-time), but for some people more important, you  
> > > can distribute your Ruby applications closed-source. In the future the  
> > > performance gain maybe could be increased by performing special  
> > > optimizations during the conversion process.
> > 
> > I remember that a long time ago, there was a ruby-to-c compiler (was it  
> > called r2c?). But IIRC, there was only little performance gain. Remember  
> > that you still need a Ruby parser, due to "eval". It would be nice, but  
> > I'd even more like to see a bytecode compiler (written in pure Ruby  
> > running on top of the bytecode interpreter).
> > 
> > Regards,
> > 
> > &nbsp;&nbsp;Michael
> 
> Yes, I have to vote for a bytecode compiler. Right now I'm using ruby  
> to develop some software on an embedded arm-linux device. I've found  
> that for this application, which is not very demanding of the system,  
> that ruby can perform pretty comparably to an equivalent C program. And  
> the development time it saves to write in a higher level language is  
> worth its weight in gold. But every time I run a ruby program it takes  
> it a significant and noticable amount of time to start up. This is  
> obviously due to the ruby parser compiling the text into bytecode every  
> time. If it was possible to precompile this bytecode and put that on the  
> target machine it would have significant advantages. Also, it would make  
> it possible to distribute the application without distributing the  
> source code to it. That isn't so important for us, but it may be for  
> others.

Use ExErb, in version 3.2 it stores the node trees. So this is true  
anymore. But from my experience i'm not sure if parsing is the time  
killer. I think it is building up the whole method universe, filling  
the method lookup caches and do other housekeepings.

If you want to check this, do a "cat \* \> out.rb" and feed the huge  
"out.rb" into the "yy\_compile" function, which only generates a node  
tree but not does the method building.

> **···**
>
> --  
> Best regards, emailto: scholz at scriptolutions dot com  
> Lothar Scholz [http://www.ruby-ide.com](http://www.ruby-ide.com)  
> CTO Scriptolutions Ruby, PHP, Python IDE 's

---

<div class="post-metadata">

**Author:** ![Mikael\_Brockman1](https://avatars.discourse-cdn.com/v4/letter/m/8baadc/32.png) [@Mikael\_Brockman1](https://rubytalk.org/u/Mikael_Brockman1)\
**Post date:** [19 July 2004 16:15 UTC](https://rubytalk.org/t/compiling-ruby-code/12652/16 "2004-07-19T16:15:21Z")

</div>

Kristof Bastiaensen \<kristof@vleeuwen.org\> writes:

> Hi,
> 
> > Kristof Bastiaensen \<kristof@vleeuwen.org\> writes:
> > 
> > > Well, I do think it is possible to compile Ruby, but it would be to hard.  
> > > Firstly eval and module\_eval should be thrown away, because they need to  
> > > be able to parse code at runtime.  
> > > Continuations make compiling very messy, since the stack needs to be  
> > > copied.
> > 
> > That's not strictly true. If you convert the code to  
> > continuation-passing style, reifying continuations doesn't require  
> > copying the stack. CHICKEN and many other Scheme compilers choose this  
> > approach.
> > 
> > &nbsp;&nbsp;mikael
> 
> That's interesting. Could you explain how that works?  
> My idea was that when a continuation is saved, it needs  
> to keep the information, where it is going to (return  
> adresses), and all local bindings. I would think the best  
> way is to save the stack, at least for compiled (machine)  
> code.

In continuation-passing style, no expressions ever return. When you  
rewrite an expression into CPS, the value that it would return is  
instead applied to a function that the expression receives as an  
argument. For example,

&nbsp;&nbsp;f (g x)

is rewritten as

&nbsp;&nbsp;(\k -\> g x (\v -\> f v k))

where \v -\> e denotes the function from v to e. Every program can be  
converted to this style. In this style, call/cc is a trivial operation,  
since every expression receives its reified continuation as an argument.

You might want to read Danvy's ``Three Steps for the CPS  
Transformation''[1] and Baker's ``CONS Should Not CONS Its Arguments,  
Part II: Cheney on the MTA''[2].

[1] [http://www.daimi.au.dk/~danvy/Papers/3steps.ps.gz](http://www.daimi.au.dk/~danvy/Papers/3steps.ps.gz)  
[2] [http://home.pipeline.com/~hbaker1/CheneyMTA.html](http://home.pipeline.com/~hbaker1/CheneyMTA.html)

&nbsp;&nbsp;mikael

> **···**
>
> > On Tue, 20 Jul 2004 00:00:22 +0900, Mikael Brockman wrote:

---

<div class="post-metadata">

**Author:** ![Scott\_Rubin1](https://avatars.discourse-cdn.com/v4/letter/s/22d042/32.png) [@Scott\_Rubin1](https://rubytalk.org/u/Scott_Rubin1)\
**Post date:** [19 July 2004 17:12 UTC](https://rubytalk.org/t/compiling-ruby-code/12652/17 "2004-07-19T17:12:07Z")

</div>

Lothar Scholz wrote:

> Hello Scott,
> 
> \> Michael Neumann wrote:
> 
> > > Nospam wrote:
> > > 
> > > > Hi,
> > > > 
> > > > Once in a while the question pops up if it is possible to compile Ruby  
> > > > code to native machine code. The answer has always been no. But I keep  
> > > > wondering how hard it would really be to make this possible.
> > > > 
> > > > Ruby is written in C. And when Ruby parses a Ruby script it converts  
> > > > each statement to a C call. Probably the same calls you can use on  
> > > > your own in a Ruby C extension. So why wouldn't it be possible to parse a Ruby script and convert all statements to Ruby C code and put  
> > > > it in a \*.c file (instead of calling the Ruby C statements directly).  
> > > > This \*.c file can then be compiled into machine code with a C compiler  
> > > > like gcc. If each \*.rb file is converted to a C file it could be compiled to a dynamically loadable library which could then be used on  
> > > > require statements (just like regular Ruby C extensions).
> > > > 
> > > > What I mean is, this...
> > > > 
> > > > class Example  
> > > > def example  
> > > > &nbsp;&nbsp;&nbsp;puts "Hello World!"  
> > > > end  
> > > > end
> > > > 
> > > > .... can also be written in C using the Ruby C API, am I right? So why  
> > > > wouldn't it be possible to convert all Ruby code to C code using the  
> > > > Ruby C API?
> > > > 
> > > > This would probably result in some performance gain (no need to parse  
> > > > the code anymore at run-time), but for some people more important, you  
> > > > can distribute your Ruby applications closed-source. In the future the  
> > > > performance gain maybe could be increased by performing special optimizations during the conversion process.
> > > 
> > > I remember that a long time ago, there was a ruby-to-c compiler (was it  
> > > called r2c?). But IIRC, there was only little performance gain. Remember  
> > > that you still need a Ruby parser, due to "eval". It would be nice, but  
> > > I'd even more like to see a bytecode compiler (written in pure Ruby  
> > > running on top of the bytecode interpreter).
> > > 
> > > Regards,
> > > 
> > > Michael
> 
> \> Yes, I have to vote for a bytecode compiler. Right now I'm using ruby  
> \> to develop some software on an embedded arm-linux device. I've found  
> \> that for this application, which is not very demanding of the system,  
> \> that ruby can perform pretty comparably to an equivalent C program. And  
> \> the development time it saves to write in a higher level language is  
> \> worth its weight in gold. But every time I run a ruby program it takes  
> \> it a significant and noticable amount of time to start up. This is  
> \> obviously due to the ruby parser compiling the text into bytecode every  
> \> time. If it was possible to precompile this bytecode and put that on the  
> \> target machine it would have significant advantages. Also, it would make  
> \> it possible to distribute the application without distributing the  
> \> source code to it. That isn't so important for us, but it may be for  
> \> others.
> 
> Use ExErb, in version 3.2 it stores the node trees. So this is true  
> anymore. But from my experience i'm not sure if parsing is the time  
> killer. I think it is building up the whole method universe, filling  
> the method lookup caches and do other housekeepings.
> 
> If you want to check this, do a "cat \* \> out.rb" and feed the huge  
> "out.rb" into the "yy\_compile" function, which only generates a node  
> tree but not does the method building.

Lothar,

I would  
&nbsp;&nbsp;use ExErb, except for one thing. I'm a Linux user. I have 4 computers and the target development board at my disposal. All of them run Linux. I wont lecture you on the usual Linux vs. Windows stuff, that's a waste of your time and mine. But yeah, exerb really wont help me in this case.

Every time a ruby program is launched there is a lot of up-front processing that gets done. I just need something that can alleviate some or all of this up-front processing by storing the results on disk. I don't care how it works or what it does as long as it makes programs launch faster. And it has to work on all ruby supported operating systems. Python does something like this, but python is no good for my embedded development since it requires too much disk space and it requires gcc. Ruby I can fit in a very small package (1.8MB IIRC) and it cross-compiles for arm nicely.

Thanks for your help though.

-Scott

---

<div class="post-metadata">

**Author:** ![SER1](https://avatars.discourse-cdn.com/v4/letter/s/3e96dc/32.png) [@SER1](https://rubytalk.org/u/SER1)\
**Post date:** [20 July 2004 14:02 UTC](https://rubytalk.org/t/compiling-ruby-code/12652/18 "2004-07-20T14:02:12Z")

</div>

David Ross \<drossruby@yahoo.com\> wrote in message news:\<20040719161148.44048.qmail@web21529.mail.yahoo.com\>...

> I do not know if I can look at the source to make a  
> specification, then start working on a compiler that  
> is under a BSD license. Most of the code is copyright  
> by Matz and the companies that hired him. I do not  
> want any legal complications. --David

Hey David,

I just wanted to let you know that I'm all for you exploring this  
(Ruby-to-native compiler). I'm a little distressed that the first  
responses that you got to your posting were "it can't be done," "it  
will be harder than you think," "you won't get the performance gain  
you expect," etc., etc.. I think a native Ruby compiler \*is\*  
possible, although some aspects will be difficult, and I'd \*love\* to  
see a good one.

You might talk to Robert Feldt, or take a look at RockIt. I suspect  
that Robert has a formal grammar for Ruby floating around somewhere  
and has a pretty good specification in his head if not on paper. It  
would be a pretty good place to start.

There are a number of unit tests for Ruby and Ruby code in the Ruby  
CVS repository. You should be able to \*use\* those without violating  
any white-room requirements you have.

Finally, you might just email Matz, and ask him to give you a  
specific, written exemption from whatever IP or copyright restrictions  
you're concerned about for this specific project.

--- SER

---

<div class="post-metadata">

**Author:** ![Bill\_Kelly](https://avatars.discourse-cdn.com/v4/letter/b/cc9497/32.png) [@Bill\_Kelly](https://rubytalk.org/u/Bill_Kelly)\
**Post date:** [28 July 2004 07:12 UTC](https://rubytalk.org/t/compiling-ruby-code/12652/19 "2004-07-28T07:12:04Z")

</div>

Hi,

> \> it possible to distribute the application without distributing the  
> \> source code to it. That isn't so important for us, but it may be for  
> \> others.
> 
> Use ExErb, in version 3.2 it stores the node trees.

I read this with much interest (!) ... However I just  
tried out exerb-3.2.0 and searched through the binary  
and was able to see my source code there. I was hoping  
that if it stores the node trees, my source code would  
no longer be in ASCII form. Did I misunderstand?

Thanks,

Regards,

Bill

> **···**
>
> From: "Lothar Scholz" \<mailinglists@scriptolutions.com\>

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard6](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Ara.T.Howard6](https://rubytalk.org/u/Ara.T.Howard6)\
**Post date:** [19 July 2004 17:42 UTC](https://rubytalk.org/t/compiling-ruby-code/12652/20 "2004-07-19T17:42:07Z")

</div>

how about a simple 'ruby-interpreter' server written as a drb object. the  
server could simply start ruby via a fork'd process or via a pipe, and run the  
code in question; something similar in spirit to mod\_ruby. you would  
eliminate the start up costs only with this method, but your post seem to  
suggest this might be o.k./sufficient

-a

> **···**
>
> On Mon, 19 Jul 2004, Scott Rubin wrote:
> 
> > I would  
> > use ExErb, except for one thing. I'm a Linux user. I have 4 computers and the target development board at my disposal. All of them run Linux. I wont lecture you on the usual Linux vs. Windows stuff, that's a waste of your time and mine. But yeah, exerb really wont help me in this case.
> > 
> > Every time a ruby program is launched there is a lot of up-front processing that gets done. I just need something that can alleviate some or all of this up-front processing by storing the results on disk. I don't care how it works or what it does as long as it makes programs launch faster. And it has to work on all ruby supported operating systems. Python does something like this, but python is no good for my embedded development since it requires too much disk space and it requires gcc. Ruby I can fit in a very small package (1.8MB IIRC) and it cross-compiles for arm nicely.
> > 
> > Thanks for your help though.
> > 
> > -Scott
> 
> # --
> 
> > EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov  
> > PHONE :: 303.497.6469  
> > A flower falls, even though we love it;  
> > and a weed grows, even though we do not love it. --Dogen
> 
> ===============================================================================

[Next page](https://rubytalk.org/t/compiling-ruby-code/12652.md?page=2)
