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

Frozen string literals? #495

Open
ibrahima opened this issue Mar 22, 2024 · 0 comments
Open

Frozen string literals? #495

ibrahima opened this issue Mar 22, 2024 · 0 comments

Comments

@ibrahima
Copy link
Contributor

ibrahima commented Mar 22, 2024

Hi! I've been having some memory issues with an application and I started profiling it using https://github.com/MiniProfiler/rack-mini-profiler#memory-profiling. I found the source of the leaks, but I also noticed that in the allocated strings section the Scout APM gem comes up a lot (at the top of the list). It seems like we could save on a lot of memory allocations by freezing some string literals and/or creating constants for commonly used strings.


      2868  "AutoInstrument"
      1459  /rails/vendor/ruby/3.2.0/gems/scout_apm-5.3.5/lib/scout_apm/tracked_request.rb:192
      1391  /rails/vendor/ruby/3.2.0/gems/scout_apm-5.3.5/lib/scout_apm/auto_instrument/layer.rb:9
        12  /rails/vendor/ruby/3.2.0/gems/scout_apm-5.3.5/lib/scout_apm/bucket_name_splitter.rb:17
         6  /rails/vendor/ruby/3.2.0/gems/scout_apm-5.3.5/lib/scout_apm/instruments/action_controller_rails_3_rails4.rb:102

      1394  ":"
      1391  /rails/vendor/ruby/3.2.0/gems/scout_apm-5.3.5/lib/scout_apm/auto_instrument/layer.rb:6
         3  /usr/local/lib/ruby/3.2.0/net/http.rb:1986

if layer.type == 'AutoInstrument'

layer = ScoutApm::Layer.new('AutoInstrument', name)

file_name, _ = backtrace.first.split(":", 2)
also comes up a lot - I guess the file names get computed a lot and the colon is also reallocated frequently.

I'm not sure if it's enough to stick a .freeze after the literals or if you need to pull out a constant to save the memory allocations.

Maybe for a bigger application this would just be noise, but it happens that the "AutoInstrument" string is the top string allocated in the memory profiles that I'm looking at. I think overall Scout's overhead is pretty low but it would always be good to get it lower!

It seems like as of #427 the files that can't run with that pragma are already marked, so it might be possible to put the pragma on all other files. But for now I'm mainly interested in these few files that seem to be triggering a lot of allocations. (Well, I don't really know if it's enough allocations to be noticeable, but a few thousand is certainly more than 1 or 2, at least, so in principle it seems like we could be doing less work.)

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

No branches or pull requests

1 participant