ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: jean.boussier@gmail.com
To: ruby-core@ruby-lang.org
Subject: [ruby-core:92592] [Ruby trunk Feature#15836] [Proposal] Make Module#name and Symbol#to_s return their internal fstrings
Date: Tue, 07 May 2019 23:05:23 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-77953.20190507230523.c034d226ec1a5c99@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-15836.20190507143338@ruby-lang.org

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


> Matz said that but it will not be for ruby 3.0 at the least.

I assumed it was due for 3.0, but good to know it isn't.

> I think other ruby users may be able to transition into frozen strings if enough time is given 

I contribute to many gems, and from what I can see `# frozen_string_literal: true` is extremely common. A good part is likely due to rubocop enforcing it, another is likely due to various article about how freezing strings made many codebases faster (sometimes oversold but that's another topic).

> with strings being frozen it appears to me as if one use case (the speed factor) is nullified

I'm not 100% sure I understood your point correctly. What I meant by hash keys being frozen is:

```
hash = {}
string = :foo.to_s # One string allocated here
hash[string] = true # A second string is allocated here because Hash apply: `-string.dup`
```

If `Symbol#to_s` was to return it's internal fstring, the above snippet would save 2 string allocations.

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

* 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/

  parent reply	other threads:[~2019-05-07 23:05 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <redmine.issue-15836.20190507143338@ruby-lang.org>
2019-05-07 14:33 ` [ruby-core:92585] [Ruby trunk Feature#15836] [Proposal] Make Module#name and Symbol#to_s return their internal fstrings jean.boussier
2019-05-07 14:42 ` [ruby-core:92586] " mame
2019-05-07 14:54 ` [ruby-core:92587] " hanmac
2019-05-07 15:36 ` [ruby-core:92588] " chris
2019-05-07 15:46 ` [ruby-core:92589] " jean.boussier
2019-05-07 16:47 ` [ruby-core:92590] " shevegen
2019-05-07 23:05 ` jean.boussier [this message]
2019-05-08  3:15 ` [ruby-core:92593] " ruby-core
2019-05-08  8:28 ` [ruby-core:92595] " jean.boussier
2019-05-08 11:30 ` [ruby-core:92597] " jean.boussier
2019-05-08 14:21 ` [ruby-core:92598] " pdahorek
2019-06-13  6:05 ` [ruby-core:93090] " duerst
2019-06-13  6:12 ` [ruby-core:93091] " matz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-77953.20190507230523.c034d226ec1a5c99@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).