From: Jan Wedekind Date: 2008-12-05T10:29:53+09:00 Subject: [ruby-core:20345] Achieving C-like performance with more indirection? I am working on a Ruby-extension for doing real-time computer vision [1]. In the past I've used static C++ code to optimise the computationally expensive parts of the program. Recently I have replaced all this C++ code with Ruby code which makes use of libJIT [2] (i.e. the only remaining C++ code is for I/O integration). The performance currently is about 50% percent of an equivalent C++ implementation and there are still possibilities for optimisation (caching compiled methods, loop-unrolling). Here is an example illustrating the principle: # To compile a function like the following one ... def plus( a, b ) a + b end # ... one can let it perform computations on JIT-variables so that # "plus" will return a block of optimised code instead of a number. fun = JITFunction.compile( JITType::LINT, JITType::LINT, JITType::LINT ) do |f,a,b| plus( a, b ) end As far as I know it is not possible however to apply this approach to functions containing the structural elements "while" or "if-then-else". Of course one could think about interpreting the code of the method "plus" using ruby2ruby or obtaining a parse-tree of "plus" in some way. However this is not really satisfactory. According to [3] this does not really address the problem since this approach leads to an endless tower of reflective interpreters (language interpreted by an interpreter, which is in turn interpreted by an interpreter). [3] points out theoretical work by Friedman and Wand eliminating the tower of interpreters by using reification: "Reification is the conversion of an interpreter component into an object which the program can manipulate." I don't really know Smalltalk. But for example if "while" and "if" where methods, one could optimise more sophisticated Ruby programs by manipulating the behaviour of the Ruby VM in a particular context. Currently I need to write programs which are calling special methods which imitate loops and conditionals (also see Paul Brannan's ruby-libjit): def inc( p, pend, x, f ) f.until( proc { p == pend } ) do LINT.jit_store( LINT.jit_load( f, p ) + x, p ) p.inc!( LINT.size ) end end Has this already been considered before? I'd be interested if there is anything related I should know about. [1] http://rubyforge.org/projects/hornetseye/ [2] http://www.dotgnu.org/libjit-doc/libjit.html [3] http://www.iam.unibe.ch/~scg/Archive/PhD/denker-phd.pdf