summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorwanabe <wanabe@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-09-09 13:40:14 +0000
committerwanabe <wanabe@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-09-09 13:40:14 +0000
commit96688ecbab2e18be02af7172bcb3732e57d9290e (patch)
treea4f5761850518bd0a89e98d4e0fb681906f8cff9
parente54c30c05eb16897bc1d44b3bec06083ef1ae779 (diff)
* 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
-rw-r--r--ChangeLog10
-rw-r--r--compile.c5
-rw-r--r--insns.def29
-rw-r--r--test/ruby/test_case.rb19
4 files changed, 55 insertions, 8 deletions
diff --git a/ChangeLog b/ChangeLog
index 481f44d2e5..85f860766e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -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
diff --git a/compile.c b/compile.c
index 2fd804cac4..8cbf253cf2 100644
--- a/compile.c
+++ b/compile.c
@@ -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;
}
diff --git a/insns.def b/insns.def
index fcd97ae613..6db675214a 100644
--- a/insns.def
+++ b/insns.def
@@ -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);
}
+ }
}
}
diff --git a/test/ruby/test_case.rb b/test/ruby/test_case.rb
index 98498dada6..0067b74e2b 100644
--- a/test/ruby/test_case.rb
+++ b/test/ruby/test_case.rb
@@ -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