📜 ⬆️ ⬇️

Accelerate loading rail

Despite the title, it’s about ruby ​​rather than rails. So I decided to post this translation on the Rubi blog.

The latest releases of MRI Ruby show a significant slowdown when connecting files.

For example, our average rail application when loading makes require about 2200 times - this is somewhere completely on the right side of the chart. Absolutely no good. At 1.9.2, the application starts in 20 seconds, and at 1.9.3 it is already 46. Too slow!

There are several reasons for this, but the main one is the require algorithm, which looks like this:
def require(file) $loaded.each do |x| return false if x == file end load(file) $loaded << file end 

')
All brakes come from the cycle, and the more files we connect, the more it slows down. I wrote a patch for 1.9.3, which replaces the brake cycle with something like this:
 def require(file) return false if $loaded[file] load(file) $loaded[file] = true end 

And it gives about the following result:

Much nicer!

These are graphs of synthetic test for connecting empty files, but the patch accelerates in real projects. My application now loads about 10 seconds. At 1.9.2 it was loaded 20. An empty application loads in general in 1.1 seconds, which is even faster than in 1.8.7


How to patch


It's all quite simple, it will take no more than 10 minutes, if you have installed RVM.
 #      cd /your/rails/app time script/rails runner "puts 1" #   ruby curl https://gist.github.com/raw/996418/e2b346fbadeed458506fc69ca213ad96d1d08c3e/require-performance-fix-r31758.patch > /tmp/require-performance-fix.patch rvm install ruby-head --patch /tmp/require-performance-fix.patch -n patched # ...     —    8    MBP #     cd /your/rails/app rvm use ruby-head-patched gem install bundler --no-rdoc --no-ri bundle time script/rails runner "puts 1" 


How can I help


I need to get as much attention as possible to the patch before it is included in the main trunk. It would help a lot if you:


What's next


I believe that before the inclusion of this patch in 1.9.3 is still far away, a lot of work remains to be done, but still, this is the first of many steps aimed at accelerating the launch of applications on rails. There are Bundler and RubyGems, which spend a lot of time is not clear on what - I would like to explore their insides.
I also plan to port this patch to JRuby, since there are similar problems there. In Rubinius, it seems, initially this is all right.

Source: https://habr.com/ru/post/120180/


All Articles