📜 ⬆️ ⬇️

MacRuby Compilation Features

Foreword


I recently listened to the Ruby NoName Podcast (by the way, I highly recommend it to everyone), in which I heard the news that MacRuby 0.5 had left the beta stage. Also, the presenters said that the MacRuby package includes a great Ruby compiler - MacRubyC, which can now compile into dynamic libraries. Naturally, I became interested and decided to experiment.

Process


So what are we doing? Go to the official MacRuby website and download the latest version of the package. Unpack the archive, set. Next, run the Terminal and try:
Trial of the pen in irb
Everything is working? Fine.

Now let's taste the MacRubyC compiler. To do this, we need any .rb file, for example, hello.rb, containing just one line:
puts "Hello world!"

We write in the terminal:
macrubyc hello.rb -o hello

The output is a neat executable file that you can safely run as a regular Unix application:
Test the first compiled program in Ruby
')
However, if we try to run our program on another machine, where MacRuby does not cost, the result will be disappointing - the program lacks the MacRuby dynamic library:
Disappointing result
There is a way out - static compilation.

But even here it’s not so simple: if you don’t get LLVM , then the program will not compile:
Mistake

The most logical way out is to put this very LLVM:
svn co -r 82747 llvm.org/svn/llvm-project/llvm/trunk llvm-trunk
cd llvm-trunk
./configure
UNIVERSAL=1 UNIVERSAL_ARCH="i386 x86_64" ENABLE_OPTIMIZED=1 make -j2
sudo env UNIVERSAL=1 UNIVERSAL_ARCH="i386 x86_64" ENABLE_OPTIMIZED=1 make install

I warn you in advance, LLVM is going to take quite a long time, it took me about an hour.

Most delicious


Our programs can easily compile in statics and, accordingly, run on someone else's machine. It is wonderful. Well, let's do a little research.

First, the size: the source code hello.rb weighs only 19 bytes. If we look at the size of the first binary, we see that it weighs only 13.6 kilobytes. Not so much as it may seem. But we compiled it in dynamics! Let's take a look at the size of a binary file compiled in statics - 14.3 megabytes! And this is only “Hello world”. However, you should not worry too much: in fact, with an increase in the amount of code, the size of the executable file will not so much increase. It is quite obvious that in these 14 megabytes libraries are packed, which the program inadvertently drags along.

Now let's touch the most interesting - performance.

In order to measure performance, we will write a program consisting of just one line:
10000000.times { |i| puts i }

Then we compile our program in statics and dynamics, respectively, with the --static option and without it:
macrubyc cycle.rb -o cycle
macrubyc cycle.rb --static -o cycle-static

In order to see how fast (or slow) our programs are executed, we use the built-in Unix utility time.
Slowly, however!
The measurements were taken several times, but the result is generally the same. What do we see? We see that the program executed by the interpreter runs almost an order of magnitude faster than the standard program. At the same time, static compilation wins a few seconds from dynamic.

But if we take tests more seriously, for example, the Mandelbrot function , then everything falls into place: the compiled program runs an order of magnitude faster — only 0.32 seconds, while the interpreted program runs for about 8 seconds. Similar results are observed if the Takeyuchi function is taken as a test function (about 21 seconds for interpretation and 0.5 seconds for compilation):
Takeuchi function in action

Instead of epilogue


That's all. On the whole, the conclusions are ambiguous: on the one hand, the compiled program lags behind in linear cases, on the other hand, when compiling, good optimization is used, which gives a good result in more complex cases. However, the decision on the use of compilation in your projects to take you.

Source: https://habr.com/ru/post/84710/


All Articles