Compiling Ruby 1.9 with GCC 4.4

28 04 2009

Got the 1.9 pickaxe, so it’s definitely time to get Ruby 1.9 installed alongside 1.8. It turns out that if you try to build Ruby 1.9 with GCC 4.4 by means of the typical ./configure; make; sudo make install, you will hit a wall on the make step. I’m running into this while evaluating Fedora11 snap1, still in beta. The issue is that GCC 4.4 introduces a few changes that will impede ruby 1.9 from compiling without some minor adjustments.

These are the steps to get going:
1. Download, verify and unpack Ruby 1.9.
$ wget ftp://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.1-p0.tar.gz
$ md5sum ruby-1.9.1-p0.tar.gz
50e4f381ce68c6de72bace6d75f0135b ruby-1.9.1-p0.tar.gz #verify that hash matches
$ tar xvf ruby-1.9.1-p0.tar.gz
$ cd ruby-1.9.1-p0

At this point, we have verified and extracted the ruby 1.9 source code. Go ahead and create your platform’s compile configuration. There are many flags you can pass. To see them all, type ./configure –help. I’ve chosen to keep the libraries in their default locations, and suffix the binaries with ’19’ (leaving your 1.8 ruby installation intact). Some folks prefer to install it on a completely different path to avoid this very issue, by using the –prefix-path flag instead. That’s up to you.
./configure --program-suffix=19 --disable-pthread
At this point, you may be tempted to type “make”. If you do, you’ll get the following error:

--snip--
cont.c:90:6: error: #elif with no expression
cont.c:270:6: error: #elif with no expression
cont.c:317:6: error: #elif with no expression
make: *** [cont.o] Error 1


The resolution is to edit the cont.c file to go on. Open it up, and change the lines 90, 270 and 317 from #elif to #else.

Now, carry on as usual:
$ make
$ make test #make sure everything's sane
$ sudo make install #or su -, or whatever it is you kids use to gain admin privileges these days

You’re done. irb will open up the console on ruby 1.8, and irb19 will open it in ruby 1.9. Similarly, point your scripts to the ruby19 binary instead of ruby.

You can now try out some of the new ruby 1.9 features in irb19:
$irb19
irb(main):001:0> [1,2].inject(:+) #new injections
=> 3
irb(main):002:0> {a: 'a_val', b: 'b_val'} #new hash synthax
=> {:a=>"a_val", :b=>"b_val"}
irb(main):003:0> p = -> a,b,c {a+b+c} #lambda shorthand
=> #
irb(main):004:0> Time.now.tuesday?
=> true
irb(main):005:0> beta_string = 'ß' #utf-8 strings
=> "ß"
irb(main):006:0> π = 3.14 # and variables
=> 3.14
irb(main):007:0> π * 5
=> 15.7

Now, let’s see what the fuzz is all about. I tried running the the full ruby benchmark suite, but I got a nasty kernel panic:Apr 27 23:03:45 hgnux kernel: Critical temperature reached (95 C), shutting down.Guess I need a laptop that sucks less to run it. This isn’t over though: I’ll stick my laptop in the freezer if I have to, just to run the full benchmark suite.

Instead, let’s run and benchmark the most inefficient piece of crap on both versions of ruby. Recursion will do the trick. Let’s calculate factorials and fibonaccis left and right. Save this script somewhere:

#recursion.rb
require 'benchmark'
include Benchmark
def fact(n)
  n == 1 ? 1 : n*fact(n-1)
end

def fib(n)
  if n == 0 || n == 1
    n
  else
    fib(n-1) + fib(n-2)
  end
end

puts "Ruby #{RUBY_VERSION} patch #{RUBY_PATCHLEVEL}"

bm do |r|
  r.report do 
    (1..35).each do |n|
      fact(n)
      fib(n)
    end
  end
end

Now, invoke it with both your ruby and your ruby19 binaries:

Ruby 1.8 vs. Ruby 1.9 running recursion.rb

Ruby 1.8 vs. Ruby 1.9 running recursion.rb


$ ruby recursion.rb 
Ruby 1.8.6 patch 287
      user     system      total        real
155.320000  45.950000 201.270000 (229.181959)


$ ruby19 recursion.rb 
Ruby 1.9.1 patch 0
      user     system      total        real
 16.030000   0.100000  16.130000 ( 16.852891)

Sweet! I’ve also included the results of vmstat + vmplot of these runs. Most impressive is the minimized number of system interrupts on the ruby 1.9 version.

References:
Ruby downloads: http://www.ruby-lang.org/en/downloads/
GCC 4.4 porting notes: http://gcc.gnu.org/gcc-4.4/porting_to.html
Extended vmplot: http://ronaldbradford.com/blog/extending-vmplot-2009-03-31/
Ruby benchmark suite: http://github.com/acangiano/ruby-benchmark-suite/tree/master





Run commands on boot for Fedora Core.

21 08 2006

Content moved to http://awesomeful.net/posts/21-run-commands-on-boot-for-fedora-core





Installing vanilla Tomcat on Fedora Core.

21 08 2006

Content moved to http://awesomeful.net/posts/20-installing-vanilla-tomcat-on-fedora-core