Skip to content

Commit 2e88bca

Browse files
committed
string.c: don't create a frozen copy for str_new_shared
str_new_shared already has all the necessary logic to do this and is also smart enough to skip this step if the source string is already a shared string itself. This saves a useless String allocation on each call.
1 parent 5b03964 commit 2e88bca

File tree

1 file changed

+2
-4
lines changed

1 file changed

+2
-4
lines changed

string.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2923,8 +2923,7 @@ str_substr(VALUE str, long beg, long len, int empty)
29232923
if (!STR_EMBEDDABLE_P(len, TERM_LEN(str)) &&
29242924
SHARABLE_SUBSTRING_P(p, len, RSTRING_END(str))) {
29252925
long ofs = p - RSTRING_PTR(str);
2926-
str2 = rb_str_new_frozen(str);
2927-
str2 = str_new_shared(rb_cString, str2);
2926+
str2 = str_new_shared(rb_cString, str);
29282927
RSTRING(str2)->as.heap.ptr += ofs;
29292928
RSTRING(str2)->as.heap.len = len;
29302929
ENC_CODERANGE_CLEAR(str2);
@@ -6162,8 +6161,7 @@ str_byte_substr(VALUE str, long beg, long len, int empty)
61626161
p = s + beg;
61636162

61646163
if (!STR_EMBEDDABLE_P(len, TERM_LEN(str)) && SHARABLE_SUBSTRING_P(beg, len, n)) {
6165-
str2 = rb_str_new_frozen(str);
6166-
str2 = str_new_shared(rb_cString, str2);
6164+
str2 = str_new_shared(rb_cString, str);
61676165
RSTRING(str2)->as.heap.ptr += beg;
61686166
RSTRING(str2)->as.heap.len = len;
61696167
}

0 commit comments

Comments
 (0)