aboutsummaryrefslogtreecommitdiffstats
path: root/string.c
diff options
context:
space:
mode:
authorJohn Hawthorn <john@hawthorn.email>2019-06-25 09:01:57 -0700
committerNobuyoshi Nakada <nobu@ruby-lang.org>2019-06-26 15:01:48 +0900
commit04bc4c0662344238a556aa92f5f8f6d42586bab3 (patch)
treec3570330a2b7573f8d4aa42489c6cad2156cb4fa /string.c
parentfe0ddf0e58e65ab3ae3d6e73382c3bebcd4541e5 (diff)
downloadruby-04bc4c0662344238a556aa92f5f8f6d42586bab3.tar.gz
Resize capacity for fstring
When a string is #frozen, it's capacity is resized to fit (if it is much larger), since we know it will no longer be mutated. > puts ObjectSpace.dump(String.new("a"*30, capacity: 1000)) {"type":"STRING", "class":"0x7feaf00b7bf0", "bytesize":30, "capacity":1000, "value":"... > puts ObjectSpace.dump(String.new("a"*30, capacity: 1000).freeze) {"type":"STRING", "class":"0x7feaf00b7bf0", "frozen":true, "bytesize":30, "value":"... (ObjectSpace.dump doesn't show capacity if capacity is equal to bytesize) Previously, if we dedup into an fstring, using String#-@, capacity would not be reduced. > puts ObjectSpace.dump(-String.new("a"*30, capacity: 1000)) {"type":"STRING", "class":"0x7feaf00b7bf0", "frozen":true, "fstring":true, "bytesize":30, "capacity":1000, "value":"... This commit makes rb_fstring call rb_str_resize, the same as rb_str_freeze does. Closes: https://github.com/ruby/ruby/pull/2256
Diffstat (limited to 'string.c')
-rw-r--r--string.c3
1 files changed, 3 insertions, 0 deletions
diff --git a/string.c b/string.c
index 030465235a..8ac33e3e52 100644
--- a/string.c
+++ b/string.c
@@ -321,6 +321,9 @@ rb_fstring(VALUE str)
return str;
}
+ if (!OBJ_FROZEN(str))
+ rb_str_resize(str, RSTRING_LEN(str));
+
fstr = register_fstring(str);
if (!bare) {