[前][次][番号順一覧][スレッド一覧]

ruby-changes:51005

From: k0kubun <ko1@a...>
Date: Fri, 20 Apr 2018 22:42:14 +0900 (JST)
Subject: [ruby-changes:51005] k0kubun:r63212 (trunk): _mjit_compile_send.erb: inline attr_reader call

k0kubun	2018-04-20 22:42:08 +0900 (Fri, 20 Apr 2018)

  New Revision: 63212

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=63212

  Log:
    _mjit_compile_send.erb: inline attr_reader call
    
    _mjit_compile_send_guard.erb: carve out the shared logic to invalidate
    inlined method call
    
    common.mk: update dependency for this change
    
    test_jit.rb: add test for attr_reader optimization
    
    * Benchmark
    
    ```
    require 'benchmark_driver'
    
    Benchmark.driver do |x|
      x.prelude %{
        class C
          attr_reader :a
          def initialize
            @a = 1
          end
        end
    
        o = C.new
    
        def l o
          i = 0
          while i < 1000000
            o.a
            i += 1
          end
        end
      }
      x.report 'aread', %{ l o }
      x.loop_count 1000
    
      x.rbenv 'before', 'before,--jit', 'after,--jit'
      x.verbose
    end
    ```
    
    ```
    before: ruby 2.6.0dev (2018-04-20 trunk 63211) [x86_64-linux]
    before,--jit: ruby 2.6.0dev (2018-04-20 trunk 63211) +JIT [x86_64-linux]
    after,--jit: ruby 2.6.0dev (2018-04-20 trunk 63211) +JIT [x86_64-linux]
    last_commit=_mjit_compile_send.erb: inline attr_reader call
    Calculating -------------------------------------
                             before  before,--jit  after,--jit
                   aread     54.597       122.894      218.574 i/s -      1.000k times in 18.316102s 8.137089s 4.575106s
    
    Comparison:
                                aread
             after,--jit:       218.6 i/s
            before,--jit:       122.9 i/s - 1.78x  slower
                  before:        54.6 i/s - 4.00x  slower
    
    ```
    
    * Optcarrot
    
    A little made faster?
    
    fps: 71.35 -> 72.11

  Added files:
    trunk/tool/ruby_vm/views/_mjit_compile_send_guard.erb
  Modified files:
    trunk/common.mk
    trunk/test/ruby/test_jit.rb
    trunk/tool/ruby_vm/views/_mjit_compile_send.erb
Index: tool/ruby_vm/views/_mjit_compile_send_guard.erb
===================================================================
--- tool/ruby_vm/views/_mjit_compile_send_guard.erb	(nonexistent)
+++ tool/ruby_vm/views/_mjit_compile_send_guard.erb	(revision 63212)
@@ -0,0 +1,14 @@ https://github.com/ruby/ruby/blob/trunk/tool/ruby_vm/views/_mjit_compile_send_guard.erb#L1
+% # Copyright (c) 2018 Takashi Kokubun.  All rights reserved.
+% #
+% # This file is a part of  the programming language Ruby.  Permission is hereby
+% # granted, to either  redistribute and/or modify this file,  provided that the
+% # conditions mentioned  in the  file COPYING  are met.   Consult the  file for
+% # details.
+%
+% # JIT: Invalidate call cache if it requires vm_search_method. This allows to inline some of following things.
+            fprintf(f, "    if (UNLIKELY(GET_GLOBAL_METHOD_STATE() != %"PRI_SERIALT_PREFIX"u ||\n", cc->method_state);
+            fprintf(f, "        RCLASS_SERIAL(CLASS_OF(stack[%d])) != %"PRI_SERIALT_PREFIX"u)) {\n", b->stack_size - 1 - argc, cc->class_serial);
+            fprintf(f, "        reg_cfp->pc = original_body_iseq + %d;\n", pos);
+            fprintf(f, "        reg_cfp->sp = (VALUE *)reg_cfp->bp + %d;\n", b->stack_size + 1);
+            fprintf(f, "        goto cancel;\n");
+            fprintf(f, "    }\n");
Index: tool/ruby_vm/views/_mjit_compile_send.erb
===================================================================
--- tool/ruby_vm/views/_mjit_compile_send.erb	(revision 63211)
+++ tool/ruby_vm/views/_mjit_compile_send.erb	(revision 63212)
@@ -29,16 +29,12 @@ https://github.com/ruby/ruby/blob/trunk/tool/ruby_vm/views/_mjit_compile_send.erb#L29
                 fprintf(f, "    MAYBE_UNUSED(unsigned int) stack_size = %u;\n", b->stack_size);
             }
 
+% # JIT: Invalidate call cache if it requires vm_search_method. This allows to inline some of following things.
+<%= render 'mjit_compile_send_guard' -%>
+
 % # JIT: move sp and pc if necessary
 <%= render 'mjit_compile_pc_and_sp', locals: { insn: insn } -%>
 
-% # JIT: Invalidate call cache if it requires vm_search_method. This allows to inline some of following things.
-            fprintf(f, "    if (UNLIKELY(GET_GLOBAL_METHOD_STATE() != %"PRI_SERIALT_PREFIX"u ||\n", cc->method_state);
-            fprintf(f, "        RCLASS_SERIAL(CLASS_OF(stack[%d])) != %"PRI_SERIALT_PREFIX"u)) {\n", b->stack_size - 1 - argc, cc->class_serial);
-            fprintf(f, "        reg_cfp->pc = original_body_iseq + %d;\n", pos);
-            fprintf(f, "        goto cancel;\n");
-            fprintf(f, "    }\n");
-
 % # JIT: Print insn body in insns.def
             fprintf(f, "    {\n");
             fprintf(f, "        struct rb_calling_info calling;\n");
@@ -88,5 +84,19 @@ https://github.com/ruby/ruby/blob/trunk/tool/ruby_vm/views/_mjit_compile_send.erb#L84
             fprintf(f, "}\n");
             break;
         }
+% if insn.name == 'opt_send_without_block'
+        else if (cc->me->def->type == VM_METHOD_TYPE_IVAR) {
+% # JIT: Invalidate call cache if it requires vm_search_method. This allows to inline some of following things.
+<%= render 'mjit_compile_send_guard' -%>
+
+% # JIT: vm_call_ivar without sp motion
+            fprintf(f, "    stack[%d] = vm_getivar(stack[%d], (ID)0x%"PRIxVALUE", NULL, (CALL_CACHE)0x%"PRIxVALUE", 1);\n",
+                    b->stack_size - argc - 1, b->stack_size - argc - 1, cc->me->def->body.attr.id, (VALUE)cc);
+
+% # compiler: Move JIT compiler's internal stack pointer
+            b->stack_size += <%= insn.call_attribute('sp_inc') %>;
+            break;
+        }
+% end
     }
 }
Index: test/ruby/test_jit.rb
===================================================================
--- test/ruby/test_jit.rb	(revision 63211)
+++ test/ruby/test_jit.rb	(revision 63212)
@@ -529,6 +529,41 @@ class TestJIT < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_jit.rb#L529
     end;
   end
 
+  def test_attr_reader
+    assert_eval_with_jit("#{<<~"begin;"}\n#{<<~"end;"}", stdout: "4nil\nnil\n6", success_count: 2, min_calls: 2)
+    begin;
+      class A
+        attr_reader :a, :b
+
+        def initialize
+          @a = 2
+        end
+
+        def test
+          a
+        end
+
+        def undefined
+          b
+        end
+      end
+
+      a = A.new
+      print(a.test * a.test)
+      p(a.undefined)
+      p(a.undefined)
+
+      # redefinition
+      class A
+        def test
+          3
+        end
+      end
+
+      print(2 * a.test)
+    end;
+  end
+
   private
 
   # The shortest way to test one proc
Index: common.mk
===================================================================
--- common.mk	(revision 63211)
+++ common.mk	(revision 63212)
@@ -924,6 +924,7 @@ $(srcs_vpath)vmtc.inc: $(srcdir)/tool/ru https://github.com/ruby/ruby/blob/trunk/common.mk#L924
 $(srcs_vpath)vm.inc: $(srcdir)/tool/ruby_vm/views/vm.inc.erb
 $(srcs_vpath)mjit_compile.inc: $(srcdir)/tool/ruby_vm/views/mjit_compile.inc.erb \
   $(srcdir)/tool/ruby_vm/views/_mjit_compile_insn.erb $(srcdir)/tool/ruby_vm/views/_mjit_compile_send.erb \
+  $(srcdir)/tool/ruby_vm/views/_mjit_compile_send_guard.erb \
   $(srcdir)/tool/ruby_vm/views/_mjit_compile_insn_body.erb $(srcdir)/tool/ruby_vm/views/_mjit_compile_pc_and_sp.erb
 
 common-srcs: $(srcs_vpath)parse.c $(srcs_vpath)lex.c $(srcs_vpath)enc/trans/newline.c $(srcs_vpath)id.c \

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]