kroach has asked for the wisdom of the Perl Monks concerning the following question:
I'm using the $# feature to shrink an array. After shrinking to half the size, the array seems to take a bit more memory. How is that possible? (perl 5.10 on 64-bit GNU/Linux)
Output:use strict; use warnings; use feature qw(say); use Devel::Size qw(size); my @array = 1 .. 10_000; say size(\@array); $#array = 5_000; say size(\@array);
Is there a way to shrink the array without causing it to grow (and probably be reallocated)?
UPDATE: It seems splice doesn't cause the same effect.
my @array = 1 .. 10_000; say size(\@array); splice @array, 5_001; say size(\@array);
Furthermore, after running a few simple benchmarks, it turns out the splice method is about as fast as last index assignment, so it seems splice doesn't do unnecessary copying in void context.