ruby-changes:46074
From: normal <ko1@a...>
Date: Mon, 27 Mar 2017 15:12:58 +0900 (JST)
Subject: [ruby-changes:46074] normal:r58144 (trunk): fix redefinition optimization for -"literal string" (UMinus)
normal 2017-03-27 15:12:37 +0900 (Mon, 27 Mar 2017) New Revision: 58144 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=58144 Log: fix redefinition optimization for -"literal string" (UMinus) Unfortunately this enlarges insns.def by yet another instruction. However, it is much prettier than opt_str_freeze in use, and maybe we can avoid having so many instructions in the future. [ruby-core:80368] * insns.def (DEFINE_INSN): new instruction: opt_str_uminus (maybe temporary) * compile.c (iseq_compile_each0): split instructions * test/ruby/test_optimization.rb (test_string_uminus): new test * vm.c (vm_init_redefined_flag): set redefinintion flag for uminus * vm_core.h (enum ruby_basic_operators): add BOP_UMINUS Modified files: trunk/compile.c trunk/insns.def trunk/test/ruby/test_optimization.rb trunk/vm.c trunk/vm_core.h Index: vm.c =================================================================== --- vm.c (revision 58143) +++ vm.c (revision 58144) @@ -1569,6 +1569,7 @@ vm_init_redefined_flag(void) https://github.com/ruby/ruby/blob/trunk/vm.c#L1569 OP(Succ, SUCC), (C(Integer), C(String), C(Time)); OP(EqTilde, MATCH), (C(Regexp), C(String)); OP(Freeze, FREEZE), (C(String)); + OP(UMinus, UMINUS), (C(String)); OP(Max, MAX), (C(Array)); OP(Min, MIN), (C(Array)); #undef C Index: test/ruby/test_optimization.rb =================================================================== --- test/ruby/test_optimization.rb (revision 58143) +++ test/ruby/test_optimization.rb (revision 58144) @@ -104,6 +104,11 @@ class TestRubyOptimization < Test::Unit: https://github.com/ruby/ruby/blob/trunk/test/ruby/test_optimization.rb#L104 assert_redefine_method('String', 'freeze', 'assert_nil "foo".freeze') end + def test_string_uminus + assert_same "foo".freeze, -"foo" + assert_redefine_method('String', '-@', 'assert_nil(-"foo")') + end + def test_string_freeze_saves_memory n = 16384 data = '.'.freeze Index: insns.def =================================================================== --- insns.def (revision 58143) +++ insns.def (revision 58144) @@ -982,6 +982,20 @@ opt_str_freeze https://github.com/ruby/ruby/blob/trunk/insns.def#L982 } DEFINE_INSN +opt_str_uminus +(VALUE str) +() +(VALUE val) +{ + if (BASIC_OP_UNREDEFINED_P(BOP_UMINUS, STRING_REDEFINED_OP_FLAG)) { + val = str; + } + else { + val = rb_funcall(rb_str_resurrect(str), idUMinus, 0); + } +} + +DEFINE_INSN opt_newarray_max (rb_num_t num) (...) Index: vm_core.h =================================================================== --- vm_core.h (revision 58143) +++ vm_core.h (revision 58144) @@ -454,6 +454,7 @@ enum ruby_basic_operators { https://github.com/ruby/ruby/blob/trunk/vm_core.h#L454 BOP_NEQ, BOP_MATCH, BOP_FREEZE, + BOP_UMINUS, BOP_MAX, BOP_MIN, Index: compile.c =================================================================== --- compile.c (revision 58143) +++ compile.c (revision 58144) @@ -5211,7 +5211,12 @@ iseq_compile_each0(rb_iseq_t *iseq, LINK https://github.com/ruby/ruby/blob/trunk/compile.c#L5211 ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction) { VALUE str = rb_fstring(node->nd_recv->nd_lit); iseq_add_mark_object(iseq, str); - ADD_INSN1(ret, line, opt_str_freeze, str); + if (node->nd_mid == idUMinus) { + ADD_INSN1(ret, line, opt_str_uminus, str); + } + else { + ADD_INSN1(ret, line, opt_str_freeze, str); + } if (popped) { ADD_INSN(ret, line, pop); } -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/