Project

General

Profile

Actions

Bug #13857

closed

frozen string literal: can freeze same string into two unique frozen strings

Added by chucke (Tiago Cardoso) about 7 years ago. Updated about 7 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
ruby -v:
2.3.4, 2.4.1
[ruby-core:82613]

Description

Running an interpreter with --enable-frozen-string-literal on, I get the following:

> "bang".object_id #=> 70303434940940  GOOD!
> "bang".object_id #=> 70303434940940  GOOD!
> "bang".object_id #=> 70303434940940  GOOD!
> c = "bang" 
> c.object_id #=> 70303434940940 GOOD!
> c.downcase #=> "bang"
> c.downcase.object_id #=> 70303430619560  SO SO!
> c.downcase.freeze.object_id #=> 70303430601780  BAD!

This ticket concerns the last two examples. In the case of performing an operation on the string, it makes sense to return a new string, even if the result is the same. However, I think that the last one could be done differently, in that the frozen result of the downcased value should be the original literal.

I didn't see yet how the frozen string literals are implemented, so this might be dependent on it, but I think that this misses a few optimization use cases. One notable example is keeping a headers hash from an http library. net/http keeps a version of the headers hash with the keys downcased, only to capitalize them on send. Something like this:

request["Content-Type"] = "text/html" #=> key stored in request will be "content-type"

will create more allocations than expected.


Related issues 1 (0 open1 closed)

Related to Ruby master - Feature #13725: [PATCH] Hash#[]= deduplicates string keys if (and only if) fstring existsClosedActions
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0