New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
String#clear keep the string capacity #4373
base: master
Are you sure you want to change the base?
Conversation
[Feature #17790] This is useful when trying to efficently re-use a buffer.
It doesn't look like Array#clear does keep the capacity ( Lines 4554 to 4556 in ff43ecc
|
Interesting, I actually didn't think to check the implementation. So apparently |
This is an interesting issue. Given that users can choose between:
I believe it's reasonable that This would be consistent with the typical use cases - buffers, etc, where the string/array/thing would be reused with the similar amount of data. The only case where it might not be realistic is when |
Yes, that's what I'm worried of. Also the idea is to be able to empty the string (or array or hash for that matter) while still retaining its capacity to reuse it. But I agree with some of the concerns raised. To do this safely you might often need to be able to query that capacity to decide wether you'd rather shrink it or not. So maybe what would be needed would be |
My feeling is 99% of these problems happen because we try to use strings for binary buffers. This problem has been going around in circles for a long time (at least several years). Honestly, not sure what the solution is - my last attempt to solve this was to treat strings with binary encoding differently (assumption that it's a data buffer). The implication in this issue would be that if the string was binary encoded, If you wanted to be more generic:
That being said, I can imagine tons of use cases where |
I don't see why the encoding matters, since non-binary encoded strings could still be used as a buffer with mutable strings.
|
[Feature #17790] https://bugs.ruby-lang.org/issues/17790
This is useful when trying to efficiently re-use a buffer, and is consistent with
Array#clear
.