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

ruby-changes:31783

From: nobu <ko1@a...>
Date: Wed, 27 Nov 2013 02:27:09 +0900 (JST)
Subject: [ruby-changes:31783] nobu:r43862 (trunk): string.c: fix memsize of frozen shared string

nobu	2013-11-27 02:27:01 +0900 (Wed, 27 Nov 2013)

  New Revision: 43862

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=43862

  Log:
    string.c: fix memsize of frozen shared string
    
    * string.c (str_new4): copy the original capacity so that memsize of
      frozen shared string returns correct size.

  Modified files:
    trunk/ChangeLog
    trunk/string.c
    trunk/test/objspace/test_objspace.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 43861)
+++ ChangeLog	(revision 43862)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Wed Nov 27 02:26:58 2013  Nobuyoshi Nakada  <nobu@r...>
+
+	* string.c (str_new4): copy the original capacity so that memsize of
+	  frozen shared string returns correct size.
+
 Wed Nov 27 02:20:13 2013  Nobuyoshi Nakada  <nobu@r...>
 
 	* array.c (rb_ary_hash): should not ignore the rest of recursive
Index: string.c
===================================================================
--- string.c	(revision 43861)
+++ string.c	(revision 43862)
@@ -749,6 +749,9 @@ str_new4(VALUE klass, VALUE str) https://github.com/ruby/ruby/blob/trunk/string.c#L749
 	STR_SET_SHARED(str2, shared); /* TODO: WB is not needed because str2 is *new* object */
     }
     else {
+	if (!STR_ASSOC_P(str)) {
+	    RSTRING(str2)->as.heap.aux.capa = RSTRING(str)->as.heap.aux.capa;
+	}
 	STR_SET_SHARED(str, str2);
     }
     rb_enc_cr_str_exact_copy(str2, str);
@@ -879,7 +882,7 @@ rb_str_free(VALUE str) https://github.com/ruby/ruby/blob/trunk/string.c#L882
 RUBY_FUNC_EXPORTED size_t
 rb_str_memsize(VALUE str)
 {
-    if (!STR_EMBED_P(str) && !STR_SHARED_P(str)) {
+    if (FL_TEST(str, STR_NOEMBED|ELTS_SHARED) == STR_NOEMBED) {
 	return STR_HEAP_SIZE(str);
     }
     else {
Index: test/objspace/test_objspace.rb
===================================================================
--- test/objspace/test_objspace.rb	(revision 43861)
+++ test/objspace/test_objspace.rb	(revision 43862)
@@ -24,6 +24,14 @@ class TestObjSpace < Test::Unit::TestCas https://github.com/ruby/ruby/blob/trunk/test/objspace/test_objspace.rb#L24
                     ObjectSpace.memsize_of(//.match("")))
   end
 
+  def test_memsize_of_root_shared_string
+    a = "hello" * 5
+    b = a.dup
+    c = nil
+    ObjectSpace.each_object(String) {|x| break c = x if x == a and x.frozen?}
+    assert_equal([0, 0, 26], [a, b, c].map {|x| ObjectSpace.memsize_of(x)})
+  end
+
   def test_argf_memsize
     size = ObjectSpace.memsize_of(ARGF)
     assert_kind_of(Integer, size)

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

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