diff options
Diffstat (limited to 'hash.c')
-rw-r--r-- | hash.c | 21 |
1 files changed, 14 insertions, 7 deletions
@@ -4380,15 +4380,22 @@ rb_hash_compare_by_id(VALUE hash) ar_force_convert_table(hash, __FILE__, __LINE__); HASH_ASSERT(RHASH_ST_TABLE_P(hash)); - tmp = hash_alloc(0); - hash_st_table_init(tmp, &identhash, RHASH_SIZE(hash)); - identtable = RHASH_ST_TABLE(tmp); + if (RHASH_TABLE_EMPTY_P(hash)) { + // Fast path: There's nothing to rehash, so we don't need a `tmp` table. + RHASH_ST_TABLE(hash)->type = &identhash; + } else { + // Slow path: Need to rehash the members of `self` into a new + // `tmp` table using the new `identhash` compare/hash functions. + tmp = hash_alloc(0); + hash_st_table_init(tmp, &identhash, RHASH_SIZE(hash)); + identtable = RHASH_ST_TABLE(tmp); - rb_hash_foreach(hash, rb_hash_rehash_i, (VALUE)tmp); + rb_hash_foreach(hash, rb_hash_rehash_i, (VALUE)tmp); - rb_hash_free(hash); - RHASH_ST_TABLE_SET(hash, identtable); - RHASH_ST_CLEAR(tmp); + rb_hash_free(hash); + RHASH_ST_TABLE_SET(hash, identtable); + RHASH_ST_CLEAR(tmp); + } return hash; } |