Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Ruby frozen_string_literal pragma to avoid ~75MB of String allocations #445

Merged
merged 1 commit into from
Mar 12, 2024

Conversation

owst
Copy link
Contributor

@owst owst commented Jan 31, 2024

I noticed that this gem makes a large number of allocations - if we add the frozen_string_literal pragma the allocations are vastly reduced. (See this for a good explanation why)

In summary, for me using Ruby 3.2.3, before this change I see ~85MB of allocations and afterwards I see ~10MB of allocations. The following script demonstrates the improvement:

#!/usr/bin/env bash
set -euv

gem unpack ruby-mailchecker -v 6.0.1
gem install heap-profiler -v 0.7.0

cat << EOF > profile.rb
require 'heap-profiler'

HeapProfiler.report("profile_#{ARGV.first}") do
  require_relative "ruby-mailchecker-6.0.1/platform/ruby/mail_checker"

  p MailChecker::BLACKLIST.size
end
EOF

ruby profile.rb before

file=ruby-mailchecker-6.0.1/platform/ruby/mail_checker.rb

echo '# frozen_string_literal: true' | cat - "$file" > temp && mv temp "$file"

ruby profile.rb after

heap-profiler profile_before | head
heap-profiler profile_after | head

For my run just now the following was printed:

heap-profiler profile_before | head
Total allocated: 89.90 MB (225505 objects)
Total retained: 5.19 MB (56057 objects)

allocated memory by gem
-----------------------------------
  87.59 MB  other
   2.31 MB  3.2.3/lib
   8.17 kB  rubygems
  320.00 B  heap-profiler-0.7.0

heap-profiler profile_after | head
Total allocated: 11.98 MB (169331 objects)
Total retained: 5.19 MB (56057 objects)

allocated memory by gem
-----------------------------------
   9.67 MB  other
   2.31 MB  3.2.3/lib
   8.13 kB  rubygems
  320.00 B  heap-profiler-0.7.0

To avoid unnecessary String allocations.
@owst owst changed the title Add frozen_string_literal pragma to avoid ~75MB of String allocations Add Ruby frozen_string_literal pragma to avoid ~75MB of String allocations Jan 31, 2024
Copy link
Collaborator

@lyrixx lyrixx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks 👍🏼

@lyrixx lyrixx merged commit bcdeb8c into FGRibreau:master Mar 12, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants