I work on some fairly large ruby on rails applications and boot up time in development always starts to become an issue, using a preloader like zeus or spring helps but reducing the first boot time is always useful. One of the big slowdowns for booting these large apps usually turns out to be initializers touching constants which then triggers an autoload. There is a popular pattern used where the class definition and configuration are split up into two, you then end up with a file in config/initializers which will run something like: MyClass.configure, which then triggers MyClass to be defined and slows down the boot process.
It would be nice instead to be able to run the configuration code when the constant gets autoloaded, something like this:
MyClass.configure do |config|
That way we still have the benefit of splitting up the class definition and the configuration, but we aren’t eagerly loading the class in the initializer, only later when it’s needed.
I’ve written a small patch which implements this behaviour: https://github.com/mariovisic/rails/commit/b5ecbf36fa087c59e716e1e85eb05a5bcb658e29
As you can see the logic is only a few lines.
Does anyone have any thoughts on if this patch would be useful to others?