From 4a2b5102c11978b66d003ecbc8710b2f587b7322 Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Fri, 22 Apr 2022 15:08:19 +0100 Subject: 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. --- include/ruby/internal/intern/hash.h | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'include/ruby') diff --git a/include/ruby/internal/intern/hash.h b/include/ruby/internal/intern/hash.h index 3cce7f61c7..af8dfd5d8f 100644 --- a/include/ruby/internal/intern/hash.h +++ b/include/ruby/internal/intern/hash.h @@ -106,6 +106,17 @@ VALUE rb_hash(VALUE obj); */ VALUE rb_hash_new(void); +/** + * Identical to rb_hash_new(), except it additionally specifies how many keys + * it is expected to contain. This way you can create a hash that is large enough + * for your need. For large hashes it means it won't need to be reallocated and + * rehashed as much, improving performance. + * + * @param[in] capa Designed capacity of the hash. + * @return An empty Hash, whose capacity is `capa`. + */ +VALUE rb_hash_new_capa(long capa); + /** * Duplicates a hash. * -- cgit v1.2.3