Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^2: Are beheaded strings known to be slow?

by ikegami (Patriarch)
on Oct 10, 2025 at 15:22 UTC ( [id://11166457]=note: print w/replies, xml ) Need Help??


in reply to Re: Are beheaded strings known to be slow?
in thread Are beheaded strings known to be slow?

That's what it is. Regex matching makes a copy of the scalar for use by $&, $1, etc. It uses the COW mechanism if possible. It doesn't for OOK scalars.

After one pass of test1's loop:

SV = PVMG(0x5bb22b8c3b80) at 0x5bb22b9beae8 REFCNT = 1 FLAGS = (SMG,POK,IsCOW,pPOK) <-- IsCOW: String buffer is shared IV = 0 NV = 0 PV = 0x5bb22b9ea230 "..." CUR = 99999 LEN = 100001 COW_REFCNT = 1 MAGIC = 0x5bb22b9c48f0 MG_VIRTUAL = &PL_vtbl_mglob MG_TYPE = PERL_MAGIC_regex_global(g) MG_FLAGS = 0x40 BYTES MG_LEN = -1

After one pass of test2's loop:

SV = PVMG(0x5bb22b8c3bb0) at 0x5bb22b9cc1f0 REFCNT = 1 FLAGS = (SMG,POK,OOK,pPOK) <-- !IsCOW: String buffer isn't shared IV = 0 NV = 0 OFFSET = 1 PV = 0x5bb22ba028e1 ( "\x01" . ) "..." CUR = 99999 LEN = 100001 MAGIC = 0x5bb22b9c48f0 MG_VIRTUAL = &PL_vtbl_mglob MG_TYPE = PERL_MAGIC_regex_global(g) MG_FLAGS = 0x40 BYTES MG_LEN = -1

That means a copy of the string buffer must have been made. By the end of the loop, a copy has been done 99,999 times (once per successful regex match).

You can run into the same issue with foreign string buffers (e.g. a memory-mapped file). See why is Perl File::Map so slow compared to File::Slurp?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11166457]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2026-02-18 19:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.