From: "Eregon (Benoit Daloze)" Date: 2022-04-21T10:31:57+00:00 Subject: [ruby-core:108345] [Ruby master Feature#18683] Allow to create hashes with a specific capacity. Issue #18683 has been updated by Eregon (Benoit Daloze). I also thought about `Array#to_h`, but in cases such as deserialization like above there is no Array and the extra allocations/operations would be non-trivial costs. I think a separate method like `Hash.create` would be a good way to expose it to Ruby (I don't like setter API, can easily be used wrong). ---------------------------------------- Feature #18683: Allow to create hashes with a specific capacity. https://bugs.ruby-lang.org/issues/18683#change-97370 * 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: