summaryrefslogtreecommitdiff
path: root/array.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-01-12 01:25:26 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-01-12 01:25:26 +0000
commite19cf850df3935320294e3438f39ee7848a50d12 (patch)
treed58a3ac3d72c24eb1a03f5591ef1a4f9ef7858f8 /array.c
parent4d69e03b667df09ecf1add84e6e148d26e289b80 (diff)
array.c: trivial optimizations
* array.c (rb_ary_bsearch): trivial optimizations, for Fixnum, and by keeping the last satisfied element. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@49216 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'array.c')
-rw-r--r--array.c16
1 files changed, 7 insertions, 9 deletions
diff --git a/array.c b/array.c
index 27fa0d3..55a6ba9 100644
--- a/array.c
+++ b/array.c
@@ -2593,8 +2593,8 @@ static VALUE
rb_ary_bsearch(VALUE ary)
{
long low = 0, high = RARRAY_LEN(ary), mid;
- int smaller = 0, satisfied = 0;
- VALUE v, val;
+ int smaller = 0;
+ VALUE v, val, satisfied = Qnil;
RETURN_ENUMERATOR(ary, 0, 0);
while (low < high) {
@@ -2602,11 +2602,11 @@ rb_ary_bsearch(VALUE ary)
val = rb_ary_entry(ary, mid);
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; /* Fixnum preserves its sign-bit */
}
else if (v == Qtrue) {
- satisfied = 1;
+ satisfied = val;
smaller = 1;
}
else if (v == Qfalse || v == Qnil) {
@@ -2614,7 +2614,7 @@ rb_ary_bsearch(VALUE ary)
}
else if (rb_obj_is_kind_of(v, rb_cNumeric)) {
const VALUE zero = INT2FIX(0);
- switch (rb_cmpint(rb_funcallv(v, id_cmp, 1, &zero), v, INT2FIX(0))) {
+ switch (rb_cmpint(rb_funcallv(v, id_cmp, 1, &zero), v, zero)) {
case 0: return val;
case 1: smaller = 1; break;
case -1: smaller = 0;
@@ -2632,9 +2632,7 @@ rb_ary_bsearch(VALUE ary)
low = mid + 1;
}
}
- if (low == RARRAY_LEN(ary)) return Qnil;
- if (!satisfied) return Qnil;
- return rb_ary_entry(ary, low);
+ return satisfied;
}