Loading precompiled code

Hi,

Does ruby support storing and loading bytecode !!

Thanks

As far as I know, there is no bytecode available from the Ruby compiler.
I
have never seen any reference to it. JRuby does compile down to Java
bytecodes.

2009/8/4 Husam S. [email protected]

Hi,

Does ruby support storing and loading bytecode !!

Thanks


Regards,

Ken

Seek wisdom through disbelief

Will 1.9.1 has bytecode and you can get it using
“RubyVM::InstructionSequence.compile_file”, but it doesn
looks like there is an APIs you can use to load them.

This how I manage to store it.

code = RubyVM::InstructionSequence.compile_file ‘sample.rb’

File.open “simple.rbvm”, “w” do |file|
file << Marshal.dump(code.to_a)
end

Thanks thats clear things up :slight_smile:

Husam S. wrote:

Does ruby support storing and loading bytecode !!

Short answer: No, it doesn’t.

(Slightly) longer answer: No, Ruby doesn’t support storing and
loading bytecode, but most Ruby Implementations do. IOW: it’s not
part of the Ruby Language Specification, but most Ruby Implementations
support it anyway. However, each Implementation has its own bytecode,
its own archive format and its own APIs. And, of course, some
implementations don’t even have bytecode, so it’s obviously
impossible for them to support bytecode loading.

(Much too) long answer: There is no portable bytecode specification
for Ruby, and thus also no standard way to load precompiled bytecode
archives. However, almost all Ruby implementations use some kind of
bytecode or intcode format, and several of them can dump and reload
bytecode archives.

YARV (http://Ruby-Lang.Org/) always compiles to bytecode before
executing the code, however that is usually only done in memory. There
are ways to dump out the bytecode to disk. At the moment, there is no
way to read it back in
(https://GitHub.Com/RubySpec/MatzRuby/blob/trunk/iseq.c#L1427-1429),
however. This will change in the future: work is underway on a
bytecode verifier for YARV, and once that is done, bytecode can safely
be loaded into the VM, without fear of corruption. Also, the JRuby
developers have indicated that they are willing to implement a YARV VM
emulator
(https://GitHub.Com/JRuby/JRuby/blob/da23e51968ffc17d750fca340f4b9538e5e2b425/src/org/jruby/ast/executable/YARVMachine.java)
inside JRuby, once the YARV bytecode format and verifier are
stabilized, so that you could load YARV bytecode into JRuby. (Note
that this version is obsolete
(https://GitHub.Com/JRuby/JRuby/commit/2257c8f811fab8d675a942c86ab712922dc46c6f/).)

Rubinius (http://Rubini.us/) also always compiles to bytecode, and
it has a format for compiled files
(https://GitHub.Com/EvanPhx/Rubinius/blob/master/kernel/compiler/compiled_file.rb)
(’.rbc’ files, analogous to JVM ‘.class’ files). There is a chance
that Rubinius might implement a YARV emulator, if deploying apps as
YARV bytecode ever becomes popular. Also, the JRuby developers have
indicated that they are willing to implement a Rubinius bytecode
emulator
(https://GitHub.Com/JRuby/JRuby/blob/da23e51968ffc17d750fca340f4b9538e5e2b425/src/org/jruby/ast/executable/RubiniusMachine.java)
inside JRuby, if Rubinius bytecode becomes a popular way of deploying
Ruby apps. (Note that this version is also obsolete
(https://GitHub.Com/JRuby/JRuby/commit/2257c8f811fab8d675a942c86ab712922dc46c6f/).)

XRuby (http://XRuby.GoogleCode.Com/) is a pure compiler, it compiles
Ruby sourcecode straight to JVM bytecode (’.class’ files). You can
deploy these ‘.class’ files just like any other Java application.

JRuby (http://JRuby.Org/) started out as an interpreter, but it has
both a JIT compiler and an AOT compiler
(https://GitHub.Com/JRuby/JRuby/tree/master/src/org/jruby/compiler/)
(‘jrubyc’) that can compile Ruby sourcecode to JVM bytecode (’.class’
files). Also, work is underway to create a new
(https://GitHub.Com/JRuby/JRuby/blob/master/tool/compiler2.rb)
compiler that can compile (type-annotated) Ruby code to JVM bytecode
that actually looks like a Java class and can be used from Java code
without barriers.

Ruby.NET (http://RubyDotNETCompiler.GoogleCode.Com/) is a pure
compiler that compiles Ruby sourcecode to CIL bytecode (PE ‘.dll’ or
‘.exe’ files). You can deploy these just like any other CLI
application.

IronRuby (http://IronRuby.Net/) also compiles to CIL bytecode, but
typically does this in-memory. However, you can pass commandline
switches
(https://GitHub.Com/IronRuby/IronRuby/blob/master/Merlin/Main/Languages/Ruby/Ruby/Hosting/RubyOptionsParser.cs#L275-276)
to it, so it dumps the ‘.dll’ and ‘.exe’ files out to disk. Once you
have those, they can be deployed normally.

BlueRuby
(https://SDN.SAP.Com/irj/scn/wiki?path=/display/Research/BlueRuby)
automatically pre-parses Ruby sourcecode into BRIL (BlueRuby
Intermediate Language), which is basically a serialized parsetree.
(See Blue Ruby - A Ruby VM in SAP ABAP
(https://SDN.SAP.Com/irj/scn/go/portal/prtroot/docs/library/uuid/408a9a3b-03f9-2b10-b29c-f0a3374b19d8)
for details.)

I think (but I am definitely not sure) that there is a way to get
Cardinal (https://GitHub.Com/Cardinal/Cardinal/) to dump out Parrot
(http://ParrotCode.Org/) bytecode archives (PBC). (Actually,
Cardinal only compiles to PAST, and then Parrot takes over, so it
would be Parrot’s job to dump and load bytecode archives.)

MRI doesn’t have a bytecode.

HotRuby (http://HotRuby.Accelart.Jp/) and Red Sun
(https://GitHub.Com/JonathanBranam/RedSun/) appear to have the same
bytecode format as YARV, and they appear to be able to load bytecode,
but I am not familiar with them and can’t make a definitive statement.

I am not familiar enough with tinyrb
(http://Code.MACournoyer.Com/tinyrb/) or MagLev
(http://MagLev.GemStone.Com/) to make any statement about them.

jwm

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs