Project

General

Profile

Feature #8579

Frozen string syntax

Added by charliesome (Charlie Somerville) about 7 years ago. Updated almost 7 years ago.

Status:
Closed
Priority:
Normal
Target version:
[ruby-core:55699]

Description

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.


Files

FrozenStringSyntax.pdf (41 KB) FrozenStringSyntax.pdf charliesome (Charlie Somerville), 08/20/2013 08:57 AM
FrozenStringSyntax_2.pdf (33.7 KB) FrozenStringSyntax_2.pdf charliesome (Charlie Somerville), 08/20/2013 11:29 AM

Related issues

Related to Ruby master - Feature #8923: Frozen nil/true/falseClosedmatz (Yukihiro Matsumoto)09/19/2013Actions
Related to Ruby master - Feature #8906: Freeze SymbolsClosedmatz (Yukihiro Matsumoto)09/14/2013Actions
Related to Ruby master - Feature #8909: Expand "f" frozen suffix to literal arrays and hashesRejectedmatz (Yukihiro Matsumoto)09/14/2013Actions
Related to Ruby master - Feature #8992: Use String#freeze and compiler tricks to replace "str"f suffixClosedmatz (Yukihiro Matsumoto)Actions
Related to Ruby master - Feature #8976: file-scope freeze_string directiveClosedmatz (Yukihiro Matsumoto)Actions

Also available in: Atom PDF