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?

有帮助吗?

解决方案 2

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.

其他提示

It's half-possible.

  • Download extension from here and compile it.
  • Require lib iseq.so
  • ok, now load method for bytecode available

Example (compilator/loader)

require "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

it's usage

$ ruby compilator.rb compile my_project.rb # => compile to bytecode
$ ruby compilator.rb load my_project.bin   # => load from bytecode and eval

Note

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).

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top