You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Jun 3, 2023. It is now read-only.
Here's a memory allocation report taken from one of our Rails endpoints with miniprofiler in production:
Total allocated: 36329328 bytes (541884 objects)
Total retained: 4395083 bytes (18004 objects)
allocated memory by gem
-----------------------------------
14044335 transit-ruby-ff2e3acd071a
4148691 temple-0.7.6
3993243 slim-3.0.6
3794721 activesupport-5.0.7
2622300 actionview-5.0.7
1827160 front/app
1686968 other
834761 dalli-2.7.6
787136 transit-rails-6d6b533ba1df
494528 set
491982 actionpack-5.0.7
430072 activerecord-5.0.7
315464 concurrent-ruby-1.0.5
...
You see that transit-ruby is at the top, which is no surprise because the endpoint renders a heavy transit response. But if we look closely on allocations grouped by file, then there's definitely a room for improvement:
We can do nothing with cruby/json.rb, because it's the oj's part:
Here's a memory allocation report taken from one of our Rails endpoints with miniprofiler in production:
You see that transit-ruby is at the top, which is no surprise because the endpoint renders a heavy transit response. But if we look closely on allocations grouped by file, then there's definitely a room for improvement:
We can do nothing with cruby/json.rb, because it's the oj's part:
transit-ruby/lib/transit/marshaler/cruby/json.rb
Lines 31 to 39 in b4973f8
transit-ruby/lib/transit/marshaler/cruby/json.rb
Lines 59 to 65 in b4973f8
Same with the marshaler/base.rb, which produces a string interpolation each time:
transit-ruby/lib/transit/marshaler/base.rb
Lines 87 to 94 in b4973f8
But write handlers produce a large amount of static strings for nothing (lines 210, 234):
transit-ruby/lib/transit/write_handlers.rb
Lines 209 to 213 in b4973f8
transit-ruby/lib/transit/write_handlers.rb
Lines 233 to 237 in b4973f8
Line 211 also produces a large amount of strings, but it's a symbol to string conversion, which I believe is inevitable.
Another offender is RollingCache:
transit-ruby/lib/transit/rolling_cache.rb
Lines 49 to 51 in b4973f8
Solution
After adding
# frozen_string_literal: true
magic comment to the top of the mentioned files, I see the much nicer picture:that is, 20% less object allocations in total.
As for execution speed, I don't see any improvements in my local tests, but having less pressure on GC is always good.
Usually, those comments are being added throughout entire library to be Rails 3 ready.
The text was updated successfully, but these errors were encountered: