While Ruby 1.9 was compiling to bytecode, it could not save a pre-compiled script to disk.
We were told to expect Ruby 2 to allow saving compiled bytecode to disk, but I haven't heard much talk of that, or seen bazillions of blog posts describing how to gain performance through compilation, which I would expect to see if it were in fact implemented somewhere in Ruby 2.x.
A focused Google search doesn't seem to return anything useful.
Is it possible in 2.1 (or earlier)? If not, is this still on the roadmap?
iseq.so
load
method for bytecode availablerequire "zlib"
require "./ext/iseq" # ./ext/iseq.so
# ARGV[0] compile or load
# ARGV[1] file to compile or load
case ARGV[0]
when 'compile'
File.open("#{File.basename(ARGV[1],'.rb')}.bin",'w') do |f|
f << Zlib::Deflate.deflate(
Marshal.dump(
ISeq.compile_file( ARGV[1] ).to_a
)
)
end
when 'load'
( File.open( ARGV[1], 'r') do |f|
ISeq.load(
Marshal.restore(
Zlib::Inflate.inflate( f.read )
)
)
end ).eval
else
puts 'need options'
end
$ ruby compilator.rb compile my_project.rb # => compile to bytecode
$ ruby compilator.rb load my_project.bin # => load from bytecode and eval
For simplest project it's works on the same ruby interpreeter (1.9.3 and 2.x.x are incompatible). But for slightly more complicated project it doesn't work (segmentation fault).
I haven't seen any work in this direction from reading ruby-core (I am a committer). Compilation to bytecode doesn't take very long in ruby. Compared to the runtime of most ruby programs it hasn't yet made sense to persist compiled bytecode to disk.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With