7e86df
From d7504df2a5d8985f2a8b04f17acff5e324572c39 Mon Sep 17 00:00:00 2001
7e86df
From: Richard Leach <richardleach@users.noreply.github.com>
7e86df
Date: Sun, 11 Oct 2020 12:26:27 +0100
7e86df
Subject: [PATCH] pp_split: no SWITCHSTACK in @ary = split(...) optimisation
7e86df
MIME-Version: 1.0
7e86df
Content-Type: text/plain; charset=UTF-8
7e86df
Content-Transfer-Encoding: 8bit
7e86df
7e86df
Petr Písař: 607eaf26a99ff76ab48877e68f1d7b005dc51575 ported to 5.32.0.
7e86df
7e86df
Signed-off-by: Petr Písař <ppisar@redhat.com>
7e86df
---
7e86df
 pp.c         | 89 +++++++++++++++++++++++++++++-----------------------
7e86df
 t/op/split.t | 23 +++++++++++++-
7e86df
 2 files changed, 72 insertions(+), 40 deletions(-)
7e86df
7e86df
diff --git a/pp.c b/pp.c
7e86df
index df80830..e4863d3 100644
7e86df
--- a/pp.c
7e86df
+++ b/pp.c
7e86df
@@ -5985,6 +5985,7 @@ PP(pp_split)
7e86df
 
7e86df
     /* handle @ary = split(...) optimisation */
7e86df
     if (PL_op->op_private & OPpSPLIT_ASSIGN) {
7e86df
+	realarray = 1;
7e86df
         if (!(PL_op->op_flags & OPf_STACKED)) {
7e86df
             if (PL_op->op_private & OPpSPLIT_LEX) {
7e86df
                 if (PL_op->op_private & OPpLVAL_INTRO)
7e86df
@@ -6007,26 +6008,10 @@ PP(pp_split)
7e86df
             oldsave = PL_savestack_ix;
7e86df
         }
7e86df
 
7e86df
-	realarray = 1;
7e86df
-	PUTBACK;
7e86df
-	av_extend(ary,0);
7e86df
-	(void)sv_2mortal(SvREFCNT_inc_simple_NN(sv));
7e86df
-	av_clear(ary);
7e86df
-	SPAGAIN;
7e86df
 	if ((mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied))) {
7e86df
 	    PUSHMARK(SP);
7e86df
 	    XPUSHs(SvTIED_obj(MUTABLE_SV(ary), mg));
7e86df
-	}
7e86df
-	else {
7e86df
-	    if (!AvREAL(ary)) {
7e86df
-		I32 i;
7e86df
-		AvREAL_on(ary);
7e86df
-		AvREIFY_off(ary);
7e86df
-		for (i = AvFILLp(ary); i >= 0; i--)
7e86df
-		    AvARRAY(ary)[i] = &PL_sv_undef; /* don't free mere refs */
7e86df
-	    }
7e86df
-	    /* temporarily switch stacks */
7e86df
-	    SAVESWITCHSTACK(PL_curstack, ary);
7e86df
+	} else {
7e86df
 	    make_mortal = 0;
7e86df
 	}
7e86df
     }
7e86df
@@ -6358,29 +6343,56 @@ PP(pp_split)
7e86df
     LEAVE_SCOPE(oldsave); /* may undo an earlier SWITCHSTACK */
7e86df
     SPAGAIN;
7e86df
     if (realarray) {
7e86df
-	if (!mg) {
7e86df
-	    if (SvSMAGICAL(ary)) {
7e86df
-		PUTBACK;
7e86df
+        if (!mg) {
7e86df
+            PUTBACK;
7e86df
+            if(AvREAL(ary)) {
7e86df
+                if (av_count(ary) > 0)
7e86df
+                    av_clear(ary);
7e86df
+            } else {
7e86df
+                AvREAL_on(ary);
7e86df
+                AvREIFY_off(ary);
7e86df
+
7e86df
+                if (AvMAX(ary) > -1) {
7e86df
+                    /* don't free mere refs */
7e86df
+                    Zero(AvARRAY(ary), AvMAX(ary), SV*);
7e86df
+                }
7e86df
+            }
7e86df
+            if(AvMAX(ary) < iters)
7e86df
+                av_extend(ary,iters);
7e86df
+            SPAGAIN;
7e86df
+
7e86df
+            /* Need to copy the SV*s from the stack into ary */
7e86df
+            Copy(SP + 1 - iters, AvARRAY(ary), iters, SV*);
7e86df
+            AvFILLp(ary) = iters - 1;
7e86df
+
7e86df
+            if (SvSMAGICAL(ary)) {
7e86df
+                PUTBACK;
7e86df
 		mg_set(MUTABLE_SV(ary));
7e86df
 		SPAGAIN;
7e86df
-	    }
7e86df
-	    if (gimme == G_ARRAY) {
7e86df
-		EXTEND(SP, iters);
7e86df
-		Copy(AvARRAY(ary), SP + 1, iters, SV*);
7e86df
-		SP += iters;
7e86df
-		RETURN;
7e86df
-	    }
7e86df
+            }
7e86df
+
7e86df
+            if (gimme != G_ARRAY) {
7e86df
+                /* SP points to the final SV* pushed to the stack. But the SV*  */
7e86df
+                /* are not going to be used from the stack. Point SP to below   */
7e86df
+                /* the first of these SV*.                                      */
7e86df
+                SP -= iters;
7e86df
+                PUTBACK;
7e86df
+            }
7e86df
 	}
7e86df
 	else {
7e86df
-	    PUTBACK;
7e86df
-	    ENTER_with_name("call_PUSH");
7e86df
-	    call_sv(SV_CONST(PUSH),G_SCALAR|G_DISCARD|G_METHOD_NAMED);
7e86df
-	    LEAVE_with_name("call_PUSH");
7e86df
-	    SPAGAIN;
7e86df
+            PUTBACK;
7e86df
+            av_extend(ary,iters);
7e86df
+            av_clear(ary);
7e86df
+
7e86df
+            ENTER_with_name("call_PUSH");
7e86df
+            call_sv(SV_CONST(PUSH),G_SCALAR|G_DISCARD|G_METHOD_NAMED);
7e86df
+            LEAVE_with_name("call_PUSH");
7e86df
+            SPAGAIN;
7e86df
+
7e86df
 	    if (gimme == G_ARRAY) {
7e86df
 		SSize_t i;
7e86df
 		/* EXTEND should not be needed - we just popped them */
7e86df
-		EXTEND(SP, iters);
7e86df
+		EXTEND_SKIP(SP, iters);
7e86df
 		for (i=0; i < iters; i++) {
7e86df
 		    SV **svp = av_fetch(ary, i, FALSE);
7e86df
 		    PUSHs((svp) ? *svp : &PL_sv_undef);
7e86df
@@ -6389,13 +6401,12 @@ PP(pp_split)
7e86df
 	    }
7e86df
 	}
7e86df
     }
7e86df
-    else {
7e86df
-	if (gimme == G_ARRAY)
7e86df
-	    RETURN;
7e86df
-    }
7e86df
 
7e86df
-    GETTARGET;
7e86df
-    XPUSHi(iters);
7e86df
+    if (gimme != G_ARRAY) {
7e86df
+        GETTARGET;
7e86df
+        XPUSHi(iters);
7e86df
+     }
7e86df
+
7e86df
     RETURN;
7e86df
 }
7e86df
 
7e86df
diff --git a/t/op/split.t b/t/op/split.t
7e86df
index 14f9158..7f37512 100644
7e86df
--- a/t/op/split.t
7e86df
+++ b/t/op/split.t
7e86df
@@ -7,7 +7,7 @@ BEGIN {
7e86df
     set_up_inc('../lib');
7e86df
 }
7e86df
 
7e86df
-plan tests => 176;
7e86df
+plan tests => 182;
7e86df
 
7e86df
 $FS = ':';
7e86df
 
7e86df
@@ -648,6 +648,19 @@ is "@a", '1 2 3', 'assignment to split-to-array (stacked)';
7e86df
     is (+@a, 0, "empty utf8 string");
7e86df
 }
7e86df
 
7e86df
+# correct stack adjustments (gh#18232)
7e86df
+{
7e86df
+    sub foo { return @_ }
7e86df
+    my @a = foo(1, scalar split " ", "a b");
7e86df
+    is(join('', @a), "12", "Scalar split to a sub parameter");
7e86df
+}
7e86df
+
7e86df
+{
7e86df
+    sub foo { return @_ }
7e86df
+    my @a = foo(1, scalar(@x = split " ", "a b"));
7e86df
+    is(join('', @a), "12", "Split to @x then use scalar result as a sub parameter");
7e86df
+}
7e86df
+
7e86df
 fresh_perl_is(<<'CODE', '', {}, "scalar split stack overflow");
7e86df
 map{int"";split//.0>60for"0000000000000000"}split// for"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
7e86df
 CODE
7e86df
@@ -667,3 +680,11 @@ CODE
7e86df
         ok(eq_array(\@result,['a','b']), "Resulting in ('a','b')");
7e86df
     }
7e86df
 }
7e86df
+
7e86df
+# check that the (@ary = split) optimisation survives @ary being modified
7e86df
+
7e86df
+fresh_perl_is('my @ary; @ary = split(/\w(?{ @ary[1000] = 1 })/, "abc");',
7e86df
+        '',{},'(@ary = split ...) survives @ary being Renew()ed');
7e86df
+fresh_perl_is('my @ary; @ary = split(/\w(?{ undef @ary })/, "abc");',
7e86df
+        '',{},'(@ary = split ...) survives an (undef @ary)');
7e86df
+
7e86df
-- 
7e86df
2.25.4
7e86df