From: "Юрий Соколов" Date: 2013-08-26T08:10:13+04:00 Subject: [ruby-core:56805] Re: [ruby-trunk - Feature #8579] Frozen string syntax --f46d04430414d81d7804e4d1eea2 Content-Type: text/plain; charset=KOI8-R Content-Transfer-Encoding: quoted-printable 24.08.2013 23:55 =D0=CF=CC=D8=DA=CF=D7=C1=D4=C5=CC=D8 "kstephens (Kurt Step= hens)" < redmine@ruby-lang.org> =CE=C1=D0=C9=D3=C1=CC: > > > Issue #8579 has been updated by kstephens (Kurt Stephens). > > > How about something more generic? A prefix operator that memoizes and freezes any expression result in a thread-safe manner on first eval: > > %f'a frozen string' # > %f"a frozen #{interpolated} string" # > %f{a: 'frozen', :hash 'value'} # A frozen, inline memoized Hash. > %f[:a, 'frozen', :array, 'value'] # A frozen, inline, memoized Array. > %f(some(:method, 'call')) # You get the idea. Looks pretty! +1 > ---------------------------------------- > Feature #8579: Frozen string syntax > https://bugs.ruby-lang.org/issues/8579#change-41341 > > 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/ --f46d04430414d81d7804e4d1eea2 Content-Type: text/html; charset=KOI8-R Content-Transfer-Encoding: quoted-printable


24.08.2013 23:55 =D0=CF=CC=D8=DA=CF=D7=C1=D4=C5=CC=D8 "kstephens (Kurt= Stephens)" <redmine@ruby-= lang.org> =CE=C1=D0=C9=D3=C1=CC:
>
>
> Issue #8579 has been updated by kstephens (Kurt =9AStephens).
>
>
> How about something more generic? =9AA prefix operator that memoizes a= nd freezes any expression result in a thread-safe manner on first eval:
>
> =9A =9A %f'a frozen string' =9A =9A =9A =9A =9A =9A =9A =9A #<= br> > =9A =9A %f"a frozen #{interpolated} string" #
> =9A =9A %f{a: 'frozen', :hash 'value'} =9A =9A =9A# A = frozen, inline memoized Hash.
> =9A =9A %f[:a, 'frozen', :array, 'value'] =9A # A froz= en, inline, memoized Array.
> =9A =9A %f(some(:method, 'call')) =9A =9A =9A =9A =9A # You ge= t the idea.

Looks pretty! +1

> ----------------------------------------
> Feature #8579: Frozen string syntax
> https:= //bugs.ruby-lang.org/issues/8579#change-41341
>
> 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 evalu= ated a new String object must be duped.
>
> It's quite common to see code that stores a frozen String object i= nto 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 St= ring object every time. The benefit of this syntax is that it removes the n= eed to pull string literals away from where they are used.
>
> Here's an example of the proposed %f() syntax in action:
>
> =9A =9A def foo
> =9A =9A =9A ["bar".object_id, %f(bar).object_id]
> =9A =9A end
>
> =9A =9A p foo # might print "[123, 456]"
>
> =9A =9A p foo # might print "[789, 456]"
>
> These string literals could also be stored into a global refcounted ta= ble for deduplication across the entire program, futher reducing memory usa= ge.
>
> If this proposal is accepted, I can handle implementation work.
>
>
> --
> http://bugs.ruby-lang.org/<= br>

--f46d04430414d81d7804e4d1eea2--