On Tue, Oct 02, 2001 at 09:16:50PM -0600, Chris Fedde wrote:
> Found some more recomendations for the memory usage stuff. I don't
> recall if this part had been made available for comment.
>
> thanks
>
> Index: perlfaq3.pod
> ===================================================================
> RCS file: /home/perlcvs/perlfaq/perlfaq3.pod,v
> retrieving revision 1.4
> diff -u -r1.4 perlfaq3.pod
> --- perlfaq3.pod 2001/10/02 19:42:02 1.4
> +++ perlfaq3.pod 2001/10/03 03:09:55
> @@ -519,6 +519,51 @@
> way you do it, but it makes a huge difference when they start getting
> larger.
>
> +=item * Use map and grep selectively
> +
> +Remember that both map and grep expect a LIST argument, so doing this:
> +
> + @wanted = grep {/pattern/} <FILE>;
> +
> +will cause the entire file to be slurped. For large files, it's better
> +to loop:
> +
> + while (<FILE>) {
> + push(@wanted, $_) if /pattern/;
> + }
Saying "it is better" is too absolute. It *may* be better, but that
depends on the percentages of matches. If you have 100,000 lines, and
99,990 will match, you might prefer the grep over 99,990 pushes.
> +=item * Avoid unnecessary quotes and stringification
> +
> +Don't quote large strings unless absolutely necessary:
> +
> + my $copy = "$large_string";
> +
> +makes 2 copies of $large_string (one for $copy and another for the
> +quotes), whereas
> +
> + my $copy = $large_string;
> +
> +only makes one copy.
Too many implementation details. From a language aspect, the double copy
is something that is irrelevant; in fact, in a new version of Perl this
problem might no longer exist.
But you miss out the reason people have warned against overquoting
in clpm for many years. It's an *engineering* reason. The code forces
stringification. Which is ok for strings and numerals, but a problem for
references; as it cannot be reversed. And it's a problem that's there
regardless of the size of the scalar.
> +Ditto for stringifying large arrays:
> +
> + {
> + local $, = "\n";
> + print @big_array;
> + }
> +
> +is much more memory-efficient than either
> +
> + print join "\n", @big_array;
> +
> +or
> +
> + {
> + local $" = "\n";
> + print "@big_array";
> + }
Implementation issues that should not appear in the FAQ. If it needs
mentioning anywhere, it's in the TODO file.
In fact, the FAQ should mention these methods to be *equivalent*.
Because, that's what they are.
Abigail