ruby-changes:10518
From: nobu <ko1@a...>
Date: Thu, 5 Feb 2009 12:02:25 +0900 (JST)
Subject: [ruby-changes:10518] Ruby:r22072 (trunk): * array.c (ary_add_hash): split from ary_make_hash().
nobu 2009-02-05 12:01:55 +0900 (Thu, 05 Feb 2009) New Revision: 22072 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=22072 Log: * array.c (ary_add_hash): split from ary_make_hash(). * array.c (ary_recycle_hash): clears internally used hash. this name came from [ruby-dev:37908]. * array.c (rb_ary_diff, rb_ary_and, rb_ary_or, rb_ary_uniq_bang): recycle hashes. Modified files: trunk/ChangeLog trunk/array.c Index: array.c =================================================================== --- array.c (revision 22071) +++ array.c (revision 22072) @@ -2864,23 +2864,35 @@ } static VALUE -ary_make_hash(VALUE ary1, VALUE ary2) +ary_add_hash(VALUE hash, VALUE ary) { - VALUE hash = rb_hash_new(); long i; - RBASIC(hash)->klass = 0; - for (i=0; i<RARRAY_LEN(ary1); i++) { - rb_hash_aset(hash, RARRAY_PTR(ary1)[i], Qtrue); + for (i=0; i<RARRAY_LEN(ary); i++) { + rb_hash_aset(hash, RARRAY_PTR(ary)[i], Qtrue); } - if (ary2) { - for (i=0; i<RARRAY_LEN(ary2); i++) { - rb_hash_aset(hash, RARRAY_PTR(ary2)[i], Qtrue); - } - } return hash; } +static VALUE +ary_make_hash(VALUE ary) +{ + VALUE hash = rb_hash_new(); + + RBASIC(hash)->klass = 0; + return ary_add_hash(hash, ary); +} + +static inline void +ary_recycle_hash(VALUE hash) +{ + if (RHASH(hash)->ntbl) { + st_table *tbl = RHASH(hash)->ntbl; + RHASH(hash)->ntbl = 0; + st_free_table(tbl); + } +} + /* * call-seq: * array - other_array -> an_array @@ -2900,13 +2912,14 @@ volatile VALUE hash; long i; - hash = ary_make_hash(to_ary(ary2), 0); + hash = ary_make_hash(to_ary(ary2)); ary3 = rb_ary_new(); for (i=0; i<RARRAY_LEN(ary1); i++) { if (st_lookup(RHASH_TBL(hash), RARRAY_PTR(ary1)[i], 0)) continue; rb_ary_push(ary3, rb_ary_elt(ary1, i)); } + ary_recycle_hash(hash); return ary3; } @@ -2930,7 +2943,7 @@ ary2 = to_ary(ary2); ary3 = rb_ary_new2(RARRAY_LEN(ary1) < RARRAY_LEN(ary2) ? RARRAY_LEN(ary1) : RARRAY_LEN(ary2)); - hash = ary_make_hash(ary2, 0); + hash = ary_make_hash(ary2); if (RHASH_EMPTY_P(hash)) return ary3; @@ -2941,6 +2954,7 @@ rb_ary_push(ary3, v); } } + ary_recycle_hash(hash); return ary3; } @@ -2965,7 +2979,7 @@ ary2 = to_ary(ary2); ary3 = rb_ary_new2(RARRAY_LEN(ary1)+RARRAY_LEN(ary2)); - hash = ary_make_hash(ary1, ary2); + hash = ary_add_hash(ary_make_hash(ary1), ary2); for (i=0; i<RARRAY_LEN(ary1); i++) { v = vv = rb_ary_elt(ary1, i); @@ -2979,6 +2993,7 @@ rb_ary_push(ary3, v); } } + ary_recycle_hash(hash); return ary3; } @@ -3002,7 +3017,7 @@ VALUE hash, v, vv; long i, j; - hash = ary_make_hash(ary, 0); + hash = ary_make_hash(ary); if (RARRAY_LEN(ary) == RHASH_SIZE(hash)) { return Qnil; @@ -3014,6 +3029,7 @@ } } ARY_SET_LEN(ary, j); + ary_recycle_hash(hash); return ary; } Index: ChangeLog =================================================================== --- ChangeLog (revision 22071) +++ ChangeLog (revision 22072) @@ -1,7 +1,15 @@ -Thu Feb 5 11:37:47 2009 Nobuyoshi Nakada <nobu@r...> +Thu Feb 5 12:01:53 2009 Nobuyoshi Nakada <nobu@r...> + * array.c (ary_add_hash): split from ary_make_hash(). + * array.c (ary_make_hash): hide a Hash used internally. + * array.c (ary_recycle_hash): clears internally used hash. this + name came from [ruby-dev:37908]. + + * array.c (rb_ary_diff, rb_ary_and, rb_ary_or, rb_ary_uniq_bang): + recycle hashes. + Thu Feb 5 11:21:35 2009 Nobuyoshi Nakada <nobu@r...> * configure.in (RUBY_LIB_VERSION): added for library version, to -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/