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

ruby-changes:73760

From: Peter <ko1@a...>
Date: Wed, 28 Sep 2022 22:05:47 +0900 (JST)
Subject: [ruby-changes:73760] 6f8d17e43c (master): Make string slices views rather than copies

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

From 6f8d17e43c7b9b3b8bf42df0ef1ca085080b65f0 Mon Sep 17 00:00:00 2001
From: Peter Zhu <peter@p...>
Date: Tue, 27 Sep 2022 14:50:40 -0400
Subject: Make string slices views rather than copies

Just like commit 1c16645 for arrays, this commit changes string slices
to be a view rather than a copy even if it can be allocated through VWA.
---
 string.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)

diff --git a/string.c b/string.c
index b9c567cbe2..bdab6fa143 100644
--- a/string.c
+++ b/string.c
@@ -2802,18 +2802,20 @@ str_subseq(VALUE str, long beg, long len) https://github.com/ruby/ruby/blob/trunk/string.c#L2802
 {
     VALUE str2;
 
-    if (!STR_EMBEDDABLE_P(len, TERM_LEN(str)) &&
-            SHARABLE_SUBSTRING_P(beg, len, RSTRING_LEN(str))) {
-        str2 = rb_str_new_shared(str);
+    const long rstring_embed_capa_max = ((sizeof(struct RString) - offsetof(struct RString, as.embed.ary)) / sizeof(char)) - 1;
+
+    if (!SHARABLE_SUBSTRING_P(beg, len, RSTRING_LEN(str)) ||
+            len <= rstring_embed_capa_max) {
+        str2 = rb_str_new(RSTRING_PTR(str) + beg, len);
+        RB_GC_GUARD(str);
+    }
+    else {
+        str2 = str_new_shared(rb_cString, str);
         RSTRING(str2)->as.heap.ptr += beg;
         if (RSTRING(str2)->as.heap.len > len) {
             RSTRING(str2)->as.heap.len = len;
         }
     }
-    else {
-        str2 = rb_str_new(RSTRING_PTR(str) + beg, len);
-        RB_GC_GUARD(str);
-    }
 
     return str2;
 }
-- 
cgit v1.2.1


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

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