From a902d92a78262a0fd111789742f9c9e2a7fa2f42 Mon Sep 17 00:00:00 2001 From: Richard Leach Date: Tue, 2 Apr 2024 15:36:45 +0000 Subject: [PATCH] Perl_op_convert_list: No unnecessary processing of unfolded CONSTs When Perl_op_convert_list is called to stringify a CONST OP, but the `op_folded` flag has not been passed in, a fair amount of activity occurs before the function returns the original CONST OP unchanged. After this commit, the CONST OP is returned directly. For example, prior to this commit, given the statement 'print "2\n', the function is passed a simple CONST OP: 1 const SVOP(0x55c99432c0b8) ===> [SELF] PARENT ===> [0x0] FLAGS = (SCALAR,SLABBED) SV = PV("2\n"\0) List context is forced upon it to yield: 2 list LISTOP(0x55c99432c048) ===> [0x0] PARENT ===> [0x0] FLAGS = (UNKNOWN,KIDS,SLABBED) | 3 +--pushmark OP(0x55c99432c088) ===> [SELF] | FLAGS = (SCALAR,SLABBED,MORESIB) | 1 +--const SVOP(0x55c99432c0b8) ===> [SELF] FLAGS = (SCALAR,SLABBED) SV = PV("2\n"\0) The pushmark is nullified and the parent type set: 2 stringify LISTOP(0x55c99432c048) ===> [0x0] PARENT ===> [0x0] FLAGS = (UNKNOWN,KIDS,SLABBED) | 3 +--null (ex-pushmark) OP(0x55c99432c088) ===> [SELF] | FLAGS = (SCALAR,SLABBED,MORESIB) | 1 +--const SVOP(0x55c99432c0b8) ===> [SELF] FLAGS = (SCALAR,SLABBED) SV = PV("2\n"\0) CHECKOP sets the number of non-null KIDS for the stringify: 2 stringify LISTOP(0x55c99432c048) ===> [0x0] PARENT ===> [0x0] FLAGS = (UNKNOWN,KIDS,SLABBED) PRIVATE = (0x1) | 3 +--null (ex-pushmark) OP(0x55c99432c088) ===> [SELF] | FLAGS = (SCALAR,SLABBED,MORESIB) | 1 +--const SVOP(0x55c99432c0b8) ===> [SELF] FLAGS = (SCALAR,SLABBED) SV = PV("2\n"\0) Then it is constant folded back to: 1 const SVOP(0x55c99432c0b8) ===> [SELF] PARENT ===> [0x0] FLAGS = (SCALAR,SLABBED) SV = PV("2\n"\0) --- op.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/op.c b/op.c index 3fc23eca49af..5c10e81ff350 100644 --- a/op.c +++ b/op.c @@ -5531,6 +5531,16 @@ Perl_op_convert_list(pTHX_ I32 type, I32 flags, OP *o) if (FEATURE_MODULE_TRUE_IS_ENABLED) flags |= OPf_SPECIAL; } + if (type == OP_STRINGIFY && OP_TYPE_IS(o, OP_CONST) && + !(flags & OPf_FOLDED) ) { + assert(!OpSIBLING(o)); + /* Don't wrap a single CONST in a list, process that list, + * then constant fold the list back to the starting OP. + * Note: Folded CONSTs do not seem to occur frequently + * enough for it to be worth the code bloat of also + * providing a fast path for them. */ + return o; + } if (!o || o->op_type != OP_LIST) o = force_list(o, FALSE); else