diff options
author | Jean Boussier <[email protected]> | 2022-09-26 12:48:24 +0200 |
---|---|---|
committer | Jean Boussier <[email protected]> | 2022-09-26 13:41:17 +0200 |
commit | 2e88bca24ff4cafeb6afe5b062ff7181bc4b3a9b (patch) | |
tree | e77b707d1edaea8b1fa6c840e8a146a4175dd505 /string.c | |
parent | 5b0396473bbcd70756a09d887fb7436d6cd72dce (diff) |
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.
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/6443
Diffstat (limited to 'string.c')
-rw-r--r-- | string.c | 6 |
1 files changed, 2 insertions, 4 deletions
@@ -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; } |