summaryrefslogtreecommitdiff
path: root/hash.c
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2023-11-07 12:10:00 +0900
committerYusuke Endoh <mame@ruby-lang.org>2023-11-21 15:15:23 +0900
commitf5c3cda7d6e9053ae5ed66e36d13e568595ff1ec (patch)
treef25083e886a9aa4bb96956c12696a7d108d96814 /hash.c
parent1cf2fa3af50203f261341ff4c250735c9da5767b (diff)
Do not change hash type in Hash#assoc
Diffstat (limited to 'hash.c')
-rw-r--r--hash.c61
1 files changed, 27 insertions, 34 deletions
diff --git a/hash.c b/hash.c
index 78fbfb866c..d025b1c30a 100644
--- a/hash.c
+++ b/hash.c
@@ -4080,24 +4080,17 @@ assoc_cmp(VALUE a, VALUE b)
return !RTEST(rb_equal(a, b));
}
-static VALUE
-lookup2_call(VALUE arg)
-{
- VALUE *args = (VALUE *)arg;
- return rb_hash_lookup2(args[0], args[1], Qundef);
-}
-
-struct reset_hash_type_arg {
- VALUE hash;
- const struct st_hash_type *orighash;
+struct assoc_arg {
+ st_table *tbl;
+ st_data_t key;
};
static VALUE
-reset_hash_type(VALUE arg)
+assoc_lookup(VALUE arg)
{
- struct reset_hash_type_arg *p = (struct reset_hash_type_arg *)arg;
- HASH_ASSERT(RHASH_ST_TABLE_P(p->hash));
- RHASH_ST_TABLE(p->hash)->type = p->orighash;
+ struct assoc_arg *p = (struct assoc_arg*)arg;
+ st_data_t data;
+ if (st_lookup(p->tbl, p->key, &data)) return (VALUE)data;
return Qundef;
}
@@ -4127,30 +4120,30 @@ assoc_i(VALUE key, VALUE val, VALUE arg)
static VALUE
rb_hash_assoc(VALUE hash, VALUE key)
{
- st_table *table;
- const struct st_hash_type *orighash;
VALUE args[2];
if (RHASH_EMPTY_P(hash)) return Qnil;
- ar_force_convert_table(hash, __FILE__, __LINE__);
- HASH_ASSERT(RHASH_ST_TABLE_P(hash));
- table = RHASH_ST_TABLE(hash);
- orighash = table->type;
-
- if (orighash != &identhash) {
- VALUE value;
- struct reset_hash_type_arg ensure_arg;
- struct st_hash_type assochash;
-
- assochash.compare = assoc_cmp;
- assochash.hash = orighash->hash;
- table->type = &assochash;
- args[0] = hash;
- args[1] = key;
- ensure_arg.hash = hash;
- ensure_arg.orighash = orighash;
- value = rb_ensure(lookup2_call, (VALUE)&args, reset_hash_type, (VALUE)&ensure_arg);
+ if (RHASH_ST_TABLE_P(hash) && RHASH_ST_TABLE(hash)->type != &identhash) {
+ VALUE value = Qundef;
+ st_table assoctable = *RHASH_ST_TABLE(hash);
+ assoctable.type = &(struct st_hash_type){
+ .compare = assoc_cmp,
+ .hash = assoctable.type->hash,
+ };
+ VALUE arg = (VALUE)&(struct assoc_arg){
+ .tbl = &assoctable,
+ .key = (st_data_t)key,
+ };
+
+ if (RB_OBJ_FROZEN(hash)) {
+ value = assoc_lookup(arg);
+ }
+ else {
+ hash_iter_lev_inc(hash);
+ value = rb_ensure(assoc_lookup, arg, hash_foreach_ensure, hash);
+ }
+ hash_verify(hash);
if (!UNDEF_P(value)) return rb_assoc_new(key, value);
}