ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:103342] [Ruby master Feature#17790] Have a way to clear a String without resetting its capacity
@ 2021-04-09 10:42 jean.boussier
  2021-04-09 11:30 ` [ruby-core:103344] " jean.boussier
                   ` (11 more replies)
  0 siblings, 12 replies; 14+ messages in thread
From: jean.boussier @ 2021-04-09 10:42 UTC (permalink / raw)
  To: ruby-core

Issue #17790 has been reported by byroot (Jean Boussier).

----------------------------------------
Feature #17790: Have a way to clear a String without resetting its capacity
https://bugs.ruby-lang.org/issues/17790

* 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/

^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2021-04-21 23:27 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-09 10:42 [ruby-core:103342] [Ruby master Feature#17790] Have a way to clear a String without resetting its capacity jean.boussier
2021-04-09 11:30 ` [ruby-core:103344] " jean.boussier
2021-04-09 13:38 ` [ruby-core:103347] " marcandre-ruby-core
2021-04-09 17:53 ` [ruby-core:103349] " eregontp
2021-04-09 18:39 ` [ruby-core:103352] " dylan.smith
2021-04-09 18:44 ` [ruby-core:103353] " dylan.smith
2021-04-09 21:28 ` [ruby-core:103356] " jean.boussier
2021-04-09 21:42   ` [ruby-core:103357] " Eric Wong
2021-04-10  9:28 ` [ruby-core:103369] " eregontp
2021-04-10  9:33 ` [ruby-core:103370] " eregontp
2021-04-10 15:17 ` [ruby-core:103373] " jean.boussier
2021-04-12  1:29 ` [ruby-core:103387] " daniel
2021-04-14 18:28 ` [ruby-core:103451] " dylan.smith
2021-04-21 23:27 ` [ruby-core:103546] " dsisnero

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).