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.6 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,UNPARSEABLE_RELAY 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 64CF41F5AE for ; Wed, 21 Apr 2021 23:27:58 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id AA01A120DE1; Thu, 22 Apr 2021 08:26:53 +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 3673D120BAC for ; Thu, 22 Apr 2021 08:26:52 +0900 (JST) Received: by filterdrecv-6b8b466b66-9k46q with SMTP id filterdrecv-6b8b466b66-9k46q-15-6080B4F7-4A 2021-04-21 23:27:51.964969838 +0000 UTC m=+529485.909383604 Received: from herokuapp.com (unknown) by ismtpd0170p1iad2.sendgrid.net (SG) with ESMTP id yQdZ51AFRHm6S2jq5Q5TSw for ; Wed, 21 Apr 2021 23:27:51.854 +0000 (UTC) Date: Wed, 21 Apr 2021 23:27:52 +0000 (UTC) From: dsisnero@gmail.com Message-ID: References: Mime-Version: 1.0 X-Redmine-Project: ruby-master X-Redmine-Issue-Tracker: Feature X-Redmine-Issue-Id: 17790 X-Redmine-Issue-Author: byroot X-Redmine-Sender: dsisnero 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-Redmine-MailingListIntegration-Message-Ids: 79595 X-SG-EID: =?us-ascii?Q?wr1eytF=2FotmoiJuqdpesvU8qnTP42WwXoRgJjSF4fNHRIzjfBNjHBHp2nrOZxW?= =?us-ascii?Q?KHp=2FrG32vXY2=2FkuTAF6hYpTl0qN0wsn5vXXYl+A?= =?us-ascii?Q?ywPgadL4dNiPnZ6X0hQq2170TVz7BKn+HCio3qt?= =?us-ascii?Q?R2NoRgb=2FavZDMnnpLiqjWLvz57F10riBMaDjPBE?= =?us-ascii?Q?RZp4QUttu0vyq0jo26eoL48Wb83mCNv9x1w=3D=3D?= To: ruby-core@ruby-lang.org X-Entity-ID: b/2+PoftWZ6GuOu3b0IycA== X-ML-Name: ruby-core X-Mail-Count: 103546 Subject: [ruby-core:103546] [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 dsisnero (Dominic Sisneros). That was what I was hoping the addition of memoryview would help with but the only way to interact with the memoryview in ruby is with Fiddle If we had a ByteArray class that implemented memoryview buffer = ByteArray.new('this is a string'.bytes) mv = Fiddle::MemoryView.new(buffer) mv.byte_size # 16 first8 = mv[0:8] # once Fiddle::MemoryView allows you to slice socket.write(first8) # once socket.write allows you to write memoryview objects without changing into string. What memoryview is supposed to do is allow the reading and writing with zero copy because it knows the offsets, strides, etc of the underlying obj in the buffer So, I think we should instead finish the parts of memoryview that are missing: 1) IO support for memoryview (read into a memoryview object and write from a memoryview object without converting to strings) 2) Add classes that implement the memoryview protocol 3) change String.bytes to return a new ByteArray class that implements the memoryview protocol 4) add a ruby extension that allows you to use memory view objects in ruby (not just Fiddle) mv = MemoryView.new(obj) mv[offset:offset_size] #slicing memory views mv.cast(format, shape) - change format or shape of memoryview but keep data mv.format mv.strides mv.shape ---------------------------------------- Feature #17790: Have a way to clear a String without resetting its capacity https://bugs.ruby-lang.org/issues/17790#change-91647 * 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/