ruby-changes:17203
From: wanabe <ko1@a...>
Date: Thu, 9 Sep 2010 22:40:24 +0900 (JST)
Subject: [ruby-changes:17203] Ruby:r29203 (trunk): * compile.c (case_when_optimizable_literal): When float value can be
wanabe 2010-09-09 22:40:14 +0900 (Thu, 09 Sep 2010) New Revision: 29203 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=29203 Log: * compile.c (case_when_optimizable_literal): When float value can be treated as integer, add to table hash of case that way. based on a patch from Ikuo KOBORI. [ruby-dev:42038] * insnf.def (opt_case_dispatch): ditto. * test/ruby/test_case.rb: add tests. Modified files: trunk/ChangeLog trunk/compile.c trunk/insns.def trunk/test/ruby/test_case.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 29202) +++ ChangeLog (revision 29203) @@ -1,3 +1,13 @@ +Thu Sep 9 22:34:48 2010 wanabe <s.wanabe@g...> + + * compile.c (case_when_optimizable_literal): When float value can be + treated as integer, add to table hash of case that way. + based on a patch from Ikuo KOBORI. [ruby-dev:42038] + + * insnf.def (opt_case_dispatch): ditto. + + * test/ruby/test_case.rb: add tests. + Thu Sep 9 17:15:15 2010 NAKAMURA, Hiroshi <nahi@r...> * test/net/http/test_https.rb (test_identity_verify_failure): follows Index: insns.def =================================================================== --- insns.def (revision 29202) +++ insns.def (revision 29203) @@ -1260,16 +1260,28 @@ (..., VALUE key) () // inc += -1; { - if (BASIC_OP_UNREDEFINED_P(BOP_EQQ)) { - VALUE val; - if (st_lookup(RHASH_TBL(hash), key, &val)) { - JUMP(FIX2INT(val)); + switch(TYPE(key)) { + case T_FLOAT: { + double ival; + if (modf(RFLOAT_VALUE(key), &ival) == 0.0) { + key = FIXABLE(ival) ? LONG2FIX((long)ival) : rb_dbl2big(ival); } - else { - JUMP(else_offset); + } + case T_SYMBOL: /* fall through */ + case T_FIXNUM: + case T_BIGNUM: + case T_STRING: + if (BASIC_OP_UNREDEFINED_P(BOP_EQQ)) { + VALUE val; + if (st_lookup(RHASH_TBL(hash), key, &val)) { + JUMP(FIX2INT(val)); + } + else { + JUMP(else_offset); + } + break; } - } - else { + default: { /* fall through (else) */ struct opt_case_dispatch_i_arg arg; arg.obj = key; @@ -1282,6 +1294,7 @@ else { JUMP(else_offset); } + } } } Index: compile.c =================================================================== --- compile.c (revision 29202) +++ compile.c (revision 29203) @@ -2303,6 +2303,11 @@ switch (nd_type(node)) { case NODE_LIT: { VALUE v = node->nd_lit; + double ival; + if (TYPE(v) == T_FLOAT && + modf(RFLOAT_VALUE(v), &ival) == 0.0) { + return FIXABLE(ival) ? LONG2FIX((long)ival) : rb_dbl2big(ival); + } if (SYMBOL_P(v) || rb_obj_is_kind_of(v, rb_cNumeric)) { return v; } Index: test/ruby/test_case.rb =================================================================== --- test/ruby/test_case.rb (revision 29202) +++ test/ruby/test_case.rb (revision 29203) @@ -84,4 +84,23 @@ class Fixnum; undef ===; def ===(o); p 42; true; end; end; case 1; when 1; end EOS end + + def test_optimization + case 1 + when 0.9, 1.1 + assert(false) + when 1.0 + assert(true) + else + assert(false) + end + case 536870912 + when 536870911.9, 536870912.1 + assert(false) + when 536870912.0 + assert(true) + else + assert(false) + end + end end -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/