Skip to content

Commit

Permalink
Avoid unnecessary copying when removing the leading part of a string
Browse files Browse the repository at this point in the history
Remove the superfluous str_modify_keep_cr() call from rb_str_update().
It ends up calling either rb_str_drop_bytes() or rb_str_splice_0(),
which already does checks if necessary.

The extra call makes the string "independent". This is not always
wanted, in other words, it can keep the same shared root when merely
removing the leading part of a shared string.
  • Loading branch information
rhenium committed Sep 9, 2022
1 parent 9faa9ce commit aff6534
Showing 1 changed file with 0 additions and 1 deletion.
1 change: 0 additions & 1 deletion string.c
Expand Up @@ -5359,7 +5359,6 @@ rb_str_update(VALUE str, long beg, long len, VALUE val)
if (len > slen - beg) {
len = slen - beg;
}
str_modify_keep_cr(str);
p = str_nth(RSTRING_PTR(str), RSTRING_END(str), beg, enc, singlebyte);
if (!p) p = RSTRING_END(str);
e = str_nth(p, RSTRING_END(str), len, enc, singlebyte);
Expand Down

0 comments on commit aff6534

Please sign in to comment.