in reply to Why is using binmode on a file handle 77 times slower than just closing it and re-opening?

The problem is binmode doesn't change the layer of the filehandle, it adds a new layer.

Try appending the following line to your test:

warn PerlIO::get_layers($handle{$file});
And then try to count how many times the string encoding(utf-8-strict)utf8repeats. In my case, it was 922 ×
my @l = grep /utf-?8/, PerlIO::get_layers($handle{$file}); warn @l / 2;

To fix that, clear the layers before applying new ones by starting with :raw:

binmode $fh, ':raw:encoding(UTF-8)';
The result becomes agreeable:
Rate close noclose close 2137/s -- -28% noclose 2951/s 38% --
And the warning now shows a pretty low number.

Updated: Show how to count the repeated layers.

Update 2: On MSWin, you might need to add :crlf after :raw if needed, too.

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
  • Comment on Re: Why is using binmode on a file handle 77 times slower than just closing it and re-opening?
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: Why is using binmode on a file handle 77 times slower than just closing it and re-opening?
by Maelstrom (Beadle) on Nov 25, 2024 at 09:00 UTC
    Much thanks the universe makes sense again. The results got even better when I remembered to add the seek statement.