rspec / rspec-support

Common code needed by the other RSpec gems. Not intended for direct use.
https://rspec.info
MIT License
95 stars 103 forks source link

Reduce memory usage by using frozen string literals #586

Closed technicalpickles closed 9 months ago

technicalpickles commented 9 months ago

I was running memory_profiler on a spec with a large number of examples (950), and it surfaced several places in rspec-support that aren't using frozen strings.

Originally, it was :: in lib/rspec/support/recursive_const_methods.rb that caught my attention, but I figure it can be enabled everywhere.

I did not enable it for specs, because I figure that wouldn't have as much impact since it would only affect rspec-support's specs.

I'll include some memory_profile details in the comments.

technicalpickles commented 9 months ago

Here's the relevent sections from memory_profiler for running a spec in our suite.

Before:

Total allocated: 554396103 bytes (5949151 objects)
Total retained:  5538949 bytes (28431 objects)

allocated memory by gem
-----------------------------------
  81452952  rspec-support-3.12.1

allocated objects by gem
-----------------------------------
   1004883  rspec-support-3.12.1

Allocated String Report
-----------------------------------
     89190  "::"
     45586  /Users/josh.nichols/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rspec-support-3.12.1/lib/rspec/support/recursive_const_methods.rb:67
     21802  /Users/josh.nichols/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rspec-support-3.12.1/lib/rspec/support/recursive_const_methods.rb:57
     21802  /Users/josh.nichols/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rspec-support-3.12.1/lib/rspec/support/recursive_const_methods.rb:63

     81946  ""
     43604  /Users/josh.nichols/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rspec-support-3.12.1/lib/rspec/support/recursive_const_methods.rb:72
     21802  /Users/josh.nichols/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rspec-support-3.12.1/lib/rspec/support/recursive_const_methods.rb:64

After (by running rubocop --only Style/FrozenStringLiteralComment,Layout/EmptyLineAfterMagicComment lib/ -A on the existing version, to make a more direct comparison):

Total allocated: 545831323 bytes (5769760 objects)
Total retained:  5434133 bytes (27682 objects)

allocated memory by gem
-----------------------------------
  75269112  rspec-support-3.12.1

allocated objects by gem
-----------------------------------
    850287  rspec-support-3.12.1

allocated memory by location
-----------------------------------
  26971056  /Users/josh.nichols/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rspec-support-3.12.1/lib/rspec/support/caller_filter.rb:67
  20977488  /Users/josh.nichols/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rspec-support-3.12.1/lib/rspec/support/caller_filter.rb:70
   6659520  /Users/josh.nichols/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rspec-support-3.12.1/lib/rspec/support/recursive_const_methods.rb:69
   6342400  /Users/josh.nichols/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rspec-support-3.12.1/lib/rspec/support.rb:55
   3409040  /Users/josh.nichols/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rspec-support-3.12.1/lib/rspec/support/recursive_const_methods.rb:59
   3409040  /Users/josh.nichols/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rspec-support-3.12.1/lib/rspec/support/recursive_const_methods.rb:65
   2695520  /Users/josh.nichols/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rspec-support-3.12.1/lib/rspec/support/recursive_const_methods.rb:74

Allocated String Report
-----------------------------------
(no longer in the report)

A more human readable summary/interpretation:

JonRowe commented 5 months ago

Released in 3.12.2