Skip to content
This repository has been archived by the owner on Nov 30, 2024. It is now read-only.

Reduce memory usage by using frozen string literals #586

Merged
merged 1 commit into from
Oct 6, 2023

Conversation

technicalpickles
Copy link
Contributor

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.

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.
@technicalpickles
Copy link
Contributor Author

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:

  • 6,183,840 less bytes allocated in rspec-support (7.5% decrease)
  • 154596 less objects allocated in rspec-support (15.3% decrease)

@JonRowe JonRowe merged commit 62d7896 into rspec:main Oct 6, 2023
29 checks passed
JonRowe added a commit that referenced this pull request Oct 6, 2023
@technicalpickles technicalpickles deleted the frozen-string-literals branch October 10, 2023 15:04
JonRowe added a commit that referenced this pull request Feb 4, 2024
Reduce memory usage by using frozen string literals
JonRowe added a commit that referenced this pull request Feb 4, 2024
JonRowe added a commit that referenced this pull request Feb 4, 2024
JonRowe added a commit that referenced this pull request Feb 4, 2024
@JonRowe
Copy link
Member

JonRowe commented Feb 4, 2024

Released in 3.12.2

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants