http://qs321.pair.com?node_id=675879


in reply to Re^6: references--hard vs anonymous operational weirdness
in thread references--hard vs anonymous operational weirdness

Thanks for your reply. I now understand where I went wrong. Just to try and reduce the degree of bafflement going round I'll explain my (admittedly erroneous) thinking.

On each iteration of the loop the (simplified) process is

  1. Allocate memory for @array
  2. Initialize @array
  3. Deallocate memory assigned to @array
Note: I am only talking about the memory allocated for @array (the AV struct), not the value of @array (the contents).

A smart compiler can optimize that loop so that the allocation and deallocation of the memory occur outside the loop, that is only once. Based on my observation of the actual memory allocation I had assumed that perl was optimizing the loop in just such a manner.

Perl, however, is not that simple and its loop would be more like
  1. Allocate memory for @array
  2. Initialize @array
  3. Deallocate memory assigned to @array if not referred to else where
I am speculating that means that perl can't decide until runtime if the memory slot can be reused which in turn means the optimization isn't possible, at least in the simplest form and may not be worth the overhead in any case.

So what I was seeing was a chunk of memory deallocated and then an immediate request for a chunk of the same type and the memory manager handed over the chunk just released.