dejan/rails_panel — pull request #86 by modosc (2014-12-06 01:27:03+00:00)
Title: use a frozen copy of Rails.root in AppNotifications and LogInterceptor

i was playing with https://github.com/schneems/derailed_benchmarks while debugging a memory leak in an application and i saw that meta_request was allocating a lot of memory:

$ bundle exec rake -f perf.rake perf:mem RAILS_ENV=development
Booting: development
Running 1 times
Total allocated 155114
Total retained 1160

allocated memory by gem
-----------------------------------
meta_request-0.3.4 x 17136862
activesupport-4.1.8 x 700213
haml-4.0.6 x 346715
actionview-4.1.8 x 328915
ruby-2.1.5/lib x 327681```

the specific culprits were:

allocated memory by location
-----------------------------------
gems/meta_request-0.3.4/lib/meta_request/app_notifications.rb:11 x 13325502
gems/meta_request-0.3.4/lib/meta_request/log_interceptor.rb:39 x 3758621

both of which involve converting ```Rails.root``` from a ```Pathname``` into a ```String```.

this change adds a ```MetaRequest.rails_root``` method which caches a frozen copy of ```Rails.root.to_s``` and drops the total memory allocation of a rails boot to 40% of the previous value:

Booting: development
Running 1 times
Total allocated 67646
Total retained 1217

allocated memory by gem
-----------------------------------
meta_request/lib x 6846874
activesupport-4.1.8 x 700143
haml-4.0.6 x 346420
actionview-4.1.8 x 328915
ruby-2.1.5/lib x 327681

allocated memory by location
-----------------------------------
git/rails_panel/meta_request/lib/meta_request/log_interceptor.rb:39 x 3663501
git/rails_panel/meta_request/lib/meta_request/app_notifications.rb:11 x 3130480