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

ruby-changes:27487

From: ko1 <ko1@a...>
Date: Thu, 28 Feb 2013 23:05:53 +0900 (JST)
Subject: [ruby-changes:27487] ko1:r39539 (trunk): * compile.c (iseq_compile_each): remove redundant trace(line)

ko1	2013-02-28 23:04:53 +0900 (Thu, 28 Feb 2013)

  New Revision: 39539

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=39539

  Log:
    * compile.c (iseq_compile_each): remove redundant trace(line)
      instruction. for example, at the following script
      def m()
      p:xyzzy
      1
      2
      end
      compiler ignores `1' because there is no effect. However,
      `trace(line)' instruction remains in bytecode.
      This modification removes such redundant trace(line) instruction.
    * test/ruby/test_iseq.rb: add a test.

  Modified files:
    trunk/ChangeLog
    trunk/compile.c
    trunk/test/ruby/test_iseq.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 39538)
+++ ChangeLog	(revision 39539)
@@ -1,3 +1,18 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Thu Feb 28 22:57:48 2013  Koichi Sasada  <ko1@a...>
+
+	* compile.c (iseq_compile_each): remove redundant trace(line)
+	  instruction. for example, at the following script
+	    def m()
+	      p:xyzzy
+	      1
+	      2
+	    end
+	  compiler ignores `1' because there is no effect. However,
+	  `trace(line)' instruction remains in bytecode.
+	  This modification removes such redundant trace(line) instruction.
+
+	* test/ruby/test_iseq.rb: add a test.
+
 Thu Feb 28 22:23:27 2013  Tanaka Akira  <akr@f...>
 
 	* ext/socket/raddrinfo.c (inspect_sockaddr): don't show that Unix
Index: compile.c
===================================================================
--- compile.c	(revision 39538)
+++ compile.c	(revision 39539)
@@ -3154,6 +3154,7 @@ static int https://github.com/ruby/ruby/blob/trunk/compile.c#L3154
 iseq_compile_each(rb_iseq_t *iseq, LINK_ANCHOR *ret, NODE * node, int poped)
 {
     enum node_type type;
+    LINK_ELEMENT *saved_last_element = 0;
 
     if (node == 0) {
 	if (!poped) {
@@ -3170,6 +3171,7 @@ iseq_compile_each(rb_iseq_t *iseq, LINK_ https://github.com/ruby/ruby/blob/trunk/compile.c#L3171
 
     if (node->flags & NODE_FL_NEWLINE) {
 	ADD_TRACE(ret, nd_line(node), RUBY_EVENT_LINE);
+	saved_last_element = ret->last;
     }
 
     switch (type) {
@@ -5295,6 +5297,13 @@ iseq_compile_each(rb_iseq_t *iseq, LINK_ https://github.com/ruby/ruby/blob/trunk/compile.c#L5297
 	return COMPILE_NG;
     }
 
+    /* check & remove redundant trace(line) */
+    if (saved_last_element && ret /* ret can be 0 when error */ &&
+	ret->last == saved_last_element &&
+	((INSN *)saved_last_element)->insn_id == BIN(trace)) {
+	POP_ELEMENT(ret);
+    }
+
     debug_node_end();
     return COMPILE_OK;
 }
Index: test/ruby/test_iseq.rb
===================================================================
--- test/ruby/test_iseq.rb	(revision 39538)
+++ test/ruby/test_iseq.rb	(revision 39539)
@@ -9,6 +9,11 @@ class TestISeq < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_iseq.rb#L9
     assert_normal_exit('p RubyVM::InstructionSequence.compile("1", "mac", "", 0).to_a', bug5894)
   end
 
+  def lines src
+    body = RubyVM::InstructionSequence.new(src).to_a[13]
+    lines = body.find_all{|e| e.kind_of? Fixnum}
+  end
+
   def test_to_a_lines
     src = <<-EOS
     p __LINE__ # 1
@@ -16,9 +21,29 @@ class TestISeq < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_iseq.rb#L21
                # 3
     p __LINE__ # 4
     EOS
-    body = RubyVM::InstructionSequence.new(src).to_a[13]
-    lines = body.find_all{|e| e.kind_of? Fixnum}
-    assert_equal [1, 2, 4], lines
+    assert_equal [1, 2, 4], lines(src)
+
+    src = <<-EOS
+               # 1
+    p __LINE__ # 2
+               # 3
+    p __LINE__ # 4
+               # 5
+    EOS
+    assert_equal [2, 4], lines(src)
+
+    src = <<-EOS
+    1 # should be optimized out
+    2 # should be optimized out
+    p __LINE__ # 3
+    p __LINE__ # 4
+    5 # should be optimized out
+    6 # should be optimized out
+    p __LINE__ # 7
+    8 # should be optimized out
+    9
+    EOS
+    assert_equal [3, 4, 7, 9], lines(src)
   end
 
   def test_unsupport_type

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

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