From: "mame (Yusuke Endoh) via ruby-core" Date: 2023-01-20T05:31:54+00:00 Subject: [ruby-core:111919] [Ruby master Feature#19236] Allow to create hashes with a specific capacity from Ruby Issue #19236 has been updated by mame (Yusuke Endoh). Discussed at the dev meeting. @matz said that `Hash.create(capacity: 4096)` is acceptable (unless it conflicts with any major gems). However, several participants including @ko1 were a little cautious about introducing the new terminology "create" into Ruby core, and matz understood that. @matsuda and @mame prefer `Hash.new(capacity: 4096)`. This is a bit incompatible, but we searched gem-codesearch with the query `'\bHash\.new\(\w+: '` and found less than 20 results (manually excluding `Foo::Bar::Hash.new(...)` which is perhaps different from `::Hash`). Moreover, some of the results seemed to misunderstand `Hash.new(foo: 1)` as `{ foo: 1 }`. (The most examples are rspec; maybe because `let(:option) { { foo: 1 } }` looks bad, people inadvertently rewrote it with `let(:option) { Hash.new(foo: 1) }`.) Therefore, how about deprecating giving the keyword to `Hash.new` and then introducing `Hash.new(capacity: 4096)`? @matz said this is also acceptable if the incompatibility is not a big problem. (Off-topic: `Array.new(capacity: 4096)` is not yet available; I wonder if people want `Hash.new(capacity: 4096)` more than Array?) ---------------------------------------- Feature #19236: Allow to create hashes with a specific capacity from Ruby https://bugs.ruby-lang.org/issues/19236#change-101342 * Author: byroot (Jean Boussier) * Status: Open * Priority: Normal * Target version: 3.3 ---------------------------------------- Followup on [Feature #18683] which added a C-API for this purpose. 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 and re-hash. `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 in Ruby land. ### Proposal I think `Hash` should have a way to create a new hash with a `capacity` parameter. The logical signature of `Hash.new(capacity: 1000)` was deemed too incompatible in [Feature #18683]. @Eregon proposed to add `Hash.create(capacity: 1000)`. -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/