http://www.perlmonks.org?node_id=11157795


in reply to RE on lines read from in-memory scalar is very slow

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)