From a86fd7b1385b87525b89d286dde4963d23930202 Mon Sep 17 00:00:00 2001
From: ko1 <ko1@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>
Date: Sun, 7 Jan 2018 19:18:49 +0000
Subject: Speedup `block.call` [Feature #14330]

* insns.def (getblockparamproxy): introduce new instruction to return
  the `rb_block_param_proxy` object if possible. This object responds
  to `call` method and invoke given block (completely similar to `yield`).

* method.h (OPTIMIZED_METHOD_TYPE_BLOCK_CALL): add new optimized call type
  which is for `rb_block_param_proxy.cal`.

* vm_insnhelper.c (vm_call_method_each_type): ditto.

* vm_insnhelper.c (vm_call_opt_block_call): ditto.

* vm_core.h (BOP_CALL, PROC_REDEFINED_OP_FLAG): add check for `Proc#call`
  redefinition.

* compile.c (iseq_compile_each0): compile to use new insn
  `getblockparamproxy` for method call.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61659 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
---
 insns.def | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)

(limited to 'insns.def')

diff --git a/insns.def b/insns.def
index bab9d14488..f9375b7c0a 100644
--- a/insns.def
+++ b/insns.def
@@ -127,6 +127,54 @@ setblockparam
     VM_ENV_FLAGS_SET(ep, VM_FRAME_FLAG_MODIFIED_BLOCK_PARAM);
 }
 
+/**
+  @c variable
+  @e Get special proxy object which only responds to `call` method if the block parameter
+     represents a iseq/ifunc block. Otherwise, same as `getblockparam`.
+  @j ブロックパラメータが iseq/ifunc ブロックであれば、特殊なプロキシオブジェクトを取得する。
+ */
+DEFINE_INSN
+getblockparamproxy
+(lindex_t idx, rb_num_t level)
+()
+(VALUE val)
+{
+    const VALUE *ep = vm_get_ep(GET_EP(), level);
+    VM_ASSERT(VM_ENV_LOCAL_P(ep));
+
+    if (!VM_ENV_FLAGS(ep, VM_FRAME_FLAG_MODIFIED_BLOCK_PARAM)) {
+	VALUE block_handler = VM_ENV_BLOCK_HANDLER(ep);
+
+	if (block_handler) {
+	    switch (vm_block_handler_type(block_handler)) {
+	      case block_handler_type_iseq:
+	      case block_handler_type_ifunc:
+		val = rb_block_param_proxy;
+		break;
+	      case block_handler_type_symbol:
+		val = rb_sym_to_proc(VM_BH_TO_SYMBOL(block_handler));
+		goto INSN_LABEL(set);
+	      case block_handler_type_proc:
+		val = VM_BH_TO_PROC(block_handler);
+		goto INSN_LABEL(set);
+	      default:
+		VM_UNREACHABLE(getblockparamproxy);
+	    }
+	}
+	else {
+	    val = Qnil;
+	  INSN_LABEL(set):
+	    vm_env_write(ep, -(int)idx, val);
+	    VM_ENV_FLAGS_SET(ep, VM_FRAME_FLAG_MODIFIED_BLOCK_PARAM);
+	}
+    }
+    else {
+	val = *(ep - idx);
+	RB_DEBUG_COUNTER_INC(lvar_get);
+	(void)RB_DEBUG_COUNTER_INC_IF(lvar_get_dynamic, level > 0);
+    }
+}
+
 /**
   @c variable
   @e Get value of special local variable ($~, $_, ..).
-- 
cgit v1.2.3

