Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the impact of eager_load=true?

I need to know why eager_load is preferred to be false in non-production environments? One of the arguments that I have heard of says that eager_load eager loads most of Rails and application in memory. Hence using eager_load for individual test makes it run slower. However this raises some questions like how does a test run without loading Rails and application related code? What is the Rails and application related code that is being eager loaded? config.eager_load_namespaces gives the following classes:

ActiveSupport
ActionDispatch
ActiveModel
ActionView
ActionController
ActiveRecord
ActionMailer
Jquery::Rails::Engine
MyApp::Application

Are all of these classes and their subclasses being eager loaded?

What are the clear disadvantages of using eager_load = false in development or testing environment?

like image 945
Alex Avatar asked Jan 08 '15 05:01

Alex


People also ask

What is config Eager_load?

config. eager_load when true, eager loads all registered config. eager_load_namespaces . This includes your application, engines, Rails frameworks and any other registered namespace.

How eager loading works in Rails?

Eager loading lets you preload the associated data (authors) for all the posts from the database, improves the overall performance by reducing the number of queries, and provides you with the data that you want to display in your views, but the only catch here is which one to use.


2 Answers

However this raises some questions like how does a test run without loading Rails and application related code?

The test loads the necessary code on demand as it tries to use it. So for example on some code line the test wants to use the ActiveRecord class. With eager_load set to false this class is not required yet, which will lead to an exception on a vanilla ruby program. Within a rails project however the test will require ActiveRecord on demand as it uses it. So at the end a single test runs faster, because only the code parts it needed have been required.

This technique is the opposite of eager loading and it's called autoloading

What is the Rails and application related code that is being eager loaded?

Check out https://github.com/rails/rails. It's a bunch of stuff.

Are all of these classes and their subclasses being eager loaded?

yes

What are the clear disadvantages of using eager_load = false in development or testing environment?

In development environment it's an advantage and a best practice as you get faster boot times (neglected when using a preloader like spring). Probably it's also easier for reloading changes along with the cache_classes=false option, as you have less to reload (just an assumption).

In test environment sometimes you just can't use eager_load=false if you want to estimate some code metrics like code coverage or doing style checks. E.g. simple_cov requires you to eager load all the code before starting tests.

And in general it can happen that some library can't be used with eager loading because it does some initialization on loading a class which has to be already available even before calling its methods. However this is a rare case, having said that, it happened to us with the neo4j.rb gem

like image 88
dre-hh Avatar answered Sep 22 '22 13:09

dre-hh


Eager load makes rails load all of your app on startup which increases startup time.

For example if you just want to load the rails console to check the behaviour of one model then you have to wait for all of the models, controllers etc. to load even though you only wanted to use one of them

like image 40
Frederick Cheung Avatar answered Sep 23 '22 13:09

Frederick Cheung