From: Dominik Bathon Date: 2006-08-01T04:54:59+09:00 Subject: Re: converting some autogenerated ruby code to C Hi Eric, On Mon, 31 Jul 2006 05:02:08 +0200, Eric Mahurin wrote: > ruby : 19.2s > rubyToAnsiC : 0.7s > * assumed argument was a Fixnum > * converted downto iterator to a while loop > * didn't handle overflow properly and use Bignum - gave wrong answer > rubyToRubyC : 20.9s > * converted downto iterator to a while loop > * 3 messages sent (rb_funcall) per iteration of the inner loop > hand-optimized rubyC : 14.8s > * precalculated interns/symbols for method calls > * reordered operands so that a constant operand was the receiver if > possible > * used rb_funcall2 instead of rb_funcall > downto iterator rubyC : 15.3s > * more direct translation of original ruby > * used downto iterator and block (needed a couple extra helper > functions) > * precalculated interns/symbols for method calls > * one block call and one message sent per iteration of the inner loop > > For my purposes, rubyToAnsiC is pretty much useless since I need duck > typing on most of my arguments (can't infer the type). I also found > that rubyToC wasn't very robust - simply changing n.downto(2) to > n.step(2,-1) broke it. I think a more direct translation would be > better. I don't see a lot of benefit from the other solutions, so > I'll pursue other avenues for optimization or just wait for YARV. You could try Ruby2CExtension (http://ruby2cext.rubyforge.org/), it is similar to rubyToRubyC but supports blocks and closures and tries to match Ruby's semantics as close as possible. It can handle most Ruby code. You should first try the 0.1.0 release and see if it works with your Ruby code (also read the limitations document). This version doesn't have any real optimizations, so the compiled code will probably only be a bit faster than the pure Ruby version. Once that works you can try the HEAD revision of svn://rubyforge.org/var/svn/ruby2cext/trunk. This new version has constant lookup caching and optimizes calls to most public methods (including operators) of builtin classes (Array, Bignum, FalseClass, Fixnum, Float, Hash, NilClass, Regexp, String, Symbol, TrueClass) that don't do anything with blocks. This can produce significant speedups depending on your code. I am currently thinking about doing optimizations for common iterators like Array/Range#each, map, ... and Fixnum#times, upto, ... which should yield further speedups. Dominik