【问题标题】:Compiling Ruby 2.1 to bytecode将 Ruby 2.1 编译为字节码
【发布时间】:2014-01-20 05:08:18
【问题描述】:

Ruby 1.9 was compiling to bytecode 时,无法将预编译的脚本保存到磁盘。

我们被告知要等待Ruby 2 to allow saving compiled bytecode 到磁盘,但我没有听到太多关于这个的讨论,也没有看到大量描述如何通过编译获得性能的博客文章,我希望看看它是否真的如此在 Ruby 2.x 的某个地方实现。

focused Google search 似乎没有返回任何有用的信息。

在 2.1(或更早版本)中可以吗?如果没有,这是否仍在路线图中?

【问题讨论】:

标签: ruby ruby-2.0 ruby-2.1


【解决方案1】:

通过阅读 ruby​​-core(我是提交者),我还没有看到任何这方面的工作。在 ruby​​ 中编译为字节码不需要很长时间。与大多数 ruby​​ 程序的运行时相比,将编译后的字节码保存到磁盘还没有意义。

【讨论】:

    【解决方案2】:

    有一半可能。

    • here 下载扩展并编译。
    • 需要库iseq.so
    • 好的,现在load 字节码方法可用

    示例(编译器/加载器)

    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
    

    它的用法

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

    注意

    对于最简单的项目,它适用于同一个 ruby​​ 解释器(1.9.3 和 2.x.x 不兼容)。但是对于稍微复杂一点的项目,它就不起作用了(分段错误)。

    【讨论】:

      猜你喜欢
      • 2018-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-08
      相关资源
      最近更新 更多