ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: eregontp@gmail.com
To: ruby-core@ruby-lang.org
Subject: [ruby-core:96514] [Ruby master Feature#16150] Add a way to request a frozen string from to_s
Date: Fri, 27 Dec 2019 11:37:38 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-83446.20191227113738.8aec4e97330ee927@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-16150.20190907051037@ruby-lang.org

Issue #16150 has been updated by Eregon (Benoit Daloze).


zunda (zunda an) wrote:
> For now, this can be worked around with making sure to have the `readpartial` method return an unfrozen empty string:

Yes, that looks like a good fix to me.
I would indeed expect usages of `read` and `readpartial` to assume it to return a newly-allocated mutable String.
Using `nil#to_s` to generate an empty mutable String was arguably rather hacky.
I'd write it like `chunk || +""` or `chunk || "".dup`.

----------------------------------------
Feature #16150: Add a way to request a frozen string from to_s
https://bugs.ruby-lang.org/issues/16150#change-83446

* Author: headius (Charles Nutter)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
Much of the time when a user calls to_s, they are just looking for a simple string representation to display or to interpolate into another string. In my brief exploration, the result of to_s is rarely mutated directly.

It seems that we could save a lot of objects by providing a way to explicitly request a *frozen* string.

For purposes of discussion I will call this to_frozen_string, which is a terrible name.

This would reduce string allocations dramatically when applied to many common to_s calls:

* Symbol#to_frozen_string could always return the same cached String representation. This method is *heavily* used by almost all Ruby code that intermingles Symbols and Strings.
* nil, true, false, and any other singleton values in the system could similarly cache and return the same String object.
* The strings coming from core types could also be in the fstring cache and deduplicated as a result.
* User-provided to_s implementations could opt-in to caching and returning the same frozen String object when the author knows that the result will always be the same.

A few ideas for what to call this:

* `to_fstring` or `fstring` reflects internal the "fstring" cache but is perhaps not obvious for most users.
* `to_s(frozen: true)` is clean but there will be many cases when the kwargs hash doesn't get eliminated, making matters worse.
* `def to_s(frozen = false)` would be mostly free but may not be compatible with existing to_s params (like `Integer#to_s(radix)`

This idea was inspired by @schneems's talk at RubyConf Thailand, where he showed significant overhead in ActiveRecord from Symbol#to_s allocation.



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

      parent reply	other threads:[~2019-12-27 11:37 UTC|newest]

Thread overview: 61+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <redmine.issue-16150.20190907051037@ruby-lang.org>
2019-09-07  5:10 ` [ruby-core:94815] [Ruby master Feature#16150] Add a way to request a frozen string from to_s headius
2019-09-09 18:05   ` [ruby-core:94870] " Eric Wong
2019-09-07  5:22 ` [ruby-core:94816] " richard.schneeman+ruby-lang
2019-09-07  8:51 ` [ruby-core:94819] " headius
2019-09-07  9:29 ` [ruby-core:94820] " eregontp
2019-09-07 11:27 ` [ruby-core:94826] " shevegen
2019-09-07 12:06 ` [ruby-core:94828] " daniel
2019-09-08  2:19 ` [ruby-core:94835] " headius
2019-09-08  2:52 ` [ruby-core:94836] " daniel
2019-09-08  9:28 ` [ruby-core:94839] " eregontp
2019-09-08  9:40 ` [ruby-core:94840] " eregontp
2019-09-08  9:58 ` [ruby-core:94841] " eregontp
2019-09-08 10:10 ` [ruby-core:94842] " ashmaroli
2019-09-08 10:52 ` [ruby-core:94843] " sawadatsuyoshi
2019-09-08 20:16 ` [ruby-core:94847] " eregontp
2019-09-09  2:36 ` [ruby-core:94853] " daniel
2019-09-10  7:25 ` [ruby-core:94884] " eregontp
2019-09-10  8:12 ` [ruby-core:94885] " eregontp
2019-09-10 19:06 ` [ruby-core:94893] " jean.boussier
2019-09-19  7:47 ` [ruby-core:94975] " matz
2019-09-25 17:30 ` [ruby-core:95089] " jean.boussier
2019-09-25 20:41 ` [ruby-core:95094] " eregontp
2019-09-26  0:42 ` [ruby-core:95095] " matz
2019-09-26  8:33 ` [ruby-core:95099] " eregontp
2019-09-26 11:00 ` [ruby-core:95100] " jean.boussier
2019-09-26 11:26 ` [ruby-core:95101] " eregontp
2019-09-26 11:50 ` [ruby-core:95102] " jean.boussier
2019-09-26 13:23 ` [ruby-core:95107] " nobu
2019-09-26 13:35 ` [ruby-core:95108] " jean.boussier
2019-09-26 13:37 ` [ruby-core:95109] " jean.boussier
2019-09-26 14:32 ` [ruby-core:95110] " mame
2019-09-26 14:42 ` [ruby-core:95112] " nobu
2019-09-26 21:39 ` [ruby-core:95119] " headius
2019-09-26 23:14 ` [ruby-core:95121] " jean.boussier
2019-09-26 23:49 ` [ruby-core:95122] " headius
2019-09-26 23:53 ` [ruby-core:95123] " headius
2019-09-27  8:12 ` [ruby-core:95126] " jean.boussier
2019-09-28  4:33 ` [ruby-core:95142] " headius
2019-09-28  4:42   ` [ruby-core:95143] " Yukihiro Matsumoto
2019-09-29 12:29 ` [ruby-core:95145] " jean.boussier
2019-09-29 19:29 ` [ruby-core:95150] " jonathan
2019-09-29 22:08 ` [ruby-core:95152] " eregontp
2019-10-21 13:09 ` [ruby-core:95455] " jean.boussier
2019-11-01  0:55 ` [ruby-core:95621] " hsbt
2019-11-01  8:54 ` [ruby-core:95633] " matz
2019-11-01 17:43 ` [ruby-core:95638] " eregontp
2019-11-01 17:56 ` [ruby-core:95639] " eregontp
2019-11-01 23:16 ` [ruby-core:95646] " hsbt
2019-11-01 23:37 ` [ruby-core:95647] " rafael
2019-11-02 13:14 ` [ruby-core:95654] " eregontp
2019-11-05  8:38 ` [ruby-core:95690] " naruse
2019-11-05 11:00 ` [ruby-core:95693] " jean.boussier
2019-11-05 21:30 ` [ruby-core:95710] " eregontp
2019-11-05 22:24 ` [ruby-core:95711] " eregontp
2019-11-06  0:29 ` [ruby-core:95713] " duerst
2019-11-06  0:34 ` [ruby-core:95714] " hsbt
2019-11-06 10:27 ` [ruby-core:95719] " eregontp
2019-11-06 10:37 ` [ruby-core:95720] " eregontp
2019-12-23 15:29 ` [ruby-core:96435] " eregontp
2019-12-27  0:13 ` [ruby-core:96507] " zundan
2019-12-27 11:37 ` eregontp [this message]

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-83446.20191227113738.8aec4e97330ee927@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).