Why does Rails preload all its dependencies (gems) at boot time?

When Rails launches, it preloads all its dependencies (gems), which leads to a very slow startup time. In a medium-sized project I'm working on, the Rails startup time is 10-15 seconds, depending on the machine.

Although this is not a problem in production, it is a huge pain in development. Especially when working with TDD / BDD. There are solutions to speed up tests (like spork), but they introduce their own problems.

My question is: why not require the necessary dependencies in each of the code files, and not preload everything at startup?

What are the disadvantages of the manual? Extra lines of code?

+6
source share
1 answer

Rails is not PHP. Some resources are loaded automatically, but all of them that you need are loaded at boot / initialization, because it is better to do this before requests are made so that the application is ready than lazily load them on demand, slowing down the first request. Many of these last minute methods for defining and loading classes still occur, reducing the loading time to 10-15 seconds, but if you reduce 5-10 seconds from this loading time, d are simply bound to the first request. Not good, right?

A lot of the loading time you are experiencing is in the gems / plugins / libraries that you add to your project. Many of the large sizes offer ways to download only the parts you need, and much more can use this optimization. For example, if you have a Rails project that does not require an Active Record, you can replace:

require 'rails/all' 

... from:

 require "action_controller/railtie" require "action_mailer/railtie" require "active_resource/railtie" require "rails/test_unit/railtie" 

... in your application.rb to cut down on loading (and avoid missing database errors).

+3
source

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


All Articles