From: jean.boussier@...
Date: 2019-05-08T08:28:22+00:00
Subject: [ruby-core:92595] [Ruby trunk Feature#15836] [Proposal] Make Module#name and Symbol#to_s return their internal fstrings

Issue #15836 has been updated by byroot (Jean Boussier).


> Is HashWithIndifferentAccess the main rationale behind this request?

No. It's simply the poster child of how common `Symbol#to_s` is in code bases.

I shouldn't have mentioned `HashWithIndifferentAccess` because clearly lots of people have a feud with it, and now it's totally shifting the conversation.

### What the proposal is actually about

The question here, is wether `Module#name` and `Symbol#to_s` should return a new string on every call.

My own understanding of why it's like this is because historically all strings were mutable, so the way to prevent them to be mutated was to duplicate them.

But now that frozen strings are very common in code bases, and that `fstring` are a thing, IMHO getting a new string on every call is what is surprising.




----------------------------------------
Feature #15836: [Proposal] Make Module#name and Symbol#to_s return their internal fstrings
https://bugs.ruby-lang.org/issues/15836#change-77955

* Author: byroot (Jean Boussier)
* Status: Feedback
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
# Why ?

In many codebases, especially Rails apps, these two methods are the source of quite a lot of object allocations.

`Module#name` is often accessed for various introspection features, autoloading etc.

`Symbol#to_s` is access a lot by HashWithIndifferentAccess other various APIs accepting both symbols and strings. 

Returning fstrings for both of these methods could significantly reduce allocations, as well as sligthly reduce retention as it would reduce some duplications.

Also, more and more Ruby APIs are now returning fstrings. `frozen_string_literal`AFAIK should become the default some day, string used as hash keys are now automatically interned as well.

### Backward compatibilty 

Of course this is not fully backward compatible, it's inevitable that some code in the wild is mutating the strings returned by these methods, but I do believe it's a rare occurence, and easy to fix. 

### Implementation

I implemented it here: https://github.com/ruby/ruby/pull/2175



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

Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>