in reply to returning large arrays from functions

Perl does not employ a copy-on-write mechanism. As such, assigning a one array to another (as in the second example) involves copying the elements, not just copying a reference to the elements.

In the first example, you're not even assigning an array to an array. You're getting a list of the elements in the array, then you're assigning that list to another array. You might end up copying each element twice! I know there's at least one optimisation in place, but there's no escaping copying each element at least once for the reason I gave above.

  • Comment on Re: returning large arrays from functions

Replies are listed 'Best First'.
Re^2: returning large arrays from functions
by Anonymous Monk on Nov 19, 2010 at 06:54 UTC

    Did the OP, or anyone, ever, suggest that Perl might use a "copy-on-write mechanism"?

      You seem to have misunderstood what I said. Whether copying occurs in an array to array assignment or not depends on whether Perl does COW or not. If it does, copying can be avoided. If it doesn't copying must occur. It doesn't, so copying must occur. And thus, the OP's question is answered.

        Perl could recognise that the contents of the internal array, once transfered to the external array, will be garbage collected, and avoid both the copying and the GC, by simple transferring the already allocated and populated internal storage of the internal array to the external array.

        COW does not come into it.