Skip to content

Commit cd97dc8

Browse files
committed
optimise bare 'next'
If a 'next' without a label appears directly in the scope of the current loop, then skip searching the context stack for a suitable LOOP context.
1 parent 31705cd commit cd97dc8

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

pp_ctl.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2546,7 +2546,10 @@ PP(pp_next)
25462546
{
25472547
PERL_CONTEXT *cx;
25482548

2549-
cx = S_unwind_loop(aTHX);
2549+
/* if not a bare 'next' in the main scope, search for it */
2550+
cx = CX_CUR();
2551+
if (!((PL_op->op_flags & OPf_SPECIAL) && CxTYPE_is_LOOP(cx)))
2552+
cx = S_unwind_loop(aTHX);
25502553

25512554
TOPBLOCK(cx);
25522555
PL_curcop = cx->blk_oldcop;

t/perf/benchmarks

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,6 +1030,12 @@
10301030
code => 'for (@a) {}',
10311031
},
10321032

1033+
'loop::for::next4' => {
1034+
desc => 'for loop containing only next with my var and integer 4 array',
1035+
setup => 'my @a = (1..4);',
1036+
code => 'for my $x (@a) {next}',
1037+
},
1038+
10331039
'loop::while::i1' => {
10341040
desc => 'empty while loop 1 iteration',
10351041
setup => 'my $i = 0;',

0 commit comments

Comments
 (0)