Optimized instruction for Array#freeze

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

[Feature #20684]

Co-authored-by: Jean Boussier <byroot@ruby-lang.org>
This commit is contained in:
Étienne Barrié 2024-07-29 12:15:02 +02:00 committed by Jean Boussier
parent 63cbe3f6ac
commit a99707cd9c
Notes: git 2024-09-05 10:46:24 +00:00
10 changed files with 324 additions and 159 deletions

View file

@ -919,6 +919,21 @@ objtostring
}
}
DEFINE_INSN
opt_ary_freeze
(VALUE ary, CALL_DATA cd)
()
(VALUE val)
{
val = vm_opt_ary_freeze(ary, BOP_FREEZE, idFreeze);
if (UNDEF_P(val)) {
RUBY_DTRACE_CREATE_HOOK(ARRAY, RARRAY_LEN(ary));
PUSH(rb_ary_resurrect(ary));
CALL_SIMPLE_METHOD();
}
}
DEFINE_INSN
opt_str_freeze
(VALUE str, CALL_DATA cd)