Howto Refactor For Performance
HOWTO: Performance Review and optimization for your Rails app
Debride to debug memory at require time
CUT_OFF=0.1 bundle exec derailed bundle:memAnalyze output, any gems we rarely use? move them to require: false in the Gemfile and use a require statement in the method or function making use of it
You might be tempted to put the require statement at the top of the class, however, doing so will not yield any performance improvements as rails will cache class load on production and therefore call your require statement, in that case you might as well keep the require in the gemfile
Have a gem that requires an initializer? consider a singleton pattern to initialize the first time the gem is required, see image_kit_factory.rb, active_merchant_factory.rb for example
If a factory class is not ideal, you could use the config.after_initialize block in application.rb to retain speed on bootup, that’s assuming that your class is not going to be used immediately