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=AWL,BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,UNPARSEABLE_RELAY 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 B74941F4B4 for ; Fri, 9 Apr 2021 18:44:44 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 1C403121336; Sat, 10 Apr 2021 03:43:43 +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 23413121335 for ; Sat, 10 Apr 2021 03:43:40 +0900 (JST) Received: by filterdrecv-c5749c756-fjbdh with SMTP id filterdrecv-c5749c756-fjbdh-14-6070A097-13 2021-04-09 18:44:39.315995333 +0000 UTC m=+1302039.649293862 Received: from herokuapp.com (unknown) by geopod-ismtpd-4-1 (SG) with ESMTP id -q46M7iMTaeZFiTQDcabPg for ; Fri, 09 Apr 2021 18:44:39.248 +0000 (UTC) Date: Fri, 09 Apr 2021 18:44:39 +0000 (UTC) From: dylan.smith@shopify.com Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 79400 X-Redmine-Project: ruby-master X-Redmine-Issue-Tracker: Feature X-Redmine-Issue-Id: 17790 X-Redmine-Issue-Author: byroot X-Redmine-Sender: dylants 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?dye2b5k+s5R=2FP40aJKRU6ni9RfYjcVEz=2FzZF8dlJcyc6gZbCsRpMqEBo1rFlJ5?= =?us-ascii?Q?j8xV=2FqeMuWqAQ=2Fnx480Cmuo+6qu1uI6W2Cada4F?= =?us-ascii?Q?XTSQ=2FlxIrzHSH5ZNJXFImd6yU3X9BKbpHNwvM+v?= =?us-ascii?Q?IlRjcRWyfQfCuywsbkFPoEiiQZNQR2WsmqZO15c?= =?us-ascii?Q?+=2Fn9yV3qgx12q3KeRYTr7d2Zbd6FW2nsguOvTR+?= =?us-ascii?Q?tB7gzZQuFy902IpGM=3D?= To: ruby-core@ruby-lang.org X-Entity-ID: b/2+PoftWZ6GuOu3b0IycA== X-ML-Name: ruby-core X-Mail-Count: 103353 Subject: [ruby-core:103353] [Ruby master Feature#17790] Have a way to clear a String without resetting its capacity 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 #17790 has been updated by dylants (Dylan Thacker-Smith). If we want `clear` to shrink memory by default, a `shrink: true` keyword argument could be added so the user could override this default with `clear(shrink: false)`. This would make the change less risky, since it wouldn't change the behaviour of existing code. ---------------------------------------- Feature #17790: Have a way to clear a String without resetting its capacity https://bugs.ruby-lang.org/issues/17790#change-91444 * Author: byroot (Jean Boussier) * Status: Open * Priority: Normal ---------------------------------------- In some tight loop it can be useful to re-use a buffer string. For instance: ```ruby buffer = String.new(encoding: Encoding::BINARY, capacity: 1024) 10.times do build_next_packet(buffer) udp_socket.send(buffer) buffer.clear end ``` Currently `Array#clear` preserve the Array capacity, but `String#clear` doesn't: ```ruby >> puts ObjectSpace.dump(Array.new(20).clear) {"address":"0x7fd3260a1558", "type":"ARRAY", "class":"0x7fd3230972e0", "length":0, "memsize":200, "flags":{"wb_protected":true}} >> puts ObjectSpace.dump(String.new(encoding: Encoding::BINARY, capacity: 1024).clear) {"address":"0x7fd322a8a320", "type":"STRING", "class":"0x7fd3230b75b8", "embedded":true, "bytesize":0, "value":"", "memsize":40, "flags":{"wb_protected":true}} ``` It would be useful if `String#clear` wouldn't free allocated memory, but if it's a backward compatibility concern to change it, then maybe another method could make sense? -- https://bugs.ruby-lang.org/