aboutsummaryrefslogtreecommitdiffstats
path: root/hash.c
diff options
context:
space:
mode:
authorglass <glass@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-10-22 00:57:46 +0000
committerglass <glass@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-10-22 00:57:46 +0000
commit0c937ec37e07a0eec4e8bf3a3326f9a434d0b2e7 (patch)
treed40463d80447cf602b75d2c446eb25f98f40b5a3 /hash.c
parent029927fccc1c1d85185e617bf7d6d7b88f7a9f3b (diff)
downloadruby-0c937ec37e07a0eec4e8bf3a3326f9a434d0b2e7.tar.gz
hash.c: optimize Hash#compare_by_identity
hash.c (rb_hash_compare_by_id): avoid unnecessary allocation of st_table. formerly, st_table created in rb_hash_modify() was not used and replaced immediately. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60337 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'hash.c')
-rw-r--r--hash.c12
1 files changed, 9 insertions, 3 deletions
diff --git a/hash.c b/hash.c
index ecc9110d58..25ab9959a4 100644
--- a/hash.c
+++ b/hash.c
@@ -2899,10 +2899,16 @@ rb_hash_compact_bang(VALUE hash)
static VALUE
rb_hash_compare_by_id(VALUE hash)
{
+ st_table *identtable;
if (rb_hash_compare_by_id_p(hash)) return hash;
- rb_hash_modify(hash);
- RHASH(hash)->ntbl->type = &identhash;
- rb_hash_rehash(hash);
+ rb_hash_modify_check(hash);
+
+ identtable = rb_init_identtable_with_size(RHASH_SIZE(hash));
+ rb_hash_foreach(hash, rb_hash_rehash_i, (VALUE)identtable);
+ if (RHASH(hash)->ntbl)
+ st_free_table(RHASH(hash)->ntbl);
+ RHASH(hash)->ntbl = identtable;
+
return hash;
}