Project

General

Profile

Actions

Feature #8579

closed

Frozen string syntax

Added by Anonymous over 10 years ago. Updated over 10 years ago.

Status:
Closed
Assignee:
-
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 Anonymous, 08/20/2013 08:57 AM
FrozenStringSyntax_2.pdf (33.7 KB) FrozenStringSyntax_2.pdf Anonymous, 08/20/2013 11:29 AM

Related issues 5 (0 open5 closed)

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
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0