Issue #9278 has been updated by colindkelley (Colin Kelley).


Thanks for the quick reply, Charles. Here are my thoughts:

> * 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 the benchmark above. 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 the comment, we've cloned the magic_encoding gem into a magic_immutable gem here:  https://github.com/Invoca/magic_immutable_string]

> * 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.

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 tokens, not bytes. So `.f` is no more expressive than `.freeze`. Whether `.f` or `.freeze`, it's still 2 tokens 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). 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!
----------------------------------------
Feature #9278: Magic comment "immutable: string" makes "literal".freeze the default for that file
https://bugs.ruby-lang.org/issues/9278#change-43954

Author: colindkelley (Colin Kelley)
Status: Open
Priority: Normal
Assignee: 
Category: core
Target version: 2.1.0


Building on https://bugs.ruby-lang.org/issues/9042, this pull request adds the magic comment # -*- immutable: string -*- that implies .freeze on every string literal in the file. To get a mutable string in a file that starts with the magic comment, use String.new or ''.dup.

Here is a corresponding github pull request:

    https://github.com/ruby/ruby/pull/487

For more details, background, and rationale, please see this blog post:

    http://development.invoca.com/magic-comment-immutable-string-makes-ruby-2-1s-literal-freeze-optimization-the-default/


-- 
http://bugs.ruby-lang.org/