DEV Community

Stan Bright for SaaSHub

Posted on

How to speed up your Ruby/Rails app by ~5% with zero efforts

This is going to be a quick one. Yet I think it might be helpful to almost everyone using ruby. A few days ago, I learned about a gem by Shopify - symbol-fstring. Thank you @strzibnyj.

It does something very simple - very well: Access symbols internal strings without duplicating them. Why is that important?

In Ruby many APIs tend to accept symbols, but regularly convert them to string internally. The typical example is ActiveSupport::HashWithIndifferentAccess, but there are plenty more.

The problem with this is that Symbol#to_s creates a new string every time it is invoked, and since it often happens in hotspots, it causes a lot of work for the garbage collector, and cause many identical strings to be kept in memory.

To activate the gem in an app, all you need is adding the gem to your Gemfile and requiring the "patch".

e.g. gem 'symbol-fstring', require: 'fstring/all'

That's all. SaaSHub has been running with it for 3 days now, and there haven't been any unexpected issues. Based on my rough benchmarks, a typical page like "basecamp alternatives" responds about 5% faster and allocates about 5% fewer objects.

I know, 5% isn't that much, yet it's "FREE". Moreover, this gem is maintained by a billion-dollar company, Shopify, popular with its high-quality dev standards. So, I guess it is both safe and worth it giving it a go.

Oldest comments (0)