Rails是否必须延迟加载所有内容?(Does Rails have to lazy load everything? Or does it?)

编程入门 行业动态 更新时间:2024-10-26 01:30:24
Rails是否必须延迟加载所有内容?(Does Rails have to lazy load everything? Or does it?)

当我注意到以下内容:0.15s在我的规格中用于简单方法,例如:

class String def to_slug (self.dup).gsub(/["']/, '').gsub(/@/, 'at').gsub(/&/, 'and').parameterize end end

我开始问自己发生了什么,所以在没有参数化的方法之后我决定这是参数化内部的一个问题而不一定是方法本身但是,好吧,加载的方式,在我看来它似乎是懒惰的当像这样的Monkey Patches应该加载时会加载,这会导致它不应该存在的延迟IMO。 所以我的问题是,Rails是否真的懒得加载包含参数化的文件,并且有办法说服Rails急切加载String和其他stdlib的补丁。

When I notice things like: 0.15s in my specs for a simple method like:

class String def to_slug (self.dup).gsub(/["']/, '').gsub(/@/, 'at').gsub(/&/, 'and').parameterize end end

I start to ask myself what is going on, so after benching the method without parameterize I decided this was a problem inside of parameterize and not necessarily with the method itself but, well, with the way it's loaded, it seems to me like it's lazily loaded when Monkey Patches like that should be eager loaded, it is causing latency where it should not exist IMO. So my questions are, does Rails really lazily load the file that contains parameterize and is there way to convince Rails to eager load patches to String and other stdlib's.

最满意答案

调用config.threadsafe! 在config/environments/test.rb应强制在引导时加载所有代码。 请注意,这会将cache_classes设置为true ,因此请勿在development环境中使用它,否则将丢失代码重新加载。

另一种方法是更改config.eager_load_paths以包含要加载的目录。

资源:

Aaron Patterson解释了config.threadsafe! 在他的文章中删除config.threadsafe! 。 关于配置的Rails指南有很多关于每个选项的信息。

Calling config.threadsafe! in your config/environments/test.rb should force all code to be loaded at boot time. Note that this will set cache_classes to true, so don't use it in development environment or you will lose code reloading.

Another way would be to change config.eager_load_paths to include the directory you want to load.

Resources:

Aaron Patterson explains config.threadsafe! in his article Removing config.threadsafe!. The Rails guide on configuration has a lot of information on each option.

更多推荐

本文发布于:2023-04-29 07:25:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1335969.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:所有内容   加载   Rails   lazy   load

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!