[ruby-core:108187] [Ruby master Feature#18683] Allow to create hashes with a specific capacity.
From:
"jeremyevans0 (Jeremy Evans)" <noreply@...>
Date:
2022-04-06 16:47:48 UTC
List:
ruby-core #108187
Issue #18683 has been updated by jeremyevans0 (Jeremy Evans).
This would break backwards compatibility:
```ruby
Hash.new(capacity: 1000)[nil]
# => {:capacity=>1000}
```
----------------------------------------
Feature #18683: Allow to create hashes with a specific capacity.
https://bugs.ruby-lang.org/issues/18683#change-97158
* Author: byroot (Jean Boussier)
* Status: Open
* Priority: Normal
----------------------------------------
Various protocol parsers such as Redis `RESP3` or `msgpack`, have to create hashes, and they know the size in advance.
For efficiency, it would be preferable if they could directly allocate a Hash of the necessary size, so that large hashes wouldn't cause many re-alloccations.
Example of code that would benefit:
- [`hiredis` bindings](https://github.com/redis-rb/redis-client/blob/830d586b665bc9569335d70e82c41377f18e0c16/ext/redis_client/hiredis/hiredis_connection.c#L157-L162)
- [Ruby `redis RESP3` parser](https://github.com/redis-rb/redis-client/blob/830d586b665bc9569335d70e82c41377f18e0c16/lib/redis_client/resp3.rb#L173-L175)
- [magpack-ruby](https://github.com/msgpack/msgpack-ruby/blob/c46bb60f79312cab902356e89f3f6035d7cad03f/ext/msgpack/unpacker.c#L641-L644)
`String` and `Array` both already offer similar APIs:
```ruby
String.new(capacity: XXX)
Array.new(XX) / rb_ary_new_capa(long)
```
However there's no such public API for Hashes, neither in Ruby land not in the C extension API.
### Proposal
I think `Hash.new` should accept a `capacity:` named parameter:
```ruby
hash = Hash.new(capacity: 1000)
```
Additionally I think the internal `rb_hash_new_with_size` function should be exposed to C extensions as `rb_hash_new_capa(long)`, for consistency with `rb_ary_new_capa(long)`.
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>