diff options
author | normal <normal@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-01-31 00:41:56 +0000 |
---|---|---|
committer | normal <normal@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-01-31 00:41:56 +0000 |
commit | 120766ca4528257d7c4e368920ae6f3e9e68b369 (patch) | |
tree | fec1a3a25d77156c9498a93272fcb7502c8a4cf8 /test | |
parent | 345c459abfcab0d9f50468ad49a5bc3087b2381e (diff) | |
download | ruby-120766ca4528257d7c4e368920ae6f3e9e68b369.tar.gz |
sprintf.c: avoid garbage in common (no exception) case
Format strings which are dynamically-generated will benefit
from this. This won't cover exceptions, but exceptions for
sprintf should be too uncommon to care about (unlike IO)
* sprintf.c (rb_str_format): use rb_str_tmp_frozen_{acquire,release}
* test/ruby/test_sprintf.rb (test_no_hidden_garbage): new test
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57473 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test')
-rw-r--r-- | test/ruby/test_sprintf.rb | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/test/ruby/test_sprintf.rb b/test/ruby/test_sprintf.rb index 595bf6d782..c80f88b15e 100644 --- a/test/ruby/test_sprintf.rb +++ b/test/ruby/test_sprintf.rb @@ -451,4 +451,14 @@ class TestSprintf < Test::Unit::TestCase bug = 'https://github.com/mruby/mruby/issues/3347' assert_equal("!", sprintf("%*c", 0, ?!.ord), bug) end + + def test_no_hidden_garbage + fmt = [4, 2, 2].map { |x| "%0#{x}d" }.join('-') # defeats optimization + ObjectSpace.count_objects(res = {}) # creates strings on first call + before = ObjectSpace.count_objects(res)[:T_STRING] + val = sprintf(fmt, 1970, 1, 1) + after = ObjectSpace.count_objects(res)[:T_STRING] + assert_equal before + 1, after, 'only new string is the created one' + assert_equal '1970-01-01', val + end end |