ruby-changes:53876
From: tenderlove <ko1@a...>
Date: Fri, 30 Nov 2018 05:39:57 +0900 (JST)
Subject: [ruby-changes:53876] tenderlove:r66095 (trunk): Use a shared array for the `duparray` instruction
tenderlove 2018-11-30 05:39:51 +0900 (Fri, 30 Nov 2018) New Revision: 66095 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=66095 Log: Use a shared array for the `duparray` instruction In this example code: ```ruby def foo [1, 2, 3, 4] end ``` The array literal uses a `duparray` instruction. Before this patch, `rb_ary_resurrect` would malloc and memcpy a new array buffer. This patch changes `rb_ary_resurrect` to use `ary_make_partial` so that the new array object shares the underlying buffer with the array stored in the instruction sequences. Before this patch, the new array object is not shared: ``` $ ruby -r objspace -e'p ObjectSpace.dump([1, 2, 3, 4])' "{\"address\":\"0x7fa2718372d0\", \"type\":\"ARRAY\", \"class\":\"0x7fa26f8b0010\", \"length\":4, \"memsize\":72, \"flags\":{\"wb_protected\":true}}\n" ``` After this patch: ``` $ ./ruby -r objspace -e'p ObjectSpace.dump([1, 2, 3, 4])' "{\"address\":\"0x7f9a76883638\", \"type\":\"ARRAY\", \"class\":\"0x7f9a758af900\", \"length\":4, \"shared\":true, \"references\":[\"0x7f9a768837c8\"], \"memsize\":40, \"flags\":{\"wb_protected\":true}}\n" ``` [Feature #15289] [ruby-core:90097] Modified files: trunk/array.c Index: array.c =================================================================== --- array.c (revision 66094) +++ array.c (revision 66095) @@ -2191,7 +2191,7 @@ rb_ary_dup(VALUE ary) https://github.com/ruby/ruby/blob/trunk/array.c#L2191 VALUE rb_ary_resurrect(VALUE ary) { - return rb_ary_new4(RARRAY_LEN(ary), RARRAY_CONST_PTR_TRANSIENT(ary)); + return ary_make_partial(ary, rb_cArray, 0, RARRAY_LEN(ary)); } extern VALUE rb_output_fs; -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/