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

ruby-changes:44952

From: nobu <ko1@a...>
Date: Thu, 8 Dec 2016 16:55:02 +0900 (JST)
Subject: [ruby-changes:44952] nobu:r57025 (trunk): compile.c: optimize literal nodes

nobu	2016-12-08 16:54:58 +0900 (Thu, 08 Dec 2016)

  New Revision: 57025

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

  Log:
    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.

  Modified files:
    trunk/compile.c
Index: compile.c
===================================================================
--- compile.c	(revision 57024)
+++ compile.c	(revision 57025)
@@ -2962,6 +2962,29 @@ enum compile_array_type_t { https://github.com/ruby/ruby/blob/trunk/compile.c#L2962
 };
 
 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
 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)
 {
@@ -2985,6 +3008,7 @@ compile_array_(rb_iseq_t *iseq, LINK_ANC https://github.com/ruby/ruby/blob/trunk/compile.c#L3008
 	while (node) {
 	    NODE *start_node = node, *end_node;
 	    NODE *kw = 0;
+	    VALUE elem[2];
 	    const int max = 0x100;
 	    DECL_ANCHOR(anchor);
 	    INIT_ANCHOR(anchor);
@@ -3004,7 +3028,7 @@ compile_array_(rb_iseq_t *iseq, LINK_ANC https://github.com/ruby/ruby/blob/trunk/compile.c#L3028
 		    }
 		    break;
 		}
-		if (opt_p && nd_type(node->nd_head) != NODE_LIT) {
+		if (opt_p && !static_literal_node_p(node, elem)) {
 		    opt_p = 0;
 		}
 
@@ -3024,15 +3048,15 @@ compile_array_(rb_iseq_t *iseq, LINK_ANC https://github.com/ruby/ruby/blob/trunk/compile.c#L3048
 		    node = start_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;
 		    }
-		    while (node && nd_type(node->nd_head) == NODE_LIT &&
-			   node->nd_next && nd_type(node->nd_next->nd_head) == NODE_LIT) {
-			rb_ary_push(ary, node->nd_head->nd_lit);
-			node = node->nd_next;
-			rb_ary_push(ary, node->nd_head->nd_lit);
-			node = node->nd_next;
+		    while (node && node->nd_next &&
+			   static_literal_node_p(node, &elem[0]) &&
+			   static_literal_node_p(node->nd_next, &elem[1])) {
+			rb_ary_cat(ary, elem, 2);
+			node = node->nd_next->nd_next;
 			len++;
 		    }
 

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

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