summaryrefslogtreecommitdiff
path: root/lib/ruby_vm/rjit
diff options
context:
space:
mode:
authorÉtienne Barrié <[email protected]>2024-08-05 12:31:24 +0200
committerJean Boussier <[email protected]>2024-09-05 12:46:02 +0200
commitbf9879791af11a20e50921551220c08d1c7f7f02 (patch)
tree68732e79fac871a629fc815b525f0cc20ed1c8bc /lib/ruby_vm/rjit
parenta99707cd9c6a1d53cf8ebc883dc210219bd67a28 (diff)
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 <[email protected]>
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/11406
Diffstat (limited to 'lib/ruby_vm/rjit')
-rw-r--r--lib/ruby_vm/rjit/insn_compiler.rb19
1 files changed, 19 insertions, 0 deletions
diff --git a/lib/ruby_vm/rjit/insn_compiler.rb b/lib/ruby_vm/rjit/insn_compiler.rb
index c6b886e192..a33ba9f468 100644
--- a/lib/ruby_vm/rjit/insn_compiler.rb
+++ b/lib/ruby_vm/rjit/insn_compiler.rb
@@ -91,6 +91,7 @@ module RubyVM::RJIT
when :objtostring then objtostring(jit, ctx, asm)
when :opt_str_freeze then opt_str_freeze(jit, ctx, asm)
when :opt_ary_freeze then opt_ary_freeze(jit, ctx, asm)
+ when :opt_hash_freeze then opt_hash_freeze(jit, ctx, asm)
when :opt_nil_p then opt_nil_p(jit, ctx, asm)
# opt_str_uminus
when :opt_newarray_send then opt_newarray_send(jit, ctx, asm)
@@ -1512,6 +1513,24 @@ module RubyVM::RJIT
# @param jit [RubyVM::RJIT::JITState]
# @param ctx [RubyVM::RJIT::Context]
# @param asm [RubyVM::RJIT::Assembler]
+ def opt_hash_freeze(jit, ctx, asm)
+ unless Invariants.assume_bop_not_redefined(jit, C::HASH_REDEFINED_OP_FLAG, C::BOP_FREEZE)
+ return CantCompile;
+ end
+
+ hash = jit.operand(0, ruby: true)
+
+ # Push the return value onto the stack
+ stack_ret = ctx.stack_push(Type::CHash)
+ asm.mov(:rax, to_value(hash))
+ asm.mov(stack_ret, :rax)
+
+ KeepCompiling
+ end
+
+ # @param jit [RubyVM::RJIT::JITState]
+ # @param ctx [RubyVM::RJIT::Context]
+ # @param asm [RubyVM::RJIT::Assembler]
def opt_str_freeze(jit, ctx, asm)
unless Invariants.assume_bop_not_redefined(jit, C::STRING_REDEFINED_OP_FLAG, C::BOP_FREEZE)
return CantCompile;