From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS4713 221.184.0.0/13 X-Spam-Status: No, score=-3.2 required=3.0 tests=AWL,BAYES_00, DKIM_ADSP_CUSTOM_MED,FORGED_GMAIL_RCVD,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from neon.ruby-lang.org (neon.ruby-lang.org [221.186.184.75]) by dcvr.yhbt.net (Postfix) with ESMTP id 120691F461 for ; Sun, 8 Sep 2019 09:40:48 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id A4C95120A6B; Sun, 8 Sep 2019 18:40:39 +0900 (JST) Received: from xtrwkhkc.outbound-mail.sendgrid.net (xtrwkhkc.outbound-mail.sendgrid.net [167.89.16.28]) by neon.ruby-lang.org (Postfix) with ESMTPS id 9A99C120A66 for ; Sun, 8 Sep 2019 18:40:37 +0900 (JST) Received: by filter0101p3las1.sendgrid.net with SMTP id filter0101p3las1-8309-5D74CC94-8 2019-09-08 09:40:36.224239953 +0000 UTC m=+407594.725754450 Received: from herokuapp.com (unknown [54.146.238.68]) by ismtpd0005p1iad2.sendgrid.net (SG) with ESMTP id tTx536BhQjezQMbjPVEaMg for ; Sun, 08 Sep 2019 09:40:36.075 +0000 (UTC) Date: Sun, 08 Sep 2019 09:40:36 +0000 (UTC) From: eregontp@gmail.com Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 70395 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 16150 X-Redmine-Issue-Author: headius X-Redmine-Sender: Eregon X-Mailer: Redmine X-Redmine-Host: bugs.ruby-lang.org X-Redmine-Site: Ruby Issue Tracking System X-Auto-Response-Suppress: All Auto-Submitted: auto-generated X-SG-EID: =?us-ascii?Q?KippOI8ZHtTweq7XfQzW93937kJ4QNWwSBuHnaMEcr0rChCYxAy9AuN+bhfbGo?= =?us-ascii?Q?hI1VbamSU4Yd4dNz0novT6KyQIndghyuNA22nr6?= =?us-ascii?Q?aPYSrCnsE44b5IpyaFv6lMQVTAyOxTaiLzzX=2FAS?= =?us-ascii?Q?+fzpl=2FtNCrsCnoH4D=2FwhWcUUBX7RbGPi+kurY1d?= =?us-ascii?Q?oi39cHAQv4CKbLRndI7fsVjkGc+33n3Ik9w=3D=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 94840 Subject: [ruby-core:94840] [Ruby master Feature#16150] Add a way to request a frozen string from to_s X-BeenThere: ruby-core@ruby-lang.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: Ruby developers List-Id: Ruby developers List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ruby-core-bounces@ruby-lang.org Sender: "ruby-core" Issue #16150 has been updated by Eregon (Benoit Daloze). I also tried this version which always returns the same String for a given Symbol: ```diff diff --git a/string.c b/string.c index 05ce0ed8d6..f306c905bc 100644 --- a/string.c +++ b/string.c @@ -10866,7 +10866,7 @@ sym_inspect(VALUE sym) VALUE rb_sym_to_s(VALUE sym) { - return str_new_shared(rb_cString, rb_sym2str(sym)); + return rb_sym2str(sym); } ``` And that passes both test-spec and test-all, except this test which probably needs to be adapted: ``` [5/8] Test_StringCapacity#test_capacity_shared = 0.00 s 1) Failure: Test_StringCapacity#test_capacity_shared [/home/eregon/code/ruby/test/-ext-/string/test_capacity.rb:17]: <0> expected but was <26>. ``` I'll make a PR. ---------------------------------------- Feature #16150: Add a way to request a frozen string from to_s https://bugs.ruby-lang.org/issues/16150#change-81460 * 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/