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

ruby-changes:62056

From: =E5=8D=9C=E9=83=A8=E6=98=8C=E5=B9=B3 <ko1@a...>
Date: Mon, 29 Jun 2020 11:08:28 +0900 (JST)
Subject: [ruby-changes:62056] d7eec15f8e (master): rb_rational_cmp: do not goto into a branch

https://git.ruby-lang.org/ruby.git/commit/?id=d7eec15f8e

From d7eec15f8e7e8667cdca144a1e288a3f72015d30 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=8D=9C=E9=83=A8=E6=98=8C=E5=B9=B3?=
 <shyouhei@r...>
Date: Tue, 16 Jun 2020 14:42:24 +0900
Subject: rb_rational_cmp: do not goto into a branch

I'm not necessarily against every goto in general, but jumping into a
branch is definitely a bad idea.  Better refactor.

diff --git a/rational.c b/rational.c
index fcad116..f50f20d 100644
--- a/rational.c
+++ b/rational.c
@@ -1085,21 +1085,19 @@ rb_rational_pow(VALUE self, VALUE other) https://github.com/ruby/ruby/blob/trunk/rational.c#L1085
 VALUE
 rb_rational_cmp(VALUE self, VALUE other)
 {
-    if (RB_INTEGER_TYPE_P(other)) {
+    switch (TYPE(other)) {
+      case T_FIXNUM:
+      case T_BIGNUM:
 	{
 	    get_dat1(self);
 
 	    if (dat->den == LONG2FIX(1))
 		return rb_int_cmp(dat->num, other); /* c14n */
 	    other = f_rational_new_bang1(CLASS_OF(self), other);
-	    goto other_is_rational;
+            /* FALLTHROUGH */
 	}
-    }
-    else if (RB_FLOAT_TYPE_P(other)) {
-	return rb_dbl_cmp(nurat_to_double(self), RFLOAT_VALUE(other));
-    }
-    else if (RB_TYPE_P(other, T_RATIONAL)) {
-	other_is_rational:
+
+      case T_RATIONAL:
 	{
 	    VALUE num1, num2;
 
@@ -1116,8 +1114,11 @@ rb_rational_cmp(VALUE self, VALUE other) https://github.com/ruby/ruby/blob/trunk/rational.c#L1114
 	    }
 	    return rb_int_cmp(rb_int_minus(num1, num2), ZERO);
 	}
-    }
-    else {
+
+      case T_FLOAT:
+        return rb_dbl_cmp(nurat_to_double(self), RFLOAT_VALUE(other));
+
+      default:
 	return rb_num_coerce_cmp(self, other, rb_intern("<=>"));
     }
 }
-- 
cgit v0.10.2


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

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