Don't call modify() on RubyStrings so eagerly when freezing them #3022
+2
−2
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This fixes #3019, where in certain cases it's possible to end up with
RubyString instances backed by very large unique ByteLists where only
a tiny portion of the bytes in the ByteList are actually needed.
What was happening is
modify
was being called on RubyStringinstances too eagerly, resulting in unnecessary duplication of their
underlying ByteList instances instead of sharing when possible.
The two changes here are in
RubyString#newFrozen
andRubyString#resize
and I believe are more correct based on the Cimplementation of these methods.
For
RubyString#newFrozen
, which roughly corresponds torb_str_new_frozen
inC (https://github.com/ruby/ruby/blob/v2_2_2/string.c#L932), the logic
looks to me to return the shared string and I don't see it calling
str_make_independent
, which is roughly the same thing asRubyString#modify
. So, I just removed the call tomodify
.For
RubyString#resize
, which roughly corresponds to torb_str_resize
in C, shared strings are only made independent iftheir length
differ (https://github.com/ruby/ruby/blob/v2_2_2/string.c#L2155). Thus,
instead of unconditionally calling
modify
here, I moved it into theconditions that are true when the length differs.