diff options
author | nagachika <nagachika@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2014-08-18 14:34:31 +0000 |
---|---|---|
committer | nagachika <nagachika@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2014-08-18 14:34:31 +0000 |
commit | 9355bcade93e7b58398d3d77fc7223d5818f016a (patch) | |
tree | 66c7865de748aae85093ed150f12ac13feb14c5a /string.c | |
parent | 5712fa573d0e8dd85b21c3bb668048f1985ccf89 (diff) |
merge revision(s) r46408,r46410,r46413,r46414,r46424,r46436,r46437: [Backport #9934]
string.c: shrink too big buffer
* string.c (rb_str_resize): shrink the buffer even if new length
is same but it is enough smaller than the capacity.
* file.c (expand_path): shrink expanded path which no longer needs
rooms to append. [ruby-core:63114] [Bug #9934]
* string.c (rb_str_resize): should consider the capacity instead
of the old length, as pointed out by nagachika.
* string.c (rb_str_resize): update capa only when buffer get
reallocated.
http://d.hatena.ne.jp/nagachika/20140613/ruby_trunk_changes_46413_46420#r46413
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_1@47215 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'string.c')
-rw-r--r-- | string.c | 11 |
1 files changed, 7 insertions, 4 deletions
@@ -2034,9 +2034,11 @@ rb_str_resize(VALUE str, long len) independent = str_independent(str); ENC_CODERANGE_CLEAR(str); slen = RSTRING_LEN(str); - if (len != slen) { + { + long capa; const int termlen = TERM_LEN(str); if (STR_EMBED_P(str)) { + if (len == slen) return str; if (len + termlen <= RSTRING_EMBED_LEN_MAX + 1) { STR_SET_EMBED_LEN(str, len); TERM_FILL(RSTRING(str)->as.ary + len, termlen); @@ -2056,14 +2058,15 @@ rb_str_resize(VALUE str, long len) return str; } else if (!independent) { + if (len == slen) return str; str_make_independent_expand(str, len - slen); } - else if (slen < len || slen - len > 1024) { + else if ((capa = RSTRING(str)->as.heap.aux.capa) < len || + (capa - len) > (len < 1024 ? len : 1024)) { REALLOC_N(RSTRING(str)->as.heap.ptr, char, len + termlen); - } - if (!STR_NOCAPA_P(str)) { RSTRING(str)->as.heap.aux.capa = len; } + else if (len == slen) return str; RSTRING(str)->as.heap.len = len; TERM_FILL(RSTRING(str)->as.heap.ptr + len, termlen); /* sentinel */ } |