Skip to content

Commit 4a2b510

Browse files
committed
Expose rb_hash_new_capa(long)
[Feature #18683] This allows parsers and similar libraries to create Hashes of a certain capacity in advance. It's useful when the key and values are streamed, hence `bulk_insert()` can't be used.
1 parent 5ce0d2a commit 4a2b510

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

hash.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1575,6 +1575,12 @@ rb_hash_new_with_size(st_index_t size)
15751575
return ret;
15761576
}
15771577

1578+
VALUE
1579+
rb_hash_new_capa(long capa)
1580+
{
1581+
return rb_hash_new_with_size((st_index_t)capa);
1582+
}
1583+
15781584
static VALUE
15791585
hash_copy(VALUE ret, VALUE hash)
15801586
{

include/ruby/internal/intern/hash.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,17 @@ VALUE rb_hash(VALUE obj);
106106
*/
107107
VALUE rb_hash_new(void);
108108

109+
/**
110+
* Identical to rb_hash_new(), except it additionally specifies how many keys
111+
* it is expected to contain. This way you can create a hash that is large enough
112+
* for your need. For large hashes it means it won't need to be reallocated and
113+
* rehashed as much, improving performance.
114+
*
115+
* @param[in] capa Designed capacity of the hash.
116+
* @return An empty Hash, whose capacity is `capa`.
117+
*/
118+
VALUE rb_hash_new_capa(long capa);
119+
109120
/**
110121
* Duplicates a hash.
111122
*

0 commit comments

Comments
 (0)