railsadminteam / rails_admin

RailsAdmin is a Rails engine that provides an easy-to-use interface for managing your data
MIT License
7.87k stars 2.25k forks source link

Optimize/simplify viable_models file path to class name logic #3589

Closed bnix closed 1 year ago

bnix commented 1 year ago

Use String#delete_prefix to reduce object allocations and simplify the existing logic that converts a file path to a class name when loading candidate models during startup. Also, move repeated path creation to respective outer loop to further cut back on string objects.

Profiler setup on a small project

MemoryProfiler.report { RailsAdmin::Config.send(:viable_models) }.pretty_print(to_file: "prof-#{Time.now.to_i}.txt")

Original code:

allocated objects by file
-----------------------------------
     11885  ruby/2.7.6/lib/ruby/2.7.0/pathname.rb
      4180  rails_admin/lib/rails_admin/config.rb

After patch:

allocated objects by file
-----------------------------------
      2141  ruby/2.7.6/lib/ruby/2.7.0/pathname.rb
      1780  rails_admin/lib/rails_admin/config.rb
coveralls commented 1 year ago

Coverage Status

Coverage: 95.886% (-0.001%) from 95.887% when pulling 81f5377772fa3e655de672bea92fd66b789e5ce0 on bnix:master into fa9a96bb12f07da349c0c2884a30a4f207991e4b on railsadminteam:master.

mshibuya commented 1 year ago

Awesome, thanks!