Issue #8579 has been updated by charliesome (Charlie Somerville).


> I believe you (charliesome) *don't* care about syntax, but you want a
> feature to introduce frozen, or same string literal. right?

Correct. I have a preference toward %f (for consistency with other string types), but I am happy as long as the same string literal feature is accepted.

> My idea is to introduce frozen suffix for (1) and once suffix for (2).

I'm ok with frozen suffix, but I'm not so sure about once suffix. I believe matz is negative towards once:

    17:10 matz: I don't recommend /re/o behavior
    17:11 matz: because it requires more complex cache mechanism

Anyway, I will revise my slide and post it here later.
----------------------------------------
Feature #8579: Frozen string syntax
https://bugs.ruby-lang.org/issues/8579#change-41290

Author: charliesome (Charlie Somerville)
Status: Open
Priority: Normal
Assignee: ko1 (Koichi Sasada)
Category: syntax
Target version: current: 2.1.0


I'd like to propose a new type of string literal - %f().

Because Ruby strings are mutable, every time a string literal is evaluated a new String object must be duped.

It's quite common to see code that stores a frozen String object into a constant which is then reused for performance reasons. Example: https://github.com/rack/rack/blob/master/lib/rack/methodoverride.rb

A new %f() string literal would instead evaluate to the same frozen String object every time. The benefit of this syntax is that it removes the need to pull string literals away from where they are used.

Here's an example of the proposed %f() syntax in action:

    def foo
      ["bar".object_id, %f(bar).object_id]
    end

    p foo # might print "[123, 456]"

    p foo # might print "[789, 456]"

These string literals could also be stored into a global refcounted table for deduplication across the entire program, futher reducing memory usage.

If this proposal is accepted, I can handle implementation work.


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