在Rails 3应用程序中使用require_dependency有什么性能影响?

时间:2023-01-17 16:54:53

I feel like I understand the difference between require and require_dependency (from How are require, require_dependency and constants reloading related in Rails?).

我觉得我理解require和require_dependency之间的区别(来自Rails中的How are require,require_dependency和constants reloading?)。

However, I'm wondering what should happen if I use some of the various methods out there (see http://hemju.com/2010/09/22/rails-3-quicktip-autoload-lib-directory-including-all-subdirectories/ and Best way to load module/class from lib folder in Rails 3?) to get all files loading so we:

但是,我想知道如果我使用其中的一些方法会发生什么(请参阅http://hemju.com/2010/09/22/rails-3-quicktip-autoload-lib-directory-including-all -subdirectories /和从Rails 3中的lib文件夹加载模块/类的最佳方法?)来加载所有文件,所以我们:

  1. don't need to use require_dependency all over the place in the application and
  2. 不需要在应用程序中的所有位置使用require_dependency

  3. don't have to restart development servers when files in the lib directory change.
  4. 当lib目录中的文件发生更改时,不必重新启动开发服务器。

It seems like development performance would be slightly impacted, which is not that big of a deal to me. How would performance be impacted in a production environment? Do all of the files generally get loaded only once if you are in production anyway? Is there a better way that I'm not seeing?

看起来开发性能会受到轻微影响,这对我来说并不是什么大不了的事。如何在生产环境中影响性能?如果您还在生产中,所有文件通常只加载一次吗?有没有更好的方式,我没有看到?

If you could include some resources where I could read more about this, they would be greatly appreciated. Some blog posts said that this behavior changed recently with Rails 3 for autoreloading lib/* files and that it was contentious, but I didn't see any links to these discussions. It would be helpful for considering the pros/cons. Thanks!

如果您可以包含一些我可以阅读更多相关信息的资源,我们将不胜感激。一些博客文章说,最近这种行为改变了Rails 3自动加载lib / *文件,这是有争议的,但我没有看到任何链接到这些讨论。考虑利弊是有帮助的。谢谢!

1 个解决方案

#1


4  

The code reloader is disabled by default in production. So if you are calling require_dependency at the top of a file it is going to be executed only once.

生产中默认禁用代码重新加载器。因此,如果您在文件顶部调用require_dependency,它将只执行一次。

The Rails 3 change you mentioned is really small. You can usually call Foo and it will be loaded from app/models/foo.rb automatically. Before it could also be loaded from lib/foo.rb. (These directories app/models and lib are called autoload paths.) Rails team decided to remove lib from autoload paths in the 3rd version. You can still put it back. But it is encouraged to leave in lib less frequenttly-changed and project-specific files. If you have something that does not belong to any of the default app subdirectories like app/models or app/controllers you don't have to put it in lib. You can add your own subdirectory. I have app/presenters, for example. There is a discussion on the old issue tracker if you want more info on that.

你提到的Rails 3变化真的很小。您通常可以调用Foo,它将自动从app / models / foo.rb加载。之前它也可以从lib / foo.rb加载。 (这些目录app / models和lib称为自动加载路径。)Rails团队决定从第3版中的自动加载路径中删除lib。你仍然可以把它还掉。但是鼓励在lib中留下频繁更改和项目特定的文件。如果您的某些内容不属于任何默认应用程序子目录(如app / models或app / controllers),则不必将其放在lib中。您可以添加自己的子目录。例如,我有app /演示者。如果您想了解更多信息,请讨论旧问题跟踪器。

#1


4  

The code reloader is disabled by default in production. So if you are calling require_dependency at the top of a file it is going to be executed only once.

生产中默认禁用代码重新加载器。因此,如果您在文件顶部调用require_dependency,它将只执行一次。

The Rails 3 change you mentioned is really small. You can usually call Foo and it will be loaded from app/models/foo.rb automatically. Before it could also be loaded from lib/foo.rb. (These directories app/models and lib are called autoload paths.) Rails team decided to remove lib from autoload paths in the 3rd version. You can still put it back. But it is encouraged to leave in lib less frequenttly-changed and project-specific files. If you have something that does not belong to any of the default app subdirectories like app/models or app/controllers you don't have to put it in lib. You can add your own subdirectory. I have app/presenters, for example. There is a discussion on the old issue tracker if you want more info on that.

你提到的Rails 3变化真的很小。您通常可以调用Foo,它将自动从app / models / foo.rb加载。之前它也可以从lib / foo.rb加载。 (这些目录app / models和lib称为自动加载路径。)Rails团队决定从第3版中的自动加载路径中删除lib。你仍然可以把它还掉。但是鼓励在lib中留下频繁更改和项目特定的文件。如果您的某些内容不属于任何默认应用程序子目录(如app / models或app / controllers),则不必将其放在lib中。您可以添加自己的子目录。例如,我有app /演示者。如果您想了解更多信息,请讨论旧问题跟踪器。