The current documentation for the Range operator ..
states that it will not burn up memory for counting loops:
... The range operator is useful for writing
foreach (1..10)
loops and for doing slice operations on arrays. In the current implementation, no temporary array is created when the range operator is used as the expression in foreach loops, but older versions of Perl might burn a lot of memory when you write something like this:1. for (1 .. 1_000_000) { 2. # code 3. }
Because of the aforementioned early implementations of for (MIN .. MAX)
, I still come by experts who are wary of using counting loops because they believe it is equivalent to:
my @temp_array = (MIN .. MAX); # Needlessly using up memory
for (@temp_array) {
Versus the more logical and memory efficient:
for ($_ = MIN; $_ <= MAX; $_++) { # Logical counting from MIN to MAX
Questions:
Is there a way that one could go about proving that a counting loop does not waste memory?
Does anyone know which versions of Perl had the memory issue and when it was fixed?
I'm able to prove to myself that counting loops don't waste memory using the below one-liner which would certainly crash my system if it was actually creating a temporary array. However, it would be nice if there was more conclusive information on the subject so that we could put this old-wives tale to rest.
$ perl -e 'for (1 .. 1_000_000_000_000_000) { print "$_\n"; last if $_ == 5 }'
1
2
3
4
5
Solution
Each of the three below answers go part of the way to explaining this issue:
- ikegami's answer thoroughly breaks down different types loops and demonstrates how counting loops differ on the front and back end. ☆
- friedo's answer shows how to use
top
to monitor memory usage. - Borodin's answer addresses the second part of my query about when this no longer became an issue:
perlop v5.4_68
(released on 23 June 1998) warns a temporary array is used.perlop v5.4_69
(released on 29 June 1998) states a temporary array is no longer used.perldelta v5.4_71
(released on 9 July 1998) states that counting loops are optimized.
I might do some specific version testing at some point, but given this is apparently a 16 year old issue, I'm confident that the warning in perlop
can be put to rest.
First of all, here's a list of different types of
for
loops and the optimizations that can be applied. All of these are present in every version of Perl from 5.6 to 5.20 (present) inclusive, and I believe it's comprehensive.for (EXPR; EXPR; EXPR)
⇒ "C-style for loop", an augmented while loop.
for (EXPRX..EXPRY)
⇒ A range and nothing else is optimized into a counting loop.
for (@ARRAY)
⇒ An array and nothing else is accessed directly rather than being flattened.
for (reverse LIST)
⇒ None of the above optimizations apply, but the list is traversed in reverse rather than being reversed.
for (LIST)
⇒ In a generic foreach loop, the
LIST
expression is evaluated before the loop starts.When
CONSTX..CONSTY
is flattened (i.e. anywhere other than infor (CONSTX..CONSTY)
), it is flattened at compile-time rather than run-time.Black box proof
Baseline memory usage:
The general case flattens.
Or worse. (It flattens into an array at compile-time in addition to the normal stack usage.)
for (CONST..CONST)
doesn't flatten.In fact,
for (EXPR..EXPR)
in general doesn't flatten.Even without tools, you could tell the the difference in compilation time.
White box proof
The unoptimized case uses a range operator in list context. Full list in memory.
This is what a range flattened at compile-time looks like:
You can see that
for (CONST..CONST)
creates anenteriter
with the "S" flag. Onenteriter
, the "S" flag means it's a counting loop.Same for
for (EXPR..EXPR)
in general.Even
for (@a)
isn't flattened!Double-check
Looking up the code for the "S" flag will confirm all of this.
pp_enteriter
(PL_op->op_flags & OPf_STACKED
checks for "S")pp_iter
.The last time
perlop
says thisis in version
5.4_68
.In
5.4_69
(released on 29 June 1998) it changes to being very close to the current versionBut I can't find the change mentioned in a
perldelta
anywhere!Anyway, we're talking about a sixteen-year-old fix,
Is there a way that one could go about proving that a counting loop does not waste memory?
One way might be to monitor the process with
top
. For example:This shows that the memory usage remains fairly constant (and trivial) while the program runs, despite iterating a list of one trillion items.
Does anyone know which versions of Perl had the memory issue and when it was fixed?
I couldn't find the precise version when this was changed with a quick grep of the perldeltas. I will note that
perlop
mentions it:But it doesn't say when that optimization was introduced. In any case, it was a long time ago.