Marshalling Ruby 2.0 Codes #
We’re all eager to see YARV spout real live bytecode, but ko1 has a fun sort of inbetween option: marshalling arrays containing Ruby 2.0 instructions to an .rbc
file. All it is: 4 bytes reading RBCM
and then the marshal.
Assuming you have YARV installed with the -yarv
suffix, go to the root directory of your checked out source code. We’re interested in rb/compile.rb.
ruby-yarv rb/compile.rb ~/svn/camping/lib/camping.rb -o camping.rbc
This isn’t anything that exciting. The .rbc
file will likely be four times the size of your original source. And, in my tests, it’s no quicker to parse. But it is an interesting way to store source code, for sure.
Load it like so:
open("camping.rbc") do |f| exit "Not really an RBCM!" unless f.read(4) == "RBCM" iseq = YARVCore::InstructionSequence.load(Marshal.load(f)) iseq.eval end
This was all covered in issue no. 15 of the Rubyist Magazine, which has a bunch of quick dissections of the instruction set and YARV assembly.
Bil
Speaking of Rubyist Magazine, I miss the guest entries from our Japanese brethren that once graced this blog.
More please.
Logan Capaldo
It could be used as an obfuscation method for those who really wanted it though. (At least if/when YARV becomes “the one true way” to run ruby.)
MonkeeSage
I don’t think that the compile hack would be any faster right now since YARV still has to load the plaintext parser in order to then load the bytecode file. But something interesting about YARV (0.4.1-r519) and speed is that for a couple million random flops, YARV is actually a little over a second faster than plain ruby 1.9/HEAD, and about 4 seconds faster than 1.8.5-r4. I can’t recall ever having a dire need to do alot of random floating point math quickly … but hey, faster is faster. :)
MenTaLguY
Bil: I think I’d also like to see the other sections besides “inspect” get some love.
Dr Nic
Is there an English version of this mag?
adam
Dr Nic: Don’t worry, in the future everyone will speak Ruby. We just have to hope we’re all there soon.
Matt Todd
Everyone is here in the future!
rite
Speed’s in the eye of the creater – MIPs is so out of date – it surely doesn’t serve a purpose with Ruby. When i’m writing firmare during the day MIPs fly by the window – but I’m doing more and more and more of the clever stuff at this level. And Ruby proper JIT will be here in good time – if everyone doesn’t sucumb to the petrol-head attitude. My 0.02
Bil
MenTaLguY: Especially 5.gets.
MenTaLguY
Bil: Yeah, totally.