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

ruby-changes:74328

From: Peter <ko1@a...>
Date: Thu, 3 Nov 2022 22:09:28 +0900 (JST)
Subject: [ruby-changes:74328] 0468136a1b (master): Make str_alloc_heap return a STR_NOEMBED string

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

From 0468136a1b07d693cf60abd0c5ccd125fc361039 Mon Sep 17 00:00:00 2001
From: Peter Zhu <peter@p...>
Date: Wed, 2 Nov 2022 15:21:50 -0400
Subject: Make str_alloc_heap return a STR_NOEMBED string

This commit refactors str_alloc_heap to return a string with the
STR_NOEMBED flag set.
---
 string.c | 53 ++++++++++++++++++++++++-----------------------------
 1 file changed, 24 insertions(+), 29 deletions(-)

diff --git a/string.c b/string.c
index 1fd82595fe..ca2cb4525e 100644
--- a/string.c
+++ b/string.c
@@ -877,30 +877,29 @@ must_not_null(const char *ptr) https://github.com/ruby/ruby/blob/trunk/string.c#L877
     }
 }
 
-static inline VALUE
-str_alloc(VALUE klass, size_t size)
-{
-    assert(size > 0);
-    RVARGC_NEWOBJ_OF(str, struct RString, klass,
-                     T_STRING | (RGENGC_WB_PROTECTED_STRING ? FL_WB_PROTECTED : 0), size);
-    return (VALUE)str;
-}
-
 static inline VALUE
 str_alloc_embed(VALUE klass, size_t capa)
 {
     size_t size = rb_str_embed_size(capa);
+    assert(size > 0);
     assert(rb_gc_size_allocatable_p(size));
 #if !USE_RVARGC
     assert(size <= sizeof(struct RString));
 #endif
-    return str_alloc(klass, size);
+
+    RVARGC_NEWOBJ_OF(str, struct RString, klass,
+                     T_STRING | (RGENGC_WB_PROTECTED_STRING ? FL_WB_PROTECTED : 0), size);
+
+    return (VALUE)str;
 }
 
 static inline VALUE
 str_alloc_heap(VALUE klass)
 {
-    return str_alloc(klass, sizeof(struct RString));
+    RVARGC_NEWOBJ_OF(str, struct RString, klass,
+                     T_STRING | STR_NOEMBED | (RGENGC_WB_PROTECTED_STRING ? FL_WB_PROTECTED : 0), sizeof(struct RString));
+
+    return (VALUE)str;
 }
 
 static inline VALUE
@@ -937,7 +936,6 @@ str_new0(VALUE klass, const char *ptr, long len, int termlen) https://github.com/ruby/ruby/blob/trunk/string.c#L936
          * mul_add_mul can be reverted to a simple ALLOC_N. */
         RSTRING(str)->as.heap.ptr =
             rb_xmalloc_mul_add_mul(sizeof(char), len, sizeof(char), termlen);
-        STR_SET_NOEMBED(str);
     }
     if (ptr) {
         memcpy(RSTRING_PTR(str), ptr, len);
@@ -1044,7 +1042,6 @@ str_new_static(VALUE klass, const char *ptr, long len, int encindex) https://github.com/ruby/ruby/blob/trunk/string.c#L1042
         RSTRING(str)->as.heap.len = len;
         RSTRING(str)->as.heap.ptr = (char *)ptr;
         RSTRING(str)->as.heap.aux.capa = len;
-        STR_SET_NOEMBED(str);
         RBASIC(str)->flags |= STR_NOFREE;
     }
     rb_enc_associate_index(str, encindex);
@@ -1441,7 +1438,6 @@ heap_str_make_shared(VALUE klass, VALUE orig) https://github.com/ruby/ruby/blob/trunk/string.c#L1438
     assert(!STR_SHARED_P(orig));
 
     VALUE str = str_alloc_heap(klass);
-    STR_SET_NOEMBED(str);
     RSTRING(str)->as.heap.len = RSTRING_LEN(orig);
     RSTRING(str)->as.heap.ptr = RSTRING_PTR(orig);
     RSTRING(str)->as.heap.aux.capa = RSTRING(orig)->as.heap.aux.capa;
@@ -1542,7 +1538,6 @@ rb_str_buf_new(long capa) https://github.com/ruby/ruby/blob/trunk/string.c#L1538
         capa = STR_BUF_MIN_SIZE;
     }
 #endif
-    FL_SET(str, STR_NOEMBED);
     RSTRING(str)->as.heap.aux.capa = capa;
     RSTRING(str)->as.heap.ptr = ALLOC_N(char, (size_t)capa + 1);
     RSTRING(str)->as.heap.ptr[0] = '\0';
@@ -1721,30 +1716,29 @@ str_replace(VALUE str, VALUE str2) https://github.com/ruby/ruby/blob/trunk/string.c#L1716
     return str;
 }
 
-static inline VALUE
-ec_str_alloc(struct rb_execution_context_struct *ec, VALUE klass, size_t size)
-{
-    assert(size > 0);
-    RB_RVARGC_EC_NEWOBJ_OF(ec, str, struct RString, klass,
-                           T_STRING | (RGENGC_WB_PROTECTED_STRING ? FL_WB_PROTECTED : 0), size);
-    return (VALUE)str;
-}
-
 static inline VALUE
 ec_str_alloc_embed(struct rb_execution_context_struct *ec, VALUE klass, size_t capa)
 {
     size_t size = rb_str_embed_size(capa);
+    assert(size > 0);
     assert(rb_gc_size_allocatable_p(size));
 #if !USE_RVARGC
     assert(size <= sizeof(struct RString));
 #endif
-    return ec_str_alloc(ec, klass, size);
+
+    RB_RVARGC_EC_NEWOBJ_OF(ec, str, struct RString, klass,
+                           T_STRING | (RGENGC_WB_PROTECTED_STRING ? FL_WB_PROTECTED : 0), size);
+
+    return (VALUE)str;
 }
 
 static inline VALUE
 ec_str_alloc_heap(struct rb_execution_context_struct *ec, VALUE klass)
 {
-    return ec_str_alloc(ec, klass, sizeof(struct RString));
+    RB_RVARGC_EC_NEWOBJ_OF(ec, str, struct RString, klass,
+                           T_STRING | STR_NOEMBED | (RGENGC_WB_PROTECTED_STRING ? FL_WB_PROTECTED : 0), sizeof(struct RString));
+
+    return (VALUE)str;
 }
 
 static inline VALUE
@@ -1762,6 +1756,7 @@ str_duplicate_setup(VALUE klass, VALUE str, VALUE dup) https://github.com/ruby/ruby/blob/trunk/string.c#L1756
     if (STR_EMBED_P(str)) {
         long len = RSTRING_EMBED_LEN(str);
 
+        assert(STR_EMBED_P(dup));
         assert(str_embed_capa(dup) >= len + 1);
         STR_SET_EMBED_LEN(dup, len);
         MEMCPY(RSTRING(dup)->as.embed.ary, RSTRING(str)->as.embed.ary, char, len + 1);
@@ -1782,6 +1777,7 @@ str_duplicate_setup(VALUE klass, VALUE str, VALUE dup) https://github.com/ruby/ruby/blob/trunk/string.c#L1777
         else if (STR_EMBED_P(root)) {
             MEMCPY(RSTRING(dup)->as.embed.ary, RSTRING(root)->as.embed.ary,
                    char, RSTRING_EMBED_LEN_MAX + 1);
+            FL_UNSET(dup, STR_NOEMBED);
         }
 #endif
         else {
@@ -1805,7 +1801,7 @@ static inline VALUE https://github.com/ruby/ruby/blob/trunk/string.c#L1801
 ec_str_duplicate(struct rb_execution_context_struct *ec, VALUE klass, VALUE str)
 {
     VALUE dup;
-    if (!USE_RVARGC || FL_TEST(str, STR_NOEMBED)) {
+    if (FL_TEST(str, STR_NOEMBED)) {
         dup = ec_str_alloc_heap(ec, klass);
     }
     else {
@@ -1819,7 +1815,7 @@ static inline VALUE https://github.com/ruby/ruby/blob/trunk/string.c#L1815
 str_duplicate(VALUE klass, VALUE str)
 {
     VALUE dup;
-    if (!USE_RVARGC || FL_TEST(str, STR_NOEMBED)) {
+    if (FL_TEST(str, STR_NOEMBED)) {
         dup = str_alloc_heap(klass);
     }
     else {
@@ -2307,7 +2303,6 @@ rb_str_times(VALUE str, VALUE times) https://github.com/ruby/ruby/blob/trunk/string.c#L2303
             str2 = str_alloc_heap(rb_cString);
             RSTRING(str2)->as.heap.aux.capa = len;
             RSTRING(str2)->as.heap.ptr = ZALLOC_N(char, (size_t)len + 1);
-            STR_SET_NOEMBED(str2);
         }
         STR_SET_LEN(str2, len);
         rb_enc_copy(str2, str);
-- 
cgit v1.2.3


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

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