[前][次][番号順一覧][スレッド一覧]

ruby-changes:37149

From: nobu <ko1@a...>
Date: Tue, 13 Jan 2015 12:51:39 +0900 (JST)
Subject: [ruby-changes:37149] nobu:r49230 (trunk): range.c: trivial optimizations

nobu	2015-01-13 12:51:35 +0900 (Tue, 13 Jan 2015)

  New Revision: 49230

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=49230

  Log:
    range.c: trivial optimizations
    
    * range.c (range_bsearch): trivial optimizations, for Fixnum, and
      by keeping the last satisfied element.

  Modified files:
    trunk/range.c
Index: range.c
===================================================================
--- range.c	(revision 49229)
+++ range.c	(revision 49230)
@@ -570,8 +570,8 @@ is_integer_p(VALUE v) https://github.com/ruby/ruby/blob/trunk/range.c#L570
 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) https://github.com/ruby/ruby/blob/trunk/range.c#L592
 	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) https://github.com/ruby/ruby/blob/trunk/range.c#L634
 	    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) https://github.com/ruby/ruby/blob/trunk/range.c#L677
 	    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));

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]