Eager_load=true 的影响是什么? [英] What's the impact of eager_load=true?
问题描述
我需要知道为什么在非生产环境中 eager_load
比 false
更受欢迎?我听说过的一个论点是 eager_load
将大部分 Rails 和应用程序加载到内存中.因此,将eager_load 用于单个测试会使其运行速度变慢.然而,这引发了一些问题,例如如何在不加载 Rails 和应用程序相关代码的情况下运行测试?正在急切加载的 Rails 和应用程序相关代码是什么?config.eager_load_namespaces
提供以下类:
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:
<代码>主动支持行动派送主动模型动作视图动作控制器活动记录行动邮递员Jquery::Rails::Engine我的应用程序::应用程序
是否所有这些类及其子类都被预先加载?
Are all of these classes and their subclasses being eager loaded?
在开发或测试环境中使用 eager_load = false
有哪些明显的缺点?
What are the clear disadvantages of using eager_load = false
in development or testing environment?
推荐答案
然而,这引发了一些问题,例如如何在不加载 Rails 和应用程序相关代码的情况下运行测试?
However this raises some questions like how does a test run without loading Rails and application related code?
测试在尝试使用时按需加载必要的代码.因此,例如在某些代码行上,测试想要使用 ActiveRecord
类.当 eager_load
设置为 false
时,这个类还不是必需的,这将导致普通 ruby 程序出现异常.然而,在 Rails 项目中,测试将需要 ActiveRecord
按需使用它.所以最终单个测试运行得更快,因为只需要它需要的代码部分.
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
正在急切加载的 Rails 和应用程序相关代码是什么?
What is the Rails and application related code that is being eager loaded?
查看https://github.com/rails/rails.这是一堆东西.
是否所有这些类及其子类都被预先加载?
Are all of these classes and their subclasses being eager loaded?
是的
在开发或测试环境中使用eager_load = false 有哪些明显的缺点?
What are the clear disadvantages of using eager_load = false in development or testing environment?
在开发环境中,这是一个优势和最佳实践,因为您可以获得更快的启动时间(在使用像 spring 这样的预加载器时被忽略).与 cache_classes=false
选项一起重新加载更改可能也更容易,因为您可以重新加载更少(只是一个假设).
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).
在测试环境中,如果你想估计一些代码指标,有时你不能使用 eager_load=false
比如代码覆盖率或风格检查.例如.simple_cov
要求您在开始测试之前预先加载所有代码.
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.
并且通常情况下,某些库不能与预先加载一起使用,因为它会在加载类时进行一些初始化,该类甚至在调用其方法之前就必须已经可用.然而,这是一种罕见的情况,话虽如此,它发生在我们身上,neo4j.rb
gem
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
这篇关于Eager_load=true 的影响是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!