From 72d707b0d0f36baadca8b5c3958e2c1080f1cdec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADt=20Str=C3=A1dal?= Date: Wed, 9 Oct 2024 12:16:02 +0200 Subject: [PATCH] reduce allocating big zeroes ndarray in mapflex - #501 --- IO/FlexRaw/FlexRaw.pm | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/IO/FlexRaw/FlexRaw.pm b/IO/FlexRaw/FlexRaw.pm index 4d9edf94d..169c9b49f 100644 --- a/IO/FlexRaw/FlexRaw.pm +++ b/IO/FlexRaw/FlexRaw.pm @@ -598,9 +598,7 @@ sub mapflex { } else { barf "Bad typename '$type' in mapflex" if !defined $flextypes{$type}; $type = $flextypes{$type}; - my $si = 1; - $si *= $_ for ref $hdr->{Dims} ? @{$hdr->{Dims}} : $hdr->{Dims}; - $size += $si * PDL::Core::howbig ($type); + $size += _data_size_in_bytes($type, $hdr->{Dims}); } } # $s now contains estimated size of data in header -- @@ -671,6 +669,13 @@ sub mapflex { wantarray ? @out : $out[0]; } +sub _data_size_in_bytes { + my ($type, $dims) = @_; + my $si = 1; + $si *= $_ for ref $dims ? @$dims : $dims; + $si * PDL::Core::howbig($type); +} + =head2 writeflex =for ref