summaryrefslogtreecommitdiff
path: root/hash.c
diff options
context:
space:
mode:
authorshyouhei <shyouhei@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-09-05 04:48:19 (GMT)
committershyouhei <shyouhei@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-09-05 04:48:19 (GMT)
commit0eb7359cc775c9d4c8dad68dd9dbf38c9680071e (patch)
treea4e596d906225086566efe79e792a3f204a16b57 /hash.c
parente5684558262ead5253b9fce36efaea6a6ae6acab (diff)
add rb_hash_new_with_size()
Sometimes, size of a hash can be calcluated a priori. By providing such info to the constructor we can avoid unnecessary internal re- allocations. This can boost for instance creation of hash literals. [Bug #13861] Signed-off-by: Urabe, Shyouhei <shyouhei@ruby-lang.org> git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59744 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'hash.c')
-rw-r--r--hash.c11
1 files changed, 10 insertions, 1 deletions
diff --git a/hash.c b/hash.c
index a80bd0a..bc02696 100644
--- a/hash.c
+++ b/hash.c
@@ -432,6 +432,15 @@ rb_hash_new(void)
return hash_alloc(rb_cHash);
}
+VALUE
+rb_hash_new_with_size(st_index_t size)
+{
+ VALUE ret = rb_hash_new();
+ if (size)
+ RHASH(ret)->ntbl = st_init_table_with_size(&objhash, size);
+ return ret;
+}
+
static VALUE
hash_dup(VALUE hash, VALUE klass, VALUE flags)
{
@@ -1927,7 +1936,7 @@ rb_hash_transform_values(VALUE hash)
VALUE result;
RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size);
- result = rb_hash_new();
+ result = rb_hash_new_with_size(RHASH_SIZE(hash));
if (!RHASH_EMPTY_P(hash)) {
rb_hash_foreach(hash, transform_values_i, result);
}