当我注意到以下内容: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 endI 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.更多推荐
发布评论