Feedback on magic comment ‘immutable: string’

Charles Nutter, the mastermind behind JRuby, had some feedback on the magic ‘immutable: string’ comment we proposed in Ruby issue 9278,  (BTW our proposal is directly built on his Ruby 2.1.0 contribution, Ruby issue 9042.)

I figured it was worth copying the response up here where the formatting is more rich.

From Charles:

A magic comment should not completely change the semantics of a literal type. Encoding magic comments do not suffer from the same issue since they only change how the bytes of the strings are interpreted by the encoding subsystem…they do not change semantics.

I view this `immutable: string` comment as less intrusive than the encoding magic comment. The magic encoding was typically required in order get code to compile at all in 1.9. In this case the magic `immutable: string` comment is just a tip to Ruby saying “Feel free to optimize string literals here”. The comment is completely optional, as is the optimization. But it’s worth it because the optimization has a big speed payoff. 1.6X in this benchmark. I think most projects will run at least 1.1X faster.

A magic comment is far removed from the actual literal strings, meaning that every developer that enters a file will have to keep in mind whether the strings have been forced to be immutable before doing any work with literal strings.

I expect that the `immutable: string` magic comment would typically be dropped at the top of all files in a project. (As many did with the encoding comment when porting to 1.9.) Any project that has automated test coverage is going to want to put the comment throughout as a project policy, because of the speed payoff.  [BTW to make it easy to add this magic comment, we’ve cloned the magic_encoding gem into a magic_immutable gem here.]

Although this eliminates or reduces calls to .freeze, it causes the opposite effect to get a mutable string in the same file…specifically, you have to call .dup.

Yes, exactly! This magic comment eliminates a giant/infeasible/ugly change (`.freeze` after every string literal) and replaces it with a tiny/feasible one. That is certainly its goal.

Consider Rails. I just did a quick/approximate regex search to count string literals in Rails 3.2.12 and found on the order of 50,000 of string literals. How many of those do you think are mutated? I’m going to guess no more than 10 places across all of Rails. (I’m hoping to get some actual stats using a hack of this branch, but for a quick estimate I audited about 150 cases that mutate a string and didn’t find a single case where the receiver was a string literal.)

So if we add the magic comment in Rails, we’ll need to add `.dup` (or change to String.new) in up to 10 places instead of adding `.freeze` after 50,000 string literals!

I think it would be better to consider adding the String#f method proposed during the rework of .freeze optimizations. Adding a .f to a literal string is not very much to ask, and in your particular script, it would actually add *fewer* characters to the code than the magic comment.

I’m with Paul Graham, who argues that expressiveness is counted in elements (~tokens), not bytes. So `.f` is no more expressive than `.freeze`. Whether `.f` or `.freeze`, it’s still at least 1 element to be added after *every* string literal. One of Ruby’s main differentiators is its beauty and English-like readability. That’s certainly what drew me to Ruby. No one who feels that way is going to put `.freeze` or `.f` after every string literal.

In summary, there will certainly be projects that don’t bother with the `immutable: string` magic comment (for example, those that lack automated test coverage, or don’t run on MRI). But many projects will use it because they will get a big, immediate performance payoff in Ruby 2.1. We’re already looking forward to it!

Leave a Reply

Your email address will not be published. Required fields are marked *


two × 9 =