diff --git a/ChangeLog b/ChangeLog index 30cab8267c..c0905e83f6 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +Wed May 21 16:48:22 2008 Yukihiro Matsumoto + + * array.c (rb_ary_compact_bang): avoid forceful realloc. + Wed May 21 07:42:28 2008 NARUSE, Yui * string.c (rb_usascii_str_new): use rb_str_new. diff --git a/array.c b/array.c index 6fd7a75e01..f42eb9f826 100644 --- a/array.c +++ b/array.c @@ -2710,11 +2710,13 @@ rb_ary_compact_bang(VALUE ary) if (NIL_P(*t)) t++; else *p++ = *t++; } - if (RARRAY_LEN(ary) == (p - RARRAY_PTR(ary))) { + n = p - RARRAY_PTR(ary); + if (RARRAY_LEN(ary) == n) { return Qnil; } - n = p - RARRAY_PTR(ary); - RESIZE_CAPA(ary, n); + if (n * 2 < ARY_CAPA(ary) && ARY_DEFAULT_SIZE * 2 < ARY_CAPA(ary)) { + RESIZE_CAPA(ary, ARY_DEFAULT_SIZE * 2); + } RARRAY(ary)->len = n; return ary;