# Freezolite: the magic gem for keeping Ruby literals safely frozen

> Introducing the freezolite gem, which helps enable frozen string literals by default in Ruby projects on a per-folder basis.

- Date: 2023-11-14T00:00:00.000Z
- Authors: Vladimir Dementyev, Travis Turner
- Categories: Performance, DX
- URL: https://evilmartians.com/chronicles/freezolite-the-magic-gem-for-keeping-ruby-literals-safely-frozen

---

Once upon a time, a question was raised in the `#backend` channel of the Evil Martians Slack. “Is there a way to magically set `frozen_string_literal` to `true` for an entire project to silence that annoying comment in every Ruby file? It's seriously frustrating, and RuboCop keeps bugging me as I'm always forgetting it somewhere!“ Sounds familiar? Read on and learn about the [Freezolite][] gem, a concoction crafted up to solve this problem by Evil ~Wizards~ Martians.

In Ruby, everything is mutable by default, including strings. This model fits Ruby's dynamic nature well but may have noticeable downsides at scale, primarily performance-wise. Why? Mutability means we cannot safely reuse the same object and have to opt into copying it over to avoid race conditions; that's what Ruby does with _literal strings_.

A literal string is defined in the source code, i.e., created statically, not dynamically. Let's consider an example:

```ruby
class Cat
  def say = "meow"
end

Cat.new.say #=> "meow"
```

In the snippet above, `"meow"` is a literal string. Even though it's the same for all the `Cat` objects we create, every time we invoke the `#say` method, **a new String object** is created (or _allocated_); the same holds true when calling `#say` multiple times for the same _cat_. Here is a quick snippet that proves the statement:

```ruby
class Cat
  def say = "meow"
end

cat = Cat.new

require "objspace"

was_alloc = ObjectSpace.count_objects[:T_STRING]
100.times { cat.say }
new_alloc = ObjectSpace.count_objects[:T_STRING]

p "Allocated strings: #{new_alloc - was_alloc}"
```

We run this code via the `ruby` command (not IRB or another console as it would add additional allocations). You should see something like `Allocated strings: 128` (we'll leave it up to the reader to figure out where the additional 28 allocations came from 😁). Changing the method source code to return `"meow".freeze` would result in the same number, minus 100 (i.e., 28 for me).

Now, imagine you've generate thousands of _cat meows_ in a unit of work in your project (e.g. when processing a web request). That might lead to massive stress on Ruby's garbage collector and, as a result, to noticeable performance degradation. This may sound like a made-up problem, but believe it or not, I've seen many Ruby on Rails applications that do not treat strings carefully in their presentation layer (serializers, presenters, decorators, etc.) and making response times slower for no reason.

Here are some examples from the open-source world demonstrating the positive effect of switching to frozen strings literals: [Rails](https://github.com/rails/rails/pull/20946) (this one even predates the `frozen_string_literal` pragma), [axlsx](https://github.com/randym/axlsx/pull/633) and [webmock](https://github.com/bblimke/webmock/pull/1033).

The positive effect of frozen strings caught the Ruby Core team’s attention [a while ago](https://bugs.ruby-lang.org/issues/8976). As a result, a new magic comment (or _pragma_) was introduced in Ruby 2.3 to freeze string literals on a file basis automatically—the infamous `# frozen_string_literal: true` comment you might have seen pretty much everywhere these days. So, instead of adding `.freeze` manually to all of the strings in a file, you just add a single line at the very beginning of it:

```ruby
# frozen_string_literal: true

class Cat
  def say = "meow"
end

Cat.new.say.frozen? #=> true
```

This magic comment has made it possible to introduce this new behavior without breaking the existing code. There was a plan to make literals immutable (or _frozen_) by default in Ruby 3.0, but... [it didn't work out](https://bugs.ruby-lang.org/issues/11473#note-53). Nonetheless, we still have to deal with this _"magic"_. So, let's see our options.

A new initiative on making literals frozen by default has started recently: [bugs.ruby-lang.org/issues/20205](https://bugs.ruby-lang.org/issues/20205).

## Living with magic... comments

Today, Ruby developers have the following alternatives when it comes to frozen strings.

The first solution is to just not care about them—no magic comments, no `#freeze`—just good old Ruby code. Yeah, you lose the potential performance benefits, but that may not bother you at all. And that's fine; I don't insist on optimization just for the sake of optimization.

And from there, a theoretical option exists to enable frozen strings globally via the Ruby command line option: `--enable=frozen-string-literal`. I've never seen it used in the wild, though. Why? Because you may still have code depending on mutable string literals, and that code would break if you make them immutable. Lucky you if you can catch this problem before hitting the production environment.

Try to run your project's tests with this option enabled as an experiment. The result for one of my pet Rails projects is below:

```sh
$ RUBYOPT="--enable=frozen-string-literal" bin/rails test

...

/ruby/3.2.0/gems/websocket-driver-0.7.5/lib/websocket/driver/hybi.rb:221:in `send_frame':
  can't modify frozen String: "C2" (FrozenError)
```

Finally, the most popular approach for large projects these days is relying on [RuboCop][] and its `Style/FrozenStringLiteralComment` rule that enforces adding the magic comment to every Ruby file. You either get used to starting every file with the `# frozen_string_literal: true` spell or following the linter instructions.

*Read also Evil Martians' article*: https://evilmartians.com/chronicles/rubocoping-with-legacy-bring-your-ruby-code-up-to-standard

Although the RuboCop approach is robust and straightforward, it's still a bit annoying. Why on Mars should I keep this tiny low-level detail in my mind? Why is there no way to tell Ruby, "Please, make all the strings in my **project** immutable by default"?

That's how this story started, with a simple question. And now I'm ready to reveal the answer.

## Freeze my project's literals, please

After the question was raised, we (the Evil Martians backend team) met to brainstorm possible solutions. Below, I present edited logs from this meeting to demonstrate how we reached the final result.

We started by grepping the Ruby codebase in search of `frozen_string_literal` mentions. The goal was to understand how the magic comment works—if you want to hack something, you must first master it. Here is what we found:

- [Here](https://github.com/ruby/ruby/blob/364a6d56d776270da09604816d623047c66c5e32/compile.c#L9838) the compiler checks if the `frozen_string_literal` option has been set for the instruction sequence (`ISEQ_COMPILE_DATA(iseq)->option->frozen_string_literal`). Hmm, compile options? Sounds interesting; let's learn more about them...
- [The list of available compile options](https://github.com/ruby/ruby/blob/364a6d56d776270da09604816d623047c66c5e32/iseq.c#L702) and their [default values](https://github.com/ruby/ruby/blob/364a6d56d776270da09604816d623047c66c5e32/vm_opts.h#L13) give us a hint: there is something called `VM::CompileOption`: "You **can change these options at runtime** by VM::CompileOption". Awesome! That's exactly what we want, to change the `frozen_string_literal` option at runtime!
- After digging, we found the API: `RubyVM::InstructionSequence.compile_option = {frozen_string_literal: true}`.

Another compile option that is worth exploring is the `tailcall_optimization`. Check out [this talk](https://www.youtube.com/watch?v=6Tblgvvit4E) if you want to learn more.

Let's see how changing compile options at runtime works. Assuming we have a `cat.rb` file with the Cat class defined above, try to run the following code in the IRB console:

```sh
$ irb

irb> load "./cat.rb"
irb> Cat.new.say.sub!("ow", "-wow")
me-wow

irb> RubyVM::InstructionSequence.compile_option = {frozen_string_literal: true}
irb> load "./cat.rb"
irb> Cat.new.say.sub!("ow", "-wow")

can't modify frozen String

irb> RubyVM::InstructionSequence.compile_option = {frozen_string_literal: false}
irb> load "./cat.rb"
irb> Cat.new.say.sub!("me", "w")
wow
```

Nice! We've just proven that enabling string immutability at runtime is possible. All that's left is to inject this logic when loading Ruby files so we can automatically set the `frozen_string_literal` option to true for the project's files.

How can one hijack the process of loading source code in Ruby? One option is to monkey-patch `Kernel.require` (and its siblings, such as `Kernel.load`). That may sound scary, but many projects do that, such as [Zeitwerk][] and [Ruby Next][], to name a couple.

*Read also Evil Martians' article*: https://evilmartians.com/chronicles/ruby-next-make-all-rubies-quack-alike

Luckily, in MRI, we have a better option: the `RubyVM::InstructionSequence.load_iseq` callback. It's an official mechanism for intervening with the default loading workflow. You only need to define this method (it's undefined by default), and Ruby will call it whenever a source file is loaded. So, the very first working prototype we had looked like this:

```ruby
# patch.rb
class RubyVM::InstructionSequence
  def self.load_iseq(path)
    # Simple file filtering logic just for the test sake
    return nil unless path.match?(%r{cat.rb$})

    frozen_string_literal = RubyVM::InstructionSequence.compile_option[:frozen_string_literal]
    RubyVM::InstructionSequence.compile_option = {frozen_string_literal: true}
    RubyVM::InstructionSequence.compile_file(path)
  ensure
    RubyVM::InstructionSequence.compile_option = {frozen_string_literal:}
  end
end
```

We set the `frozen_string_literal` option to true for every file path we're interested in and restored it to the previous value after that. You can see it in action by running the following command:

```sh
$ ruby -r ./patch.rb -r ./cat.rb -e "puts Cat.new.say.frozen?"

true
```

And that might be all—just a few lines of code to eliminate tons of `# frozen_string_literal: true` comments in your codebase. Unfortunately, things turned out a bit more complicated, and a comprehensive solution deserved to be promoted to a gem.

## Introducing Freezolite

First, relying on the `.load_iseq` hook has a limitation in that it can only be defined once. But what if you have two libraries trying to hijack the loading process? That's what would likely happen in your Rails application due to the presence of the [Bootsnap][] gem.

Bootsnap defines its own `.load_iseq` method to perform compiled instruction sequence caching. So, we must preserve its functionality, and thus, we have to rewrite our patch a bit:

```ruby
module FrozenInstructionSequence
  def load_iseq(path)
    frozen_string_literal = RubyVM::InstructionSequence.compile_option[:frozen_string_literal]

    if path.match?(%r{name.rb$})
      RubyVM::InstructionSequence.compile_option = {frozen_string_literal: true}

      defined?(super) ? super : RubyVM::InstructionSequence.compile_file(path)
    else
      defined?(super) ? super : nil
    end
  ensure
    RubyVM::InstructionSequence.compile_option = {frozen_string_literal:}
  end
end

RubyVM::InstructionSequence.singleton_class.prepend(FrozenInstructionSequence)
```

The essential bits of the snippet above are `defined?(super)` and `Module#prepend`. This way, we make our patch aware of other potential patches. Moreover, Bootsnap is smart enough to take compile options into account when [calculating cache keys](https://github.com/Shopify/bootsnap/blob/cd15951d1ea979e70a05c1a1f29140beb5dc6914/ext/bootsnap/bootsnap.c#L299). Thus, there is no need to invalidate cache manually when introducing our patch. Awesome!

After adding some high-level interface, we packed this solution into a gem: [Freezolite]. You can add it to your project and configure automatic string literal freezing with just a single line of code in your application bootstrap file. For example, in a Rails application, that would be the `config/application.rb` file:

```ruby
# config/application.rb

#...

Bundler.require(*Rails.groups)

require "freezolite/auto"

# ...
```

That's it! No more `# frozen_string_literal: true` and RuboCop failures.

## Bonus: require-hooks for more experiments

Although Freezolite only targets CRuby, I've found that having a generic interface to interact with Ruby code loading might be useful. We already had this functionality implemented in [Ruby Next][], so we've been just waiting for another project to have similar problems so we could extract it. And the day has come—let me introduce [require-hooks][].

*Meet Evil Martians at event*: https://evilmartians.com/events/ruby-next-get-future-ruby-changes-now-with-vladimir-dementyev

Require Hooks is a library that provides a common interface for injecting custom logic into source file loading in all major Rubies: Cruby, JRuby, TruffleRuby. Depending on the platform, it picks the best suitable strategy: installing the `InstructionSequence.load_iseq` callback, patching `Kernel.require`, or even patching Bootsnap (because in some cases, we only need to operate on new, non-cached code). The library passes all `Kernel.require` tests from the [ruby/spec][], so it's safe to use.

The final version of Freezolite using Require Hooks became very lightweight:

```ruby
module Freezolite
  class << self
    def setup(patterns:, exclude_patterns: nil)
      require "require-hooks/setup"

      ::RequireHooks.around_load(patterns: patterns, exclude_patterns: exclude_patterns) do |path, &block|
        was_frozen_string_literal = ::RubyVM::InstructionSequence.compile_option[:frozen_string_literal]
        ::RubyVM::InstructionSequence.compile_option = {frozen_string_literal: true}
        block.call
      ensure
        ::RubyVM::InstructionSequence.compile_option = {frozen_string_literal: was_frozen_string_literal}
      end
    end
  end
end
```

We use the `.around_load` hook here, which wraps the code loading process. Another example of using it is implementing custom syntax error handling:

```ruby
RequireHooks.around_load do |path, &block|
  block.call
rescue SyntaxError => e
  raise "Oops, your Ruby is not Ruby: #{e.message}"
end
```

Compare this to the [monkey-patches in the syntax_suggest gem](https://github.com/ruby/syntax_suggest/blob/5051fdeb6a37e28720b1ef777b716416531c5227/lib/syntax_suggest/core_ext.rb#L75-L114) doing pretty much the same thing. Can you tell the difference?

There are also `.source_transform` and `.hijack_load` to perform source-to-source transformations or take full control over the loading process, respectively.

Maybe this is the tool you've been looking for to bring your weird Ruby ideas to life!

[Freezolite]: https://github.com/ruby-next/freezolite
[require-hooks]: https://github.com/ruby-next/require-hooks
[RuboCop]: https://github.com/rubocop/rubocop
[Zeitwerk]: https://github.com/fxn/zeitwerk
[Ruby Next]: https://evilmartians.com/opensource/ruby-next
[Bootsnap]: https://github.com/Shopify/bootsnap
[ruby/spec]: https://github.com/ruby/spec

---

At Evil Martians, we transform growth-stage startups into unicorns, build developer tools, and create open source products. If you’re ready to engage warp drive, [give us a shout](https://evilmartians.com/contact-us)!

---

This article was written by [Evil Martians](https://evilmartians.com), a product development consultancy trusted by teams from early-stage startups to public companies. If you're working through a hard problem, [we can help](https://evilmartians.com/contact-us)
