Skip to content

Commit 96688ec

Browse files
committed
* 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
1 parent e54c30c commit 96688ec

File tree

4 files changed

+55
-8
lines changed

4 files changed

+55
-8
lines changed

ChangeLog

+10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
Thu Sep 9 22:34:48 2010 wanabe <s.wanabe@gmail.com>
2+
3+
* compile.c (case_when_optimizable_literal): When float value can be
4+
treated as integer, add to table hash of case that way.
5+
based on a patch from Ikuo KOBORI. [ruby-dev:42038]
6+
7+
* insnf.def (opt_case_dispatch): ditto.
8+
9+
* test/ruby/test_case.rb: add tests.
10+
111
Thu Sep 9 17:15:15 2010 NAKAMURA, Hiroshi <nahi@ruby-lang.org>
212

313
* test/net/http/test_https.rb (test_identity_verify_failure): follows

compile.c

+5
Original file line numberDiff line numberDiff line change
@@ -2303,6 +2303,11 @@ case_when_optimizable_literal(NODE * node)
23032303
switch (nd_type(node)) {
23042304
case NODE_LIT: {
23052305
VALUE v = node->nd_lit;
2306+
double ival;
2307+
if (TYPE(v) == T_FLOAT &&
2308+
modf(RFLOAT_VALUE(v), &ival) == 0.0) {
2309+
return FIXABLE(ival) ? LONG2FIX((long)ival) : rb_dbl2big(ival);
2310+
}
23062311
if (SYMBOL_P(v) || rb_obj_is_kind_of(v, rb_cNumeric)) {
23072312
return v;
23082313
}

insns.def

+21-8
Original file line numberDiff line numberDiff line change
@@ -1260,16 +1260,28 @@ opt_case_dispatch
12601260
(..., VALUE key)
12611261
() // inc += -1;
12621262
{
1263-
if (BASIC_OP_UNREDEFINED_P(BOP_EQQ)) {
1264-
VALUE val;
1265-
if (st_lookup(RHASH_TBL(hash), key, &val)) {
1266-
JUMP(FIX2INT(val));
1263+
switch(TYPE(key)) {
1264+
case T_FLOAT: {
1265+
double ival;
1266+
if (modf(RFLOAT_VALUE(key), &ival) == 0.0) {
1267+
key = FIXABLE(ival) ? LONG2FIX((long)ival) : rb_dbl2big(ival);
12671268
}
1268-
else {
1269-
JUMP(else_offset);
1269+
}
1270+
case T_SYMBOL: /* fall through */
1271+
case T_FIXNUM:
1272+
case T_BIGNUM:
1273+
case T_STRING:
1274+
if (BASIC_OP_UNREDEFINED_P(BOP_EQQ)) {
1275+
VALUE val;
1276+
if (st_lookup(RHASH_TBL(hash), key, &val)) {
1277+
JUMP(FIX2INT(val));
1278+
}
1279+
else {
1280+
JUMP(else_offset);
1281+
}
1282+
break;
12701283
}
1271-
}
1272-
else {
1284+
default: { /* fall through (else) */
12731285
struct opt_case_dispatch_i_arg arg;
12741286

12751287
arg.obj = key;
@@ -1282,6 +1294,7 @@ opt_case_dispatch
12821294
else {
12831295
JUMP(else_offset);
12841296
}
1297+
}
12851298
}
12861299
}
12871300

test/ruby/test_case.rb

+19
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,23 @@ class Symbol; undef ===; def ===(o); p 42; true; end; end; case :foo; when :foo;
8484
class Fixnum; undef ===; def ===(o); p 42; true; end; end; case 1; when 1; end
8585
EOS
8686
end
87+
88+
def test_optimization
89+
case 1
90+
when 0.9, 1.1
91+
assert(false)
92+
when 1.0
93+
assert(true)
94+
else
95+
assert(false)
96+
end
97+
case 536870912
98+
when 536870911.9, 536870912.1
99+
assert(false)
100+
when 536870912.0
101+
assert(true)
102+
else
103+
assert(false)
104+
end
105+
end
87106
end

0 commit comments

Comments
 (0)