aboutsummaryrefslogtreecommitdiffstats
path: root/time.c
diff options
context:
space:
mode:
authornormal <normal@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-01-31 02:18:58 +0000
committernormal <normal@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-01-31 02:18:58 +0000
commit3dd233315a6907a39430eb170a5ce96eb10883eb (patch)
treec8762b5fbb780498076fb2727dcb65b4226ddb93 /time.c
parent15e45d3e695b909c8b28c06a199ff826a736345e (diff)
downloadruby-3dd233315a6907a39430eb170a5ce96eb10883eb.tar.gz
time.c (time_strftime): avoid garbage in common case
strftime format strings which are dynamically-generated will benefit from avoiding garbage, here. * time.c (time_strftime): use rb_str_tmp_frozen_{acquire,release} * test/ruby/test_time.rb (test_strftime_no_hidden_garbage): new test git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57476 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'time.c')
-rw-r--r--time.c8
1 files changed, 5 insertions, 3 deletions
diff --git a/time.c b/time.c
index 30bb371781..0d93ec65ff 100644
--- a/time.c
+++ b/time.c
@@ -4422,6 +4422,7 @@ time_strftime(VALUE time, VALUE format)
const char *fmt;
long len;
rb_encoding *enc;
+ VALUE tmp;
GetTimeval(time, tobj);
MAKE_TM(time, tobj);
@@ -4429,9 +4430,9 @@ time_strftime(VALUE time, VALUE format)
if (!rb_enc_str_asciicompat_p(format)) {
rb_raise(rb_eArgError, "format should have ASCII compatible encoding");
}
- format = rb_str_new4(format);
- fmt = RSTRING_PTR(format);
- len = RSTRING_LEN(format);
+ tmp = rb_str_tmp_frozen_acquire(format);
+ fmt = RSTRING_PTR(tmp);
+ len = RSTRING_LEN(tmp);
enc = rb_enc_get(format);
if (len == 0) {
rb_warning("strftime called with empty format string");
@@ -4440,6 +4441,7 @@ time_strftime(VALUE time, VALUE format)
else {
VALUE str = rb_strftime_alloc(fmt, len, enc, &tobj->vtm, tobj->timew,
TIME_UTC_P(tobj));
+ rb_str_tmp_frozen_release(format, tmp);
if (!str) rb_raise(rb_eArgError, "invalid format: %"PRIsVALUE, format);
return str;
}