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.9 required=3.0 tests=BAYES_00, 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 5DF9B1F454 for ; Fri, 8 Nov 2019 22:31:17 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 070BE120B21; Sat, 9 Nov 2019 07:31:07 +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 D2BE5120AE6 for ; Sat, 9 Nov 2019 07:31:03 +0900 (JST) Received: by filter0172p3mdw1.sendgrid.net with SMTP id filter0172p3mdw1-17544-5DC5ECAA-75 2019-11-08 22:31:06.976721548 +0000 UTC m=+91371.544911535 Received: from herokuapp.com (unknown [18.209.44.199]) by ismtpd0043p1iad2.sendgrid.net (SG) with ESMTP id XWfelNW4TdyNyoU5pl_k-A for ; Fri, 08 Nov 2019 22:31:07.069 +0000 (UTC) Date: Fri, 08 Nov 2019 22:31:07 +0000 (UTC) From: lourens@bearmetal.eu Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 71390 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 16291 X-Redmine-Issue-Author: methodmissing X-Redmine-Sender: methodmissing 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?Fr=2F4MkNsnJ5O85XS4movydqTd1a9M98=2FgJAFvUl026OR+WTt3th79WpHtSyHP=2F?= =?us-ascii?Q?6noeIS3LiG=2F5EP7RkMxroxVfiLK1bwczmAl=2F1ae?= =?us-ascii?Q?MT9=2FpkCKIvcceyLBMNyoXEGGdmk+0Lq9WgVpFeg?= =?us-ascii?Q?4MwqNzuXLrKCUzlWoftYEMJ7ehUeALK90LBg9il?= =?us-ascii?Q?xK1D40Tp3CobdpoU9yW4=2FKmsQAYAUmIAAmA=3D=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 95765 Subject: [ruby-core:95765] [Ruby master Misc#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 methodmissing (Lourens Naud=E9). Results of `Array#frozen` -> `rb_ary_freeze` with shrink support as per sug= gestion from Shyouhei: ``` lourens@CarbonX1:~/src/redmine$ irb irb(main):001:0> RUBY_DESCRIPTION =3D> "ruby 2.7.0dev (2019-11-05T10:33:43Z obj-freeze-specifi.. 485e4f3a84) = [x86_64-linux]" irb(main):002:0> require 'objspace' =3D> true irb(main):003:0> a =3D (1..5).to_a irb(main):004:0> ObjectSpace.memsize_of(a) =3D> 200 irb(main):005:0> a.freeze =3D> [1, 2, 3, 4, 5] irb(main):006:0> ObjectSpace.memsize_of(a) =3D> 80 ``` Master: ``` lourens@CarbonX1:~/src/redmine$ irb irb(main):001:0> RUBY_DESCRIPTION =3D> "ruby 2.7.0dev (2019-11-03T14:20:01Z master 5a7487bdcd) [x86_64-linux]" irb(main):002:0> require 'objspace' =3D> true irb(main):003:0> a =3D (1..5).to_a irb(main):004:0> ObjectSpace.memsize_of(a) =3D> 200 irb(main):005:0> a.freeze =3D> [1, 2, 3, 4, 5] irb(main):006:0> ObjectSpace.memsize_of(a) =3D> 200 ``` ---------------------------------------- Misc #16291: Introduce support for resize in rb_ary_freeze and prefer inter= nal use of rb_ary_freeze and rb_str_freeze for String and Array types https://bugs.ruby-lang.org/issues/16291#change-82581 * Author: methodmissing (Lourens Naud=E9) * Status: Open * Priority: Normal * Assignee: = ---------------------------------------- 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: