This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
give REGEXP SVs the POK flag again
authorDavid Mitchell <davem@iabyn.com>
Fri, 7 Jul 2017 13:13:32 +0000 (14:13 +0100)
committerDavid Mitchell <davem@iabyn.com>
Thu, 27 Jul 2017 10:30:21 +0000 (11:30 +0100)
commitdf6b4bd56551f2d39f7c0019c23f27181d8c39c4
tree760cb0b40c0f202fe06ae67e0a239156b92597fe
parente8f01ee5fa8087b34dfe31b2c53eef0ba8718922
give REGEXP SVs the POK flag again

Commit v5.17.5-99-g8d919b0 stopped SVt_REGEXP SVs (and PVLVs acting as
regexes) from having the POK and pPOK flags set. This made things like
SvOK() and SvTRUE() slower, because as well as the quick single test for
any I/N/P/R flags, SvOK() also has to test for

    (SvTYPE(sv) == SVt_REGEXP
     || (SvFLAGS(sv) & (SVTYPEMASK|SVp_POK|SVpgv_GP|SVf_FAKE))
 == (SVt_PVLV|SVf_FAKE))

This commit fixes the issue fixed by g8d919b0 in a slightly different way,
which is less invasive and allows the POK flag.

Background:

PVLV are basically PVMGs with a few extra fields. They are intended to
be a superset of all scalar types, so any scalar value can be assigned
to a PVLV SV.

However, once REGEXPs were made into first-class scalar SVs, this
assumption broke - there are a whole bunch of fields in a regex SV body
which can't be copied to to a PVLV. So this broke:

    sub f {
        my $r = qr/abc/; # $r is reference to an SVt_REGEXP
        $_[0] = $$r;
    }

    f($h{foo}); # the hash access is deferred - a temporary PVLV is
                # passed instead

The basic idea behind the g8d919b0 fix was, for an LV-acting-as-regex,
to attach both a PVLV body and a regex body to the SV head. This commit
keeps this basic concept; it just changes how the extra body is attached.

The original fix changed SVt_REGEXP SVs so that sv.sv_u.svu_pv no longer
pointed to the regexp's string representation; instead this pointer was
stored in a union made out of the xpv_len field. Doing this necessitated
not turning the POK flag on for any REGEXP SVs.

This freed up the sv_u to point to the regex body, while the sv_any field
could continue to point to the PVLV body. An ReANY() macro was introduced
that returned the sv_u field rather than the sv_any field.

This commit changes it so that instead, on regexp SVs (and LV-as-regexp
SVs), sv_u always points to the string buffer (so they can have POK set
again), but on specifically LV-as-regex SVs, the xpv_len_u union of the
PVLV body points to the regexp body.

This means that SVt_REGEXP SVs are now completely "normal" again,
and SVt_PVLV SVs are normal except in the one case where they hold a
regex, in which case rather than storing the string buffer's length, the
PVLV body stores a pointer to the regex body.
ext/B/B/Concise.pm
ext/Devel-Peek/t/Peek.t
inline.h
op.c
perl.h
regcomp.c
regexp.h
sv.c
sv.h