This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
A better fix for leaking array assignment
authorFather Chrysostomos <sprout@cpan.org>
Mon, 24 Sep 2012 06:56:40 +0000 (23:56 -0700)
committerFather Chrysostomos <sprout@cpan.org>
Mon, 24 Sep 2012 16:37:11 +0000 (09:37 -0700)
Instead of filling up the mortals stack with as many SVs as there are
elements, just call get-magic before creating the new SV.

(This is not so easy for hashes, as we have keys as well, and
hv_common always calls get-magic on keys.)

See commits 9c744f4f4d7 and 39984de3a8, which fixed leaking bugs, but
inefficiently.

pp_hot.c

index a8d762b..302f47e 100644 (file)
--- a/pp_hot.c
+++ b/pp_hot.c
@@ -982,12 +982,14 @@ PP(pp_aassign)
            while (relem <= lastrelem) {        /* gobble up all the rest */
                SV **didstore;
                assert(*relem);
-               sv = sv_newmortal();
-               sv_setsv(sv, *relem);
+               SvGETMAGIC(*relem); /* before newSV, in case it dies */
+               sv = newSV(0);
+               sv_setsv_nomg(sv, *relem);
                *(relem++) = sv;
                didstore = av_store(ary,i++,sv);
-               if (didstore) SvREFCNT_inc_simple_void_NN(sv);
                if (magic) {
+                   if (!didstore)
+                       sv_2mortal(sv);
                    if (SvSMAGICAL(sv))
                        mg_set(sv);
                }