Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Regex match is very slow against deref'd reference to substr/lvalue, is it normal?

by dave_the_m (Monsignor)
on Aug 12, 2024 at 07:17 UTC ( [id://11161011]=note: print w/replies, xml ) Need Help??


in reply to Regex match is very slow against deref'd reference to substr/lvalue, is it normal?

It's nothing to do with regexes and everything to do with lvalue substr. Taking a reference to substr doesn't create a new string, it just creates a special magical object with a pointer to the original string and the values of the substr arguments. When $xref is derefererenced, it triggers the magic which makes an actual string based on the values. Since you have $$xref in a loop, that causes a temporary copy of the substring to be made and thrown away 40,000 times.

Dave.

  • Comment on Re: Regex match is very slow against deref'd reference to substr/lvalue, is it normal?

Replies are listed 'Best First'.
Re^2: Regex match is very slow against deref'd reference to substr/lvalue, is it normal?
by Anonymous Monk on Aug 12, 2024 at 10:21 UTC

    Thanks. No regexes involved, then. My confusion, partly, was because the "magical object" (MO), in addition to attributes you mention, also keeps value of global anchor position between global match invocations, even though temporary buffer is thrown away each time, as you explained. So, MO is smart enough to notice if referent has been changed to reset this anchor. But not smart enough to somehow employ COW (if it observes referent for changes anyway) to avoid physical copy on every dereference? I thought Perl scalar keeps offset and actual length of string, even though physical buffer may extend on either side? I.e. same as result of substr. OK, perhaps reality of MO is more complex than my simple model above, thanks for your answer, again.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2024-09-13 19:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The PerlMonks site front end has:





    Results (21 votes). Check out past polls.

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.