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

ruby-changes:61976

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

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

From 73f98d25ebe5ec83865f3d9b7d1dbe9540f5c62b 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: Thu, 11 Jun 2020 11:31:27 +0900
Subject: ary_join_1: 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/array.c b/array.c
index e062bf7..5bc2eb7 100644
--- a/array.c
+++ b/array.c
@@ -2944,6 +2944,34 @@ ary_join_0(VALUE ary, VALUE sep, long max, VALUE result) https://github.com/ruby/ruby/blob/trunk/array.c#L2944
 }
 
 static void
+ary_join_1_str(VALUE dst, VALUE src, int *first)
+{
+    rb_str_buf_append(dst, src);
+    if (*first) {
+        rb_enc_copy(dst, src);
+        *first = FALSE;
+    }
+}
+
+static void
+ary_join_1_ary(VALUE obj, VALUE ary, VALUE sep, VALUE result, VALUE val, int *first)
+{
+    if (val == ary) {
+        rb_raise(rb_eArgError, "recursive array join");
+    }
+    else {
+        VALUE args[4];
+
+        *first = FALSE;
+        args[0] = val;
+        args[1] = sep;
+        args[2] = result;
+        args[3] = (VALUE)first;
+        rb_exec_recursive(recursive_join, obj, (VALUE)args);
+    }
+}
+
+static void
 ary_join_1(VALUE obj, VALUE ary, VALUE sep, long i, VALUE result, int *first)
 {
     VALUE val, tmp;
@@ -2954,44 +2982,19 @@ ary_join_1(VALUE obj, VALUE ary, VALUE sep, long i, VALUE result, int *first) https://github.com/ruby/ruby/blob/trunk/array.c#L2982
 
 	val = RARRAY_AREF(ary, i);
 	if (RB_TYPE_P(val, T_STRING)) {
-	  str_join:
-	    rb_str_buf_append(result, val);
-	    if (*first) {
-		rb_enc_copy(result, val);
-		*first = FALSE;
-	    }
+            ary_join_1_str(result, val, first);
 	}
 	else if (RB_TYPE_P(val, T_ARRAY)) {
-	    obj = val;
-	  ary_join:
-	    if (val == ary) {
-		rb_raise(rb_eArgError, "recursive array join");
-	    }
-	    else {
-		VALUE args[4];
-
-		*first = FALSE;
-		args[0] = val;
-		args[1] = sep;
-		args[2] = result;
-		args[3] = (VALUE)first;
-		rb_exec_recursive(recursive_join, obj, (VALUE)args);
-	    }
+            ary_join_1_ary(val, ary, sep, result, val, first);
 	}
-	else {
-	    tmp = rb_check_string_type(val);
-	    if (!NIL_P(tmp)) {
-		val = tmp;
-		goto str_join;
-	    }
-	    tmp = rb_check_array_type(val);
-	    if (!NIL_P(tmp)) {
-		obj = val;
-		val = tmp;
-		goto ary_join;
-	    }
-	    val = rb_obj_as_string(val);
-	    goto str_join;
+        else if (!NIL_P(tmp = rb_check_string_type(val))) {
+            ary_join_1_str(result, tmp, first);
+        }
+        else if (!NIL_P(tmp = rb_check_array_type(val))) {
+            ary_join_1_ary(val, ary, sep, result, tmp, first);
+        }
+        else {
+            ary_join_1_str(result, rb_obj_as_string(val), first);
 	}
     }
 }
-- 
cgit v0.10.2


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

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