Just a summary, this hasn't been fixed yet.

There's three things contributing to this problem:

  1. when reading from file, sv_gets(), which is used to implement readline(), preallocates the SV to the size of the file handle's buffer, and for an in-memory file, the in-memory file is the buffer, so 16M in this case.
  2. on a match the regular expression engine makes a copy of the input string, and even in cases where perl normally wouldn't make a copy-on-write copy of the input string it does here
  3. Cygwin is very slow in updating the final byte of the large SV buffer (the PV), which holds the CoW reference count. This may be due to the way VirtualAlloc() is used to implement malloc on Cygwin.
1. means we get that huge allocation for the SV, 2. means we try to make it a CoW SV, and 3 produces the horrible performance. As an incidental effect, 2 making the SV CoW means if you save a copy of the SV:
my @save; while (<$fh>) { /successful (match with captures)/; # mark the SV as CoW push @save, $_; # extend the lifetime of the SV's PV until removed +from @save }

the memory use of the program explodes (my machine with 32G ran out of memory testing this).

I have a WIP fix for 2, but it unfortunately leaks in ASAN tests, so I haven't made a PR for it (it does prevent the performance problem)


In reply to Re: RE on lines read from in-memory scalar is very slow by tonyc
in thread RE on lines read from in-memory scalar is very slow by Danny

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.