PHP has different opcode caches like APC, Zend Optimizer to c开发者_JAVA技巧ache the code and dramatically speed things up. Does Ruby have something similar?
The default Ruby 1.9.x is based on a bytecode VM, in addition you have ruby implementations based on the Java Virtual Machine (JRuby) and LLVM (Rubinius and MacRuby). These will all do just-in-time compilation and other optimizations you'd expect from a modern VM.
Default production settings in Rails is:
config.cache_classes = true
which mean that code isn't reloaded after requests, therefore it's cached in memory.
As far as MRI is concerned, experimental bytecode caching has been released with Ruby 2.3.
All you need to do to make this feature enabled is just to require 'yomikomu' rubygem and set some environmental variables introduced at here as you can find two export commands in the example above.
It may look a bit magical why VM-level bytecode cache is enabled only by requiring 'yomikomu' rubygem. Koichi described about this at his ticket.
Here is a quick benchmark result of current bytecode cache implementation. I used 'bundle version' command with benchmark-ips on Ubuntu machine. Source
The post also provides some benchmarks for this newly released functionality:
$ ruby measure.rb
Comparison:
yomikomu(fs): 5.0 i/s
yomikomanai: 3.6 i/s - 1.40x slower
Other ruby implementations might be able to take advantage of the platform native optimizations - eg. JRuby benefits from the performance benefits of JVM JIT.
精彩评论