[前][次][番号順一覧][スレッド一覧]

ruby-changes:45400

From: normal <ko1@a...>
Date: Tue, 31 Jan 2017 09:42:01 +0900 (JST)
Subject: [ruby-changes:45400] normal:r57473 (trunk): sprintf.c: avoid garbage in common (no exception) case

normal	2017-01-31 09:41:56 +0900 (Tue, 31 Jan 2017)

  New Revision: 57473

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=57473

  Log:
    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

  Modified files:
    trunk/sprintf.c
    trunk/test/ruby/test_sprintf.rb
Index: test/ruby/test_sprintf.rb
===================================================================
--- test/ruby/test_sprintf.rb	(revision 57472)
+++ test/ruby/test_sprintf.rb	(revision 57473)
@@ -451,4 +451,14 @@ class TestSprintf < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_sprintf.rb#L451
     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
Index: sprintf.c
===================================================================
--- sprintf.c	(revision 57472)
+++ sprintf.c	(revision 57473)
@@ -475,6 +475,7 @@ rb_str_format(int argc, const VALUE *arg https://github.com/ruby/ruby/blob/trunk/sprintf.c#L475
     int tainted = 0;
     VALUE nextvalue;
     VALUE tmp;
+    VALUE orig;
     VALUE str;
     volatile VALUE hash = Qundef;
 
@@ -498,7 +499,8 @@ rb_str_format(int argc, const VALUE *arg https://github.com/ruby/ruby/blob/trunk/sprintf.c#L499
     if (OBJ_TAINTED(fmt)) tainted = 1;
     StringValue(fmt);
     enc = rb_enc_get(fmt);
-    fmt = rb_str_new4(fmt);
+    orig = fmt;
+    fmt = rb_str_tmp_frozen_acquire(fmt);
     p = RSTRING_PTR(fmt);
     end = p + RSTRING_LEN(fmt);
     blen = 0;
@@ -1196,7 +1198,7 @@ rb_str_format(int argc, const VALUE *arg https://github.com/ruby/ruby/blob/trunk/sprintf.c#L1198
     }
 
   sprint_exit:
-    RB_GC_GUARD(fmt);
+    rb_str_tmp_frozen_release(orig, fmt);
     /* XXX - We cannot validate the number of arguments if (digit)$ style used.
      */
     if (posarg >= 0 && nextarg < argc) {

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]