def fib(n)The next part requires that you have a recent version of Rubinius installed:
if n < 2
n
else
fib(n - 2) + fib(n - 1)
end
end
p fib(15)
rbx compile fib.rbThis will generate fib.rbc. Next, take a recent JRuby version and run:
jruby -R fib.rbcAnd presto, you should see 610 printed quite soon. This is JRuby executing Rubinius bytecode. I was quite happy about how it was to get this far with the functionality. Of course, JRuby doesn't support most bytecodes yet, only those needed to execute this small example, and similar things. We are also using JRuby's internals for this, which means that Rubinius MethodContext and such are not available.
Another interesting note is that running the iterative Fib algorithm like this with -J-server is actually 30% faster than MRI.
This approach is fun, and I have some other similar ideas I really want to look at. The best part about it though, is that I got the chance to look at the internals of Rubinius. I hope to have more time for it eventually. Another thing I really want to do some day is implement a Jubinius, which should be a full port of the Rubinius runtime, possibly excluding Subtend. I think it could be very nice to have the Smalltalk core of Rubinius working together with Java. Of course, I don't have any time for that, so we'll see what happens in a year or two. =) Maybe someone else does it.
2 kommentarer:
I think you might be mistaken about the performance. I'm testing it with fib(30) and it's a about 1.5x slower than normal JRuby interpreted mode, even with ObjectSpace disabled. I'm looking at it now.
I said iterative fib. I know the recursive one isn't as fast; probably because of more overhead in the call path, due to the RubiniusMethod not being optimized.
Skicka en kommentar