Ruby 自己编译, 让 Ruby 加速 7~10%(亲测有效)

rocLv · November 13, 2016 · Last by mimosa replied at November 14, 2016 · 2508 hits

首先 clone ruby 源代码

$ git clone --branch v2_3_1 git://github.com/ruby/ruby.git --depth=1

然后修改编译参数

$ export optflags='-O3 -fno-fast-math -mllvm -inline-threshold=5000'

之后运行

$ autoconf
$ ./configure && make clean && make

下图是自编译 Ruby 和用 rvm 默认安装的 Ruby 性能前后比较

自编译 RVM自动安装

性能大约提升8%

系统配置为:

具体原理可以参考这篇文章

PS:

原理是通过改变 inline 函数的长度减少了函数调用花费的时间。

这样做的一个副作用是应用占的内存会增加。

其实是 clang 比 gcc 快,之前 ruby weekly 好像发过

默认编译参数:

ruby -r rbconfig -e 'RbConfig::CONFIG.sort.each { |c| p c }'["optflags", "-O3 -fno-fast-math"]
・
・

rbenv 可以这样:

OPTFLAGS='-O3 -fno-fast-math -mllvm -inline-threshold=5000' rbenv install 2.3.1

ruby -r rbconfig -e 'puts RbConfig::CONFIG["OPTFLAGS"]'-O3 -fno-fast-math -mllvm -inline-threshold=5000
5 Floor has deleted
You need to Sign in before reply, if you don't have an account, please Sign up first.