mruby Presentation, now with structure

This is what happens when sarcasm goes too far: I end up doing a lightning talk. There's a warning for those who would be warned.

So I did this lightning talk. Not sure it was particularly easy to follow, so I thought I would do a follow up post. it has been described as a live coding talk; a misnomer since I only actually deleted lines. Consider these your slides.

How do I get it?

Get the git repository from github. Clone, cd into the directory and run "make". Job done (you need bison installed). It compiles pretty quickly.

What is mruby?

mruby is a ruby implementation, but with a compiler. For those who have only ever known bog-standard ruby or javascript, you won't have really encountered a compiler. A compiler takes your nice readable code - like this file, fibber.rb:



def fibbonacci(n)
  return 1 if n < 2
  return fibbonacci(n - 1) + fibbonacci(n - 2)
end

puts fibbonacci 20

... and turns it into something unreadable to normal humans (usually machine code), but that can be run super-fast by a computer. There are lots of different compilers that do different things - for example, while the Coffeescript compiler turns it onto a hideously unreadable language known as Javascript, Java creates an even more unreadable file of what it calls "bytecode", C/C++ compilers turn it into actual machine code instructions that get sent directly to the CPU. If you are using Ruby, Javascript, etc, there is usually a phase where the interpreter does some compilation to make the code run faster, but it never outputs it's newly munged file. In a truly compiled language, the developer compiles the code and distributes the compiled version.

Bear in mind that mruby is still in an alpha stage. Some language features are missing, and the standard library support is not quite up to standard. But it does have an irb - called mirb. I'd suggest you create a directory, or use the examples directory provided to write your code in, but if you're just playing with the language, go to bin/mirb.

mruby can be used like normal ruby. You can run the above file with the mruby binary. But the fun comes with the compiler, mrbc. The compiler doesn't produce machine code - instead, it produces a C file. It is your job to then write more C code that uses your new ruby function.

I'm lazy though, and I don't need the C file, so I wrote a script to do that bit for me. Avert your eyes if you are allergic to nasty hacked together scriptlets. Pop this in a file called "compile_mruby" and run it with:

ruby ./compile_mruby fibber.rb

Note that this expects your code to be inside a folder inside the cloned mruby project (e.g. "examples"), so that you can access the binary from ../bin/mrbc

So, the reason you would compile this would be for the speed increase, right?


mruby, interpreted

IPRUG:: $ time ../bin/mruby fibber.rb 10946 real 0m0.009s user 0m0.005s sys 0m0.002s

mruby, compiled

IPRUG:: $ time ./fibber 10946 real 0m0.008s user 0m0.005s sys 0m0.002s

MRI ruby, 1.9.3, interpreted

IPRUG:: $ time ruby fibber.rb 10946 real 0m0.158s user 0m0.084s sys 0m0.073s

Uhh, ok, so a massive improvement over normal ruby, but only 0.001 of a second improvement between compiled mruby and interpreted. Only ran it once - that's probably just normal inaccuracies. It's faster than MRI ruby though. So why would you bother compiling at all?


A compiled binary...

-rwxr-xr-x 1 matthew staff 337K 6 Mar 00:21 fibber

... vs the a file and its interpreter

-rw-r--r-- 1 matthew staff 110B 6 Mar 00:15 fibber.rb -rwxr-xr-x 1 matthew staff 571K 28 Feb 12:32 ../bin/mruby

... vs the file and MRI binary

-rw-r--r-- 1 matthew staff 110B 6 Mar 00:15 fibber.rb -rwxr-xr-x 1 matthew staff 2.4M 25 Jun 2012 [...]/1.9.3-p194/bin/ruby

(this doesn't take into account standard libraries...)

This footprint size, and the ability to use compiled ruby directly in a C / C++ project, means mruby could be awesome for embedded systems. It's still in active developent and will be an interesting one to watch!