1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* 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.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@29203 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
wanabe 2010-09-09 13:40:14 +00:00
parent e54c30c05e
commit 96688ecbab
4 changed files with 55 additions and 8 deletions

View file

@ -1,3 +1,13 @@
Thu Sep 9 22:34:48 2010 wanabe <s.wanabe@gmail.com>
* 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@ruby-lang.org>
* test/net/http/test_https.rb (test_identity_verify_failure): follows

View file

@ -2303,6 +2303,11 @@ case_when_optimizable_literal(NODE * node)
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;
}

View file

@ -1260,16 +1260,28 @@ opt_case_dispatch
(..., 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 @@ opt_case_dispatch
else {
JUMP(else_offset);
}
}
}
}

View file

@ -84,4 +84,23 @@ class TestCase < Test::Unit::TestCase
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