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.