Optimized instruction for Hash#freeze

If a Hash which is empty or only using literals is frozen, we detect
this as a peephole optimization and change the instructions to be
`opt_hash_freeze`.

[Feature #20684]

Co-authored-by: Jean Boussier <byroot@ruby-lang.org>
This commit is contained in:
Étienne Barrié 2024-08-05 12:31:24 +02:00 committed by Jean Boussier
parent a99707cd9c
commit bf9879791a
Notes: git 2024-09-05 10:46:23 +00:00
10 changed files with 325 additions and 158 deletions

View file

@ -934,6 +934,21 @@ opt_ary_freeze
}
}
DEFINE_INSN
opt_hash_freeze
(VALUE hash, CALL_DATA cd)
()
(VALUE val)
{
val = vm_opt_hash_freeze(hash, BOP_FREEZE, idFreeze);
if (UNDEF_P(val)) {
RUBY_DTRACE_CREATE_HOOK(HASH, RHASH_SIZE(hash) << 1);
PUSH(rb_hash_resurrect(hash));
CALL_SIMPLE_METHOD();
}
}
DEFINE_INSN
opt_str_freeze
(VALUE str, CALL_DATA cd)