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.
Calling
config.threadsafe!in yourconfig/environments/test.rbshould force all code to be loaded at boot time. Note that this will setcache_classestotrue, so don’t use it indevelopmentenvironment or you will lose code reloading.Another way would be to change
config.eager_load_pathsto include the directory you want to load.Resources:
config.threadsafe!in his article Removing config.threadsafe!.