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=-2.8 required=3.0 tests=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=no 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 E93BF1F463 for ; Tue, 31 Dec 2019 06:43:54 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id C6A541209D2; Tue, 31 Dec 2019 15:43:38 +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 5A221120998 for ; Tue, 31 Dec 2019 15:43:36 +0900 (JST) Received: by filterdrecv-p3mdw1-56c97568b5-xjbx9 with SMTP id filterdrecv-p3mdw1-56c97568b5-xjbx9-19-5E0AEE19-3F 2019-12-31 06:43:38.01551693 +0000 UTC m=+1231231.065418822 Received: from herokuapp.com (unknown [3.82.119.120]) by ismtpd0072p1iad2.sendgrid.net (SG) with ESMTP id yI5jKsNPSfuIcNpwUaRJWg for ; Tue, 31 Dec 2019 06:43:37.957 +0000 (UTC) Date: Tue, 31 Dec 2019 06:43:38 +0000 (UTC) From: sam.saffron@gmail.com Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 72269 X-Redmine-Project: ruby-master X-Redmine-Issue-Id: 16291 X-Redmine-Issue-Author: methodmissing X-Redmine-Issue-Assignee: nobu X-Redmine-Sender: sam.saffron 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?GD31AXMrLYtZC3ZmvheLkg5nAqKYtjT=2Fa5aksj98ZWPJKKkVZPlMUktqN1+659?= =?us-ascii?Q?tyPDGKN7Oq2YC8YqkRRwzdRDj=2FQN1EEImtNFNqN?= =?us-ascii?Q?z0QIKheVkmwzQtPlmTo53T=2FklFr59jWyJkODlCh?= =?us-ascii?Q?xu74n0jx+V5lHH+Gb=2F=2FIQOSSWJs6D1QxcZ3zfd=2F?= =?us-ascii?Q?xrFOV6+BI9LjJCcYPQIiqQcMRKZ70tDgMbu=2FMWi?= =?us-ascii?Q?fJhhGL3v7GEH8fPUA=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 96606 Subject: [ruby-core:96606] [Ruby master Feature#16291] Introduce support for resize in rb_ary_freeze and prefer internal use of rb_ary_freeze and rb_str_freeze for String and Array types 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="iso-8859-1" Content-Transfer-Encoding: quoted-printable Errors-To: ruby-core-bounces@ruby-lang.org Sender: "ruby-core" Issue #16291 has been updated by sam.saffron (Sam Saffron). related, see this for for the string api = https://bugs.ruby-lang.org/issues/16029#change-82018 ---------------------------------------- Feature #16291: Introduce support for resize in rb_ary_freeze and prefer in= ternal use of rb_ary_freeze and rb_str_freeze for String and Array types https://bugs.ruby-lang.org/issues/16291#change-83574 * Author: methodmissing (Lourens Naud=E9) * Status: Open * Priority: Normal * Assignee: nobu (Nobuyoshi Nakada) * Target version: 2.8 ---------------------------------------- References Github PR https://github.com/ruby/ruby/pull/2640 ### Why? While working on https://github.com/ruby/ruby/pull/2037#issuecomment-548633= 141 I also looked at the `rb_ary_freeze` helper to determine if the same op= timization of shrinking capacity can be applied there as well. Further investigation revealed that `rb_str_freeze` attempts to resize / sh= rink strings on freeze, but we currently don't do the same for arrays despi= te API for it being exposed. The gist of the change: * Let `rb_ary_freeze` also attempt to right size an array before freezing it * Replaced internal use of `rb_obj_freeze` and `OBJ_FREEZE` of callsites th= at pass a String or Array type in with `rb_ary_freeze` and `rb_str_freeze` = specifically. ### Results Saves about 3MB of Array and String specific heap footprints on a redmine p= roduction env boot on Rails 6 (custom local upgrade - it does not officiall= y support it yet): ``` Loading production environment (Rails 6.1.0.alpha) irb(main):001:0> RUBY_DESCRIPTION =3D> "ruby 2.7.0dev (2019-11-02T06:32:49Z master 772b0613c5) [x86_64-linux]" irb(main):002:0> require 'objspace' =3D> true irb(main):003:0> GC.start =3D> nil irb(main):004:0> ObjectSpace.each_object(Array).sum {|o| ObjectSpace.memsiz= e_of(o) } =3D> 4451200 irb(main):005:0> ObjectSpace.each_object(String).sum {|o| ObjectSpace.memsi= ze_of(o) } =3D> 8608472 irb(main):006:0> exit ``` ``` Loading production environment (Rails 6.1.0.alpha) irb(main):001:0> RUBY_DESCRIPTION =3D> "ruby 2.7.0dev (2019-11-02T16:52:34Z obj-freeze-specifi.. 3bc4048899) = [x86_64-linux]" irb(main):002:0> require 'objspace' =3D> true irb(main):003:0> GC.start =3D> nil irb(main):004:0> ObjectSpace.each_object(Array).sum {|o| ObjectSpace.memsiz= e_of(o) } =3D> 3233432 irb(main):005:0> ObjectSpace.each_object(String).sum {|o| ObjectSpace.memsi= ze_of(o) } =3D> 6748422 ``` irb - the best calculator ... ``` irb(main):002:0> (4451200 - 3233432) / 1024 =3D> 1189 irb(main):003:0> (8608472 - 6748422) / 1024 =3D> 1816 ``` ### Open questions * Would it make sense to let `rb_obj_freeze` switch on object type instead = and apply this optimization without "polluting" internals with custom calls= ite changes? Native extensions can then benefit from the resize feature too. * HOWEVER: Discovered in testing that `rb_str_freeze` can fail asserts in `= rb_str_tmp_frozen_release` when sprinkled too generously especially in `io.= c`. And other issues may lurk too, thus incorporating the resizing in `rb_o= bj_freeze` won't work. See https://github.com/ruby/ruby/pull/2640#issuecom= ment-549119359 I think it could also make sense to split the PR in 2: * One changeset for `rb_ary_freeze` attempting to resize Arrays on freeze, = special case internal `rb_obj_freeze` of Arrays to prefer the new API * Apply `rb_str_resize` at internal call sites where `rb_obj_freeze` is use= d with String types Thoughts? -- = https://bugs.ruby-lang.org/ Unsubscribe: