summaryrefslogtreecommitdiff
path: root/range.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-01-13 03:51:35 (GMT)
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-01-13 03:51:35 (GMT)
commitb13432890fba7b2aac294710d67d1f96e2d7eabe (patch)
tree6d1d8d891d10db1999b44d0d0c677ff97cb7c0ff /range.c
parent75a43dbe8d287304244f708b60553d7fc80ba789 (diff)
range.c: trivial optimizations
* range.c (range_bsearch): trivial optimizations, for Fixnum, and by keeping the last satisfied element. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@49230 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'range.c')
-rw-r--r--range.c16
1 files changed, 7 insertions, 9 deletions
diff --git a/range.c b/range.c
index 9211765..232e36c 100644
--- a/range.c
+++ b/range.c
@@ -570,8 +570,8 @@ is_integer_p(VALUE v)
static VALUE
range_bsearch(VALUE range)
{
- VALUE beg, end;
- int smaller, satisfied = 0;
+ VALUE beg, end, satisfied = Qnil;
+ int smaller;
/* Implementation notes:
* Floats are handled by mapping them to 64 bits integers.
@@ -592,11 +592,11 @@ range_bsearch(VALUE range)
VALUE val = (expr); \
VALUE v = rb_yield(val); \
if (FIXNUM_P(v)) { \
- if (FIX2INT(v) == 0) return val; \
- smaller = FIX2INT(v) < 0; \
+ if (v == INT2FIX(0)) return val; \
+ smaller = (SIGNED_VALUE)v < 0; \
} \
else if (v == Qtrue) { \
- satisfied = 1; \
+ satisfied = val; \
smaller = 1; \
} \
else if (v == Qfalse || v == Qnil) { \
@@ -634,8 +634,7 @@ range_bsearch(VALUE range)
BSEARCH_CHECK(conv(low)); \
if (!smaller) return Qnil; \
} \
- if (!satisfied) return Qnil; \
- return conv(low); \
+ return satisfied; \
} while (0)
@@ -678,8 +677,7 @@ range_bsearch(VALUE range)
BSEARCH_CHECK(low);
if (!smaller) return Qnil;
}
- if (!satisfied) return Qnil;
- return low;
+ return satisfied;
}
else {
rb_raise(rb_eTypeError, "can't do binary search for %s", rb_obj_classname(beg));