From 2e88bca24ff4cafeb6afe5b062ff7181bc4b3a9b Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Mon, 26 Sep 2022 12:48:24 +0200 Subject: [PATCH] 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. --- string.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/string.c b/string.c index b455c8b7d9..1e7b16fbba 100644 --- a/string.c +++ b/string.c @@ -2923,8 +2923,7 @@ str_substr(VALUE str, long beg, long len, int empty) if (!STR_EMBEDDABLE_P(len, TERM_LEN(str)) && SHARABLE_SUBSTRING_P(p, len, RSTRING_END(str))) { long ofs = p - RSTRING_PTR(str); - str2 = rb_str_new_frozen(str); - str2 = str_new_shared(rb_cString, str2); + str2 = str_new_shared(rb_cString, str); RSTRING(str2)->as.heap.ptr += ofs; RSTRING(str2)->as.heap.len = len; ENC_CODERANGE_CLEAR(str2); @@ -6162,8 +6161,7 @@ str_byte_substr(VALUE str, long beg, long len, int empty) p = s + beg; if (!STR_EMBEDDABLE_P(len, TERM_LEN(str)) && SHARABLE_SUBSTRING_P(beg, len, n)) { - str2 = rb_str_new_frozen(str); - str2 = str_new_shared(rb_cString, str2); + str2 = str_new_shared(rb_cString, str); RSTRING(str2)->as.heap.ptr += beg; RSTRING(str2)->as.heap.len = len; }