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

ruby-changes:44628

From: nobu <ko1@a...>
Date: Fri, 11 Nov 2016 16:08:58 +0900 (JST)
Subject: [ruby-changes:44628] nobu:r56701 (trunk): rational.c: optimize

nobu	2016-11-11 16:08:53 +0900 (Fri, 11 Nov 2016)

  New Revision: 56701

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

  Log:
    rational.c: optimize
    
    * rational.c (f_{lt,eqeq,zero,one,minus_one,kind_of}_p): add RTEST
      to results of funcalls.
    
    * rational.c (f_negative_p, k_{integer,float,rational}_p):
      optimize.

  Modified files:
    trunk/rational.c
Index: rational.c
===================================================================
--- rational.c	(revision 56700)
+++ rational.c	(revision 56701)
@@ -90,12 +90,12 @@ f_div(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/rational.c#L90
     return rb_funcall(x, '/', 1, y);
 }
 
-inline static VALUE
+inline static int
 f_lt_p(VALUE x, VALUE y)
 {
     if (FIXNUM_P(x) && FIXNUM_P(y))
-	return f_boolcast(FIX2LONG(x) < FIX2LONG(y));
-    return rb_funcall(x, '<', 1, y);
+	return (SIGNED_VALUE)x < (SIGNED_VALUE)y;
+    return RTEST(rb_funcall(x, '<', 1, y));
 }
 
 binop(mod, '%')
@@ -155,8 +155,8 @@ inline static VALUE https://github.com/ruby/ruby/blob/trunk/rational.c#L155
 f_eqeq_p(VALUE x, VALUE y)
 {
     if (FIXNUM_P(x) && FIXNUM_P(y))
-	return f_boolcast(FIX2LONG(x) == FIX2LONG(y));
-    return rb_funcall(x, id_eqeq_p, 1, y);
+	return x == y;
+    return RTEST(rb_funcall(x, id_eqeq_p, 1, y));
 }
 
 fun2(expt)
@@ -165,59 +165,50 @@ fun2(idiv) https://github.com/ruby/ruby/blob/trunk/rational.c#L165
 
 #define f_expt10(x) f_expt(INT2FIX(10), x)
 
-inline static VALUE
+inline static int
 f_negative_p(VALUE x)
 {
-    if (FIXNUM_P(x))
-	return f_boolcast(FIX2LONG(x) < 0);
-    return rb_funcall(x, '<', 1, ZERO);
+    return rb_num_negative_p(x);
 }
 
 #define f_positive_p(x) (!f_negative_p(x))
 
-inline static VALUE
+inline static int
 f_zero_p(VALUE x)
 {
-    if (RB_TYPE_P(x, T_FIXNUM)) {
-	return f_boolcast(FIX2LONG(x) == 0);
-    }
-    else if (RB_TYPE_P(x, T_BIGNUM)) {
-	return Qfalse;
+    if (RB_INTEGER_TYPE_P(x)) {
+	return x == LONG2FIX(0);
     }
     else if (RB_TYPE_P(x, T_RATIONAL)) {
 	VALUE num = RRATIONAL(x)->num;
 
-	return f_boolcast(FIXNUM_P(num) && FIX2LONG(num) == 0);
+	return num == LONG2FIX(0);
     }
-    return rb_funcall(x, id_eqeq_p, 1, ZERO);
+    return RTEST(rb_funcall(x, id_eqeq_p, 1, ZERO));
 }
 
 #define f_nonzero_p(x) (!f_zero_p(x))
 
-inline static VALUE
+inline static int
 f_one_p(VALUE x)
 {
-    if (RB_TYPE_P(x, T_FIXNUM)) {
-	return f_boolcast(FIX2LONG(x) == 1);
-    }
-    else if (RB_TYPE_P(x, T_BIGNUM)) {
-	return Qfalse;
+    if (RB_INTEGER_TYPE_P(x)) {
+	return x == LONG2FIX(1);
     }
     else if (RB_TYPE_P(x, T_RATIONAL)) {
 	VALUE num = RRATIONAL(x)->num;
 	VALUE den = RRATIONAL(x)->den;
 
-	return f_boolcast(FIXNUM_P(num) && FIX2LONG(num) == 1 &&
-			  FIXNUM_P(den) && FIX2LONG(den) == 1);
+	return num == LONG2FIX(1) && den == LONG2FIX(1);
     }
-    return rb_funcall(x, id_eqeq_p, 1, ONE);
+    return RTEST(rb_funcall(x, id_eqeq_p, 1, ONE));
 }
 
-inline static VALUE
+inline static int
 f_minus_one_p(VALUE x)
 {
-    if (RB_TYPE_P(x, T_FIXNUM)) {
-	return f_boolcast(FIX2LONG(x) == -1);
+    if (RB_INTEGER_TYPE_P(x)) {
+	return x == LONG2FIX(-1);
     }
     else if (RB_TYPE_P(x, T_BIGNUM)) {
 	return Qfalse;
@@ -226,40 +217,40 @@ f_minus_one_p(VALUE x) https://github.com/ruby/ruby/blob/trunk/rational.c#L217
 	VALUE num = RRATIONAL(x)->num;
 	VALUE den = RRATIONAL(x)->den;
 
-	return f_boolcast(FIXNUM_P(num) && FIX2LONG(num) == -1 &&
-			  FIXNUM_P(den) && FIX2LONG(den) == 1);
+	return num == LONG2FIX(-1) && den == LONG2FIX(1);
     }
-    return rb_funcall(x, id_eqeq_p, 1, INT2FIX(-1));
+    return RTEST(rb_funcall(x, id_eqeq_p, 1, INT2FIX(-1)));
 }
 
-inline static VALUE
+inline static int
 f_kind_of_p(VALUE x, VALUE c)
 {
-    return rb_obj_is_kind_of(x, c);
+    VALUE ret = rb_obj_is_kind_of(x, c);
+    return RTEST(ret);
 }
 
-inline static VALUE
+inline static int
 k_numeric_p(VALUE x)
 {
     return f_kind_of_p(x, rb_cNumeric);
 }
 
-inline static VALUE
+inline static int
 k_integer_p(VALUE x)
 {
-    return f_kind_of_p(x, rb_cInteger);
+    return RB_INTEGER_TYPE_P(x);
 }
 
-inline static VALUE
+inline static int
 k_float_p(VALUE x)
 {
-    return f_kind_of_p(x, rb_cFloat);
+    return RB_FLOAT_TYPE_P(x);
 }
 
-inline static VALUE
+inline static int
 k_rational_p(VALUE x)
 {
-    return f_kind_of_p(x, rb_cRational);
+    return RB_TYPE_P(x, T_RATIONAL);
 }
 
 #define k_exact_p(x) (!k_float_p(x))
@@ -1134,7 +1125,7 @@ nurat_eqeq_p(VALUE self, VALUE other) https://github.com/ruby/ruby/blob/trunk/rational.c#L1125
 	}
     }
     else if (RB_TYPE_P(other, T_FLOAT)) {
-	return f_eqeq_p(f_to_f(self), other);
+	return f_boolcast(f_eqeq_p(f_to_f(self), other));
     }
     else if (RB_TYPE_P(other, T_RATIONAL)) {
 	{
@@ -1148,7 +1139,7 @@ nurat_eqeq_p(VALUE self, VALUE other) https://github.com/ruby/ruby/blob/trunk/rational.c#L1139
 	}
     }
     else {
-	return f_eqeq_p(other, self);
+	return rb_funcall(other, id_eqeq_p, 1, self);
     }
 }
 

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

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