From c5fc1ce975ecdf1c6818714e47579c5d3531c4ca Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 6 Jun 2022 17:27:56 -0700 Subject: Emit special instruction for array literal + .(hash|min|max) This commit introduces a new instruction `opt_newarray_send` which is used when there is an array literal followed by either the `hash`, `min`, or `max` method. ``` [a, b, c].hash ``` Will emit an `opt_newarray_send` instruction. This instruction falls back to a method call if the "interested" method has been monkey patched. Here are some examples of the instructions generated: ``` $ ./miniruby --dump=insns -e '[@a, @b].max' == disasm: #@-e:1 (1,0)-(1,12)> (catch: FALSE) 0000 getinstancevariable :@a, ( 1)[Li] 0003 getinstancevariable :@b, 0006 opt_newarray_send 2, :max 0009 leave $ ./miniruby --dump=insns -e '[@a, @b].min' == disasm: #@-e:1 (1,0)-(1,12)> (catch: FALSE) 0000 getinstancevariable :@a, ( 1)[Li] 0003 getinstancevariable :@b, 0006 opt_newarray_send 2, :min 0009 leave $ ./miniruby --dump=insns -e '[@a, @b].hash' == disasm: #@-e:1 (1,0)-(1,13)> (catch: FALSE) 0000 getinstancevariable :@a, ( 1)[Li] 0003 getinstancevariable :@b, 0006 opt_newarray_send 2, :hash 0009 leave ``` [Feature #18897] [ruby-core:109147] Co-authored-by: John Hawthorn --- bootstraptest/test_insns.rb | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'bootstraptest') diff --git a/bootstraptest/test_insns.rb b/bootstraptest/test_insns.rb index 91fba9b011..d2e799f855 100644 --- a/bootstraptest/test_insns.rb +++ b/bootstraptest/test_insns.rb @@ -214,9 +214,11 @@ tests = [ 'true'.freeze }, - [ 'opt_newarray_max', %q{ [ ].max.nil? }, ], - [ 'opt_newarray_max', %q{ [1, x = 2, 3].max == 3 }, ], - [ 'opt_newarray_max', <<-'},', ], # { + [ 'opt_newarray_send', %q{ ![ ].hash.nil? }, ], + + [ 'opt_newarray_send', %q{ [ ].max.nil? }, ], + [ 'opt_newarray_send', %q{ [1, x = 2, 3].max == 3 }, ], + [ 'opt_newarray_send', <<-'},', ], # { class Array def max true @@ -224,9 +226,9 @@ tests = [ end [1, x = 2, 3].max }, - [ 'opt_newarray_min', %q{ [ ].min.nil? }, ], - [ 'opt_newarray_min', %q{ [3, x = 2, 1].min == 1 }, ], - [ 'opt_newarray_min', <<-'},', ], # { + [ 'opt_newarray_send', %q{ [ ].min.nil? }, ], + [ 'opt_newarray_send', %q{ [3, x = 2, 1].min == 1 }, ], + [ 'opt_newarray_send', <<-'},', ], # { class Array def min true -- cgit v1.2.1