summaryrefslogtreecommitdiff
path: root/array.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-08-25 08:56:47 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-08-25 08:56:47 +0000
commit569d8219292fefe205979fd8ea951619316d4f72 (patch)
tree1d8e89c9d4ae792382f74cb2a6e2778075f0e3ea /array.c
parentc6465aeeb153428057426663d51906052e39b36c (diff)
* array.c (rb_ary_sample): use frozen shared array to get rid of
reallocation. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@29092 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'array.c')
-rw-r--r--array.c2
1 files changed, 1 insertions, 1 deletions
diff --git a/array.c b/array.c
index 172a653aa2..b506b086d4 100644
--- a/array.c
+++ b/array.c
@@ -3826,6 +3826,7 @@ rb_ary_sample(int argc, VALUE *argv, VALUE ary)
rb_scan_args(argc, argv, "1", &nv);
n = NUM2LONG(nv);
if (n < 0) rb_raise(rb_eArgError, "negative sample number");
+ RB_GC_GUARD(ary) = ary_make_shared(ary);
ptr = RARRAY_PTR(ary);
len = RARRAY_LEN(ary);
if (n > len) n = len;
@@ -3872,7 +3873,6 @@ rb_ary_sample(int argc, VALUE *argv, VALUE ary)
VALUE *ptr_result;
result = rb_ary_new4(len, ptr);
ptr_result = RARRAY_PTR(result);
- RB_GC_GUARD(ary);
for (i=0; i<n; i++) {
j = RAND_UPTO(len-i) + i;
nv = ptr_result[j];