From: "shan (Shannon Skipper)" <noreply@...> Date: 2022-04-20T19:36:13+00:00 Subject: [ruby-core:108321] [Ruby master Feature#18683] Allow to create hashes with a specific capacity. Issue #18683 has been updated by shan (Shannon Skipper). Another option would be `Hash.new { capacity }` like `to_enum { size }`. ---------------------------------------- Feature #18683: Allow to create hashes with a specific capacity. https://bugs.ruby-lang.org/issues/18683#change-97342 * 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>