scout_apm_ruby icon indicating copy to clipboard operation
scout_apm_ruby copied to clipboard

Frozen string literals?

Open ibrahima opened this issue 11 months ago • 0 comments

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

https://github.com/scoutapp/scout_apm_ruby/blob/bf9fb4b55fc2e2412a76a4d8731b55bb3b1d7e15/lib/scout_apm/tracked_request.rb#L192 https://github.com/scoutapp/scout_apm_ruby/blob/bf9fb4b55fc2e2412a76a4d8731b55bb3b1d7e15/lib/scout_apm/auto_instrument/layer.rb#L9

https://github.com/scoutapp/scout_apm_ruby/blob/bf9fb4b55fc2e2412a76a4d8731b55bb3b1d7e15/lib/scout_apm/auto_instrument/layer.rb#L6 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 https://github.com/scoutapp/scout_apm_ruby/pull/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.)

ibrahima avatar Mar 22 '24 00:03 ibrahima