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

ruby-changes:61194

From: =E5=8D=9C=E9=83=A8=E6=98=8C=E5=B9=B3 <ko1@a...>
Date: Mon, 11 May 2020 11:08:02 +0900 (JST)
Subject: [ruby-changes:61194] 534277fa7b (master): rb_str_new: hoist RB_CONSTANT_P out of function

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

From 534277fa7b3b8aa7c25db51af0c1b32e7dfa6139 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: Sun, 10 May 2020 18:47:03 +0900
Subject: rb_str_new: hoist RB_CONSTANT_P out of function

https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html says:

> GCC never returns 1 when you call the inline function with a string
> constant or ...

Because rb_str_new and familiy take string constants, it was a bad idea
for them to contain RB_CONSTANT_P.  We should instead do so in
corresponding macros. [ruby-core:98212]

diff --git a/include/ruby/internal/intern/string.h b/include/ruby/internal/intern/string.h
index e08029f..bfb5c69 100644
--- a/include/ruby/internal/intern/string.h
+++ b/include/ruby/internal/intern/string.h
@@ -139,168 +139,132 @@ rbimpl_strlen(const char *str) https://github.com/ruby/ruby/blob/trunk/include/ruby/internal/intern/string.h#L139
 }
 
 static inline VALUE
-rbimpl_str_new(const char *str, long len)
-{
-    if /* constexpr */ (! RBIMPL_CONSTANT_P(str)) {
-        return rb_str_new(str, len);
-    }
-    else if  /* constexpr */ (! RBIMPL_CONSTANT_P(len)) {
-        return rb_str_new(str, len);
-    }
-    else {
-        return rb_str_new_static(str, len);
-    }
-}
-
-static inline VALUE
 rbimpl_str_new_cstr(const char *str)
 {
-    if /* constexpr */ (! RBIMPL_CONSTANT_P(str)) {
-        return rb_str_new_cstr(str);
-    }
-    else {
-        long len = rbimpl_strlen(str);
-        return rb_str_new_static(str, len);
-    }
-}
-
-static inline VALUE
-rbimpl_usascii_str_new(const char *str, long len)
-{
-    if /* constexpr */ (! RBIMPL_CONSTANT_P(str)) {
-        return rb_usascii_str_new(str, len);
-    }
-    else if  /* constexpr */ (! RBIMPL_CONSTANT_P(len)) {
-        return rb_usascii_str_new(str, len);
-    }
-    else {
-        return rb_usascii_str_new_static(str, len);
-    }
-}
-
-static inline VALUE
-rbimpl_utf8_str_new(const char *str, long len)
-{
-    if /* constexpr */ (! RBIMPL_CONSTANT_P(str)) {
-        return rb_utf8_str_new(str, len);
-    }
-    else if  /* constexpr */ (! RBIMPL_CONSTANT_P(len)) {
-        return rb_utf8_str_new(str, len);
-    }
-    else {
-        return rb_utf8_str_new_static(str, len);
-    }
+    long len = rbimpl_strlen(str);
+    return rb_str_new_static(str, len);
 }
 
 static inline VALUE
 rbimpl_tainted_str_new_cstr(const char *str)
 {
-    if /* constexpr */ (! RBIMPL_CONSTANT_P(str)) {
-        return rb_tainted_str_new_cstr(str);
-    }
-    else {
-        long len = rbimpl_strlen(str);
-        return rb_tainted_str_new(str, len);
-    }
+    long len = rbimpl_strlen(str);
+    return rb_tainted_str_new(str, len);
 }
 
 static inline VALUE
 rbimpl_usascii_str_new_cstr(const char *str)
 {
-    if /* constexpr */ (! RBIMPL_CONSTANT_P(str)) {
-        return rb_usascii_str_new_cstr(str);
-    }
-    else {
-        long len = rbimpl_strlen(str);
-        return rb_usascii_str_new_static(str, len);
-    }
+    long len = rbimpl_strlen(str);
+    return rb_usascii_str_new_static(str, len);
 }
 
 static inline VALUE
 rbimpl_utf8_str_new_cstr(const char *str)
 {
-    if /* constexpr */ (! RBIMPL_CONSTANT_P(str)) {
-        return rb_utf8_str_new_cstr(str);
-    }
-    else {
-        long len = rbimpl_strlen(str);
-        return rb_utf8_str_new_static(str, len);
-    }
+    long len = rbimpl_strlen(str);
+    return rb_utf8_str_new_static(str, len);
 }
 
 static inline VALUE
 rbimpl_external_str_new_cstr(const char *str)
 {
-    if /* constexpr */ (! RBIMPL_CONSTANT_P(str)) {
-        return rb_external_str_new_cstr(str);
-    }
-    else {
-        long len = rbimpl_strlen(str);
-        return rb_external_str_new(str, len);
-    }
+    long len = rbimpl_strlen(str);
+    return rb_external_str_new(str, len);
 }
 
 static inline VALUE
 rbimpl_locale_str_new_cstr(const char *str)
 {
-    if /* constexpr */ (! RBIMPL_CONSTANT_P(str)) {
-        return rb_locale_str_new_cstr(str);
-    }
-    else {
-        long len = rbimpl_strlen(str);
-        return rb_locale_str_new(str, len);
-    }
+    long len = rbimpl_strlen(str);
+    return rb_locale_str_new(str, len);
 }
 
 static inline VALUE
 rbimpl_str_buf_new_cstr(const char *str)
 {
-    if /* constexpr */ (! RBIMPL_CONSTANT_P(str)) {
-        return rb_str_buf_new_cstr(str);
-    }
-    else {
-        long len = rbimpl_strlen(str);
-        VALUE buf = rb_str_buf_new(len);
-        return rb_str_buf_cat(buf, str, len);
-    }
+    long len = rbimpl_strlen(str);
+    VALUE buf = rb_str_buf_new(len);
+    return rb_str_buf_cat(buf, str, len);
 }
 
 static inline VALUE
 rbimpl_str_cat_cstr(VALUE buf, const char *str)
 {
-    if /* constexpr */ (! RBIMPL_CONSTANT_P(str)) {
-        return rb_str_cat_cstr(buf, str);
-    }
-    else {
-        long len = rbimpl_strlen(str);
-        return rb_str_cat(buf, str, len);
-    }
+    long len = rbimpl_strlen(str);
+    return rb_str_cat(buf, str, len);
 }
 
 static inline VALUE
 rbimpl_exc_new_cstr(VALUE exc, const char *str)
 {
-    if /* constexpr */ (! RBIMPL_CONSTANT_P(str)) {
-        return rb_exc_new_cstr(exc, str);
-    }
-    else {
-        long len = rbimpl_strlen(str);
-        return rb_exc_new(exc, str, len);
-    }
+    long len = rbimpl_strlen(str);
+    return rb_exc_new(exc, str, len);
 }
 
-#define rb_str_new rbimpl_str_new
-#define rb_str_new_cstr rbimpl_str_new_cstr
-#define rb_usascii_str_new rbimpl_usascii_str_new
-#define rb_utf8_str_new rbimpl_utf8_str_new
-#define rb_tainted_str_new_cstr rbimpl_tainted_str_new_cstr
-#define rb_usascii_str_new_cstr rbimpl_usascii_str_new_cstr
-#define rb_utf8_str_new_cstr rbimpl_utf8_str_new_cstr
-#define rb_external_str_new_cstr rbimpl_external_str_new_cstr
-#define rb_locale_str_new_cstr rbimpl_locale_str_new_cstr
-#define rb_str_buf_new_cstr rbimpl_str_buf_new_cstr
-#define rb_str_cat_cstr rbimpl_str_cat_cstr
-#define rb_exc_new_cstr rbimpl_exc_new_cstr
+#define rb_str_new(str, len)                    \
+    ((RBIMPL_CONSTANT_P(str) &&                 \
+      RBIMPL_CONSTANT_P(len) ?                  \
+      rb_str_new_static      :                  \
+      rb_str_new) ((str), (len)))
+
+#define rb_str_new_cstr(str)                    \
+    ((RBIMPL_CONSTANT_P(str) ?                  \
+      rbimpl_str_new_cstr    :                  \
+      rb_str_new_cstr) (str))
+
+#define rb_usascii_str_new(str, len)            \
+    ((RBIMPL_CONSTANT_P(str)    &&              \
+      RBIMPL_CONSTANT_P(len)    ?               \
+      rb_usascii_str_new_static :               \
+      rb_usascii_str_new) ((str), (len)))
+
+#define rb_utf8_str_new(str, len)               \
+    ((RBIMPL_CONSTANT_P(str) &&                 \
+      RBIMPL_CONSTANT_P(len) ?                  \
+      rb_utf8_str_new_static :                  \
+      rb_utf8_str_new) ((str), (len)))
+
+#define rb_tainted_str_new_cstr(str)            \
+    ((RBIMPL_CONSTANT_P(str)      ?             \
+      rbimpl_tainted_str_new_cstr :             \
+      rb_tainted_str_new_cstr) (str))
+
+#define rb_usascii_str_new_cstr(str)            \
+    ((RBIMPL_CONSTANT_P(str)      ?             \
+      rbimpl_usascii_str_new_cstr :             \
+      rb_usascii_str_new_cstr) (str))
+
+#define rb_utf8_str_new_cstr(str)               \
+    ((RBIMPL_CONSTANT_P(str)   ?                \
+      rbimpl_utf8_str_new_cstr :                \
+      rb_utf8_str_new_cstr) (str))
+
+#define rb_external_str_new_cstr(str)           \
+    ((RBIMPL_CONSTANT_P(str)       ?            \
+      rbimpl_external_str_new_cstr :            \
+      rb_external_str_new_cstr) (str))
+
+#define rb_locale_str_new_cstr(str)             \
+    ((RBIMPL_CONSTANT_P(str)     ?              \
+      rbimpl_locale_str_new_cstr :              \
+      rb_locale_str_new_cstr) (str))
+
+#define rb_str_buf_new_cstr(str)                \
+    ((RBIMPL_CONSTANT_P(str)  ?                 \
+      rbimpl_str_buf_new_cstr :                 \
+      rb_str_buf_new_cstr) (str))
+
+#define rb_str_cat_cstr(buf, str)               \
+    ((RBIMPL_CONSTANT_P(str) ?                  \
+      rbimpl_str_cat_cstr    :                  \
+      rb_str_cat_cstr) ((buf), (str)))
+
+#define rb_exc_new_cstr(exc, str)               \
+    ((RBIMPL_CONSTANT_P(str) ?                  \
+      rbimpl_exc_new_cstr    :                  \
+      rb_exc_new_cstr) ((exc), (str)))
+
 #define rb_str_new2 rb_str_new_cstr
 #define rb_str_new3 rb_str_new_shared
 #define rb_str_new4 rb_str_new_frozen
-- 
cgit v0.10.2


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

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