summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJean Boussier <jean.boussier@gmail.com>2022-04-22 15:08:19 +0100
committerJean Boussier <jean.boussier@gmail.com>2022-04-26 16:12:47 +0200
commit4a2b5102c11978b66d003ecbc8710b2f587b7322 (patch)
tree9164539b65fc3be414aeb04aa5c7bf8a1fd96774
parent5ce0d2aa354eb996cb3ca9bb944f880ff6acfd57 (diff)
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.
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/5835
-rw-r--r--hash.c6
-rw-r--r--include/ruby/internal/intern/hash.h11
2 files changed, 17 insertions, 0 deletions
diff --git a/hash.c b/hash.c
index da85fd35c6..1f2bca12b3 100644
--- a/hash.c
+++ b/hash.c
@@ -1575,6 +1575,12 @@ rb_hash_new_with_size(st_index_t size)
return ret;
}
+VALUE
+rb_hash_new_capa(long capa)
+{
+ return rb_hash_new_with_size((st_index_t)capa);
+}
+
static VALUE
hash_copy(VALUE ret, VALUE hash)
{
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
@@ -107,6 +107,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.
*
* @param[in] hash An instance of ::rb_cHash.