Project

General

Profile

Actions

Feature #19236

closed

Allow to create hashes with a specific capacity from Ruby

Added by byroot (Jean Boussier) about 2 years ago. Updated 5 months ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:111306]

Description

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:

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 (Benoit Daloze) proposed to add Hash.create(capacity: 1000).


Related issues 1 (0 open1 closed)

Related to Ruby master - Feature #18683: Allow to create hashes with a specific capacity.ClosedActions
Actions

Also available in: Atom PDF

Like2
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0