compile.c: optimize method chain

* compile.c (iseq_peephole_optimize): optimize lengthy safe
  navigation method chain.  [Feature #11537]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52237 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2015-10-23 02:58:22 +00:00
Родитель 4f9ded5cdd
Коммит ae8f8fddb0
3 изменённых файлов: 49 добавлений и 30 удалений

Просмотреть файл

@ -1,3 +1,8 @@
Fri Oct 23 11:58:21 2015 Nobuyoshi Nakada <nobu@ruby-lang.org>
* compile.c (iseq_peephole_optimize): optimize lengthy safe
navigation method chain. [Feature #11537]
Fri Oct 23 10:58:41 2015 Shugo Maeda <shugo@ruby-lang.org>
* lib/matrix/eigenvalue_decomposition.rb (tridiagonalize): fix

Просмотреть файл

@ -1967,33 +1967,36 @@ iseq_peephole_optimize(rb_iseq_t *iseq, LINK_ELEMENT *list, const int do_tailcal
* if L2
*/
INSN *nobj = (INSN *)get_destination_insn(iobj);
if (nobj->insn_id == BIN(jump)) {
OPERAND_AT(iobj, 0) = OPERAND_AT(nobj, 0);
}
INSN *pobj = (INSN *)iobj->link.prev;
int prev_dup = (pobj && pobj->insn_id == BIN(dup));
if (nobj->insn_id == BIN(dup)) {
/*
* dup
* if L1
* ...
* L1:
* dup
* if L2
* =>
* dup
* if L2
* ...
* L1:
* dup
* if L2
*/
INSN *pobj = (INSN *)iobj->link.prev;
nobj = (INSN *)nobj->link.next;
/* basic blocks, with no labels in the middle */
if ((pobj && pobj->insn_id == BIN(dup)) &&
(nobj && nobj->insn_id == iobj->insn_id)) {
for (;;) {
if (nobj->insn_id == BIN(jump)) {
OPERAND_AT(iobj, 0) = OPERAND_AT(nobj, 0);
}
else if (prev_dup && nobj->insn_id == BIN(dup) &&
!!(nobj = (INSN *)nobj->link.next) &&
/* basic blocks, with no labels in the middle */
nobj->insn_id == iobj->insn_id) {
/*
* dup
* if L1
* ...
* L1:
* dup
* if L2
* =>
* dup
* if L2
* ...
* L1:
* dup
* if L2
*/
OPERAND_AT(iobj, 0) = OPERAND_AT(nobj, 0);
}
else break;
nobj = (INSN *)get_destination_insn(nobj);
}
}

Просмотреть файл

@ -8,8 +8,12 @@ class TestISeq < Test::Unit::TestCase
assert_normal_exit('p RubyVM::InstructionSequence.compile("1", "mac", "", 0).to_a', bug5894)
end
def compile(src, line = nil, opt = nil)
RubyVM::InstructionSequence.new(src, __FILE__, __FILE__, line, opt)
end
def lines src
body = RubyVM::InstructionSequence.new(src).to_a[13]
body = compile(src).to_a[13]
body.find_all{|e| e.kind_of? Fixnum}
end
@ -52,7 +56,7 @@ class TestISeq < Test::Unit::TestCase
end if defined?(RubyVM::InstructionSequence.load)
def test_loaded_cdhash_mark
iseq = RubyVM::InstructionSequence.compile(<<-'end;', __FILE__, __FILE__, __LINE__+1)
iseq = compile(<<-'end;', __LINE__+1)
def bug(kw)
case kw
when "false" then false
@ -73,11 +77,11 @@ class TestISeq < Test::Unit::TestCase
def test_disasm_encoding
src = "\u{3042} = 1; \u{3042}; \u{3043}"
asm = RubyVM::InstructionSequence.compile(src).disasm
asm = compile(src).disasm
assert_equal(src.encoding, asm.encoding)
assert_predicate(asm, :valid_encoding?)
src.encode!(Encoding::Shift_JIS)
asm = RubyVM::InstructionSequence.compile(src).disasm
asm = compile(src).disasm
assert_equal(src.encoding, asm.encoding)
assert_predicate(asm, :valid_encoding?)
end
@ -147,7 +151,7 @@ class TestISeq < Test::Unit::TestCase
def test_disable_opt
src = "a['foo'] = a['bar']; 'a'.freeze"
_,_,_,_,_,_,_,_,_,_,_,_,_,body= RubyVM::InstructionSequence.compile(src, __FILE__, __FILE__, __LINE__, false).to_a
body= compile(src, __LINE__, false).to_a[13]
body.each{|insn|
next unless Array === insn
op = insn.first
@ -168,11 +172,18 @@ class TestISeq < Test::Unit::TestCase
code = <<-'EOS'
['foo', 'foo', "#{$f}foo", "#{'foo'}"]
EOS
s1, s2, s3, s4 = RubyVM::InstructionSequence.compile(code, __FILE__, __FILE__, line, {frozen_string_literal: true}).eval
s1, s2, s3, s4 = compile(code, line, {frozen_string_literal: true}).eval
assert_predicate(s1, :frozen?)
assert_predicate(s2, :frozen?)
assert_not_predicate(s3, :frozen?)
assert_predicate(s4, :frozen?)
assert_same(s1, s2)
end
def test_safe_call_chain
src = "a.?a.?a.?a.?a.?a"
body = compile(src, __LINE__, {peephole_optimization: true}).to_a[13]
labels = body.select {|op, arg| op == :branchnil}.map {|op, arg| arg}
assert_equal(1, labels.uniq.size)
end
end