From: Eric Wong Date: 2021-04-09T21:42:38+00:00 Subject: [ruby-core:103357] Re: [Ruby master Feature#17790] Have a way to clear a String without resetting its capacity jean.boussier@gmail.com wrote: > > so providing a capacity method gives more control over how to expand memory > > Agreed. Without also exposing the capacity, my proposed change would be a big footgun. Yes, rb_str_resize(str, 0) is common to workaround the lack of escape analysis inside the core VM and some C exts. I think it's reasonable for Rubyists to use String#clear for the same purpose. > Maybe `String#capacity` and `String#capacity=` would make sense? But then there's the question of the behavior if you set the capacity to lower than the `size`. Should it truncate? (this could corrupt UTF-8 for instance) or should it raise? Yes, but I don't know what it should do for corruption. It would also be useful for IO#read-like methods if/when that supports destination buffer offsets. > Additionally I think `Array` and `Hash` should expose similar ways of querying and reserving capacity. Probably, yes. It seems a bit low-level, but I've been favoring "semi-automatic" memory management since we probably can't have escape analysis due to the C API. Unsubscribe: