compile.c: optimize literal nodes

* compile.c (static_literal_node_p): optimize literal nodes for
  true, false, and nil, which are static literals in specific
  nodes but not in NODE_LIT.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57025 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2016-12-08 07:54:58 +00:00
parent 05f891cbbb
commit ea07884a41

View File

@ -2961,6 +2961,29 @@ enum compile_array_type_t {
COMPILE_ARRAY_TYPE_ARGS COMPILE_ARRAY_TYPE_ARGS
}; };
static int
static_literal_node_p(NODE *node, VALUE *val)
{
node = node->nd_head;
switch (nd_type(node)) {
case NODE_LIT:
*val = node->nd_lit;
break;
case NODE_NIL:
*val = Qnil;
break;
case NODE_TRUE:
*val = Qtrue;
break;
case NODE_FALSE:
*val = Qfalse;
break;
default:
return FALSE;
}
return TRUE;
}
static int static int
compile_array_(rb_iseq_t *iseq, LINK_ANCHOR *const ret, NODE* node_root, compile_array_(rb_iseq_t *iseq, LINK_ANCHOR *const ret, NODE* node_root,
enum compile_array_type_t type, struct rb_call_info_kw_arg **keywords_ptr, int popped) enum compile_array_type_t type, struct rb_call_info_kw_arg **keywords_ptr, int popped)
@ -2985,6 +3008,7 @@ compile_array_(rb_iseq_t *iseq, LINK_ANCHOR *const ret, NODE* node_root,
while (node) { while (node) {
NODE *start_node = node, *end_node; NODE *start_node = node, *end_node;
NODE *kw = 0; NODE *kw = 0;
VALUE elem[2];
const int max = 0x100; const int max = 0x100;
DECL_ANCHOR(anchor); DECL_ANCHOR(anchor);
INIT_ANCHOR(anchor); INIT_ANCHOR(anchor);
@ -3004,7 +3028,7 @@ compile_array_(rb_iseq_t *iseq, LINK_ANCHOR *const ret, NODE* node_root,
} }
break; break;
} }
if (opt_p && nd_type(node->nd_head) != NODE_LIT) { if (opt_p && !static_literal_node_p(node, elem)) {
opt_p = 0; opt_p = 0;
} }
@ -3024,15 +3048,15 @@ compile_array_(rb_iseq_t *iseq, LINK_ANCHOR *const ret, NODE* node_root,
node = start_node; node = start_node;
while (node != end_node) { while (node != end_node) {
rb_ary_push(ary, node->nd_head->nd_lit); static_literal_node_p(node, elem);
rb_ary_push(ary, elem[0]);
node = node->nd_next; node = node->nd_next;
} }
while (node && nd_type(node->nd_head) == NODE_LIT && while (node && node->nd_next &&
node->nd_next && nd_type(node->nd_next->nd_head) == NODE_LIT) { static_literal_node_p(node, &elem[0]) &&
rb_ary_push(ary, node->nd_head->nd_lit); static_literal_node_p(node->nd_next, &elem[1])) {
node = node->nd_next; rb_ary_cat(ary, elem, 2);
rb_ary_push(ary, node->nd_head->nd_lit); node = node->nd_next->nd_next;
node = node->nd_next;
len++; len++;
} }