Will copy-on-write prevent data duplication on arrays?
Asked Answered
K

2

13

I am programming a web API client in PHP that parses CSV data into associative arrays and I want to protect my users from data-duplication when using these arrays.

My users will never be writing to these arrays (theoretically they could but it makes no sense in practice).

Now my question is... if my users pass these arrays around as arguments to methods, will PHP's copy-on-write mechanism prevent data-duplication or will any method that doesn't explicitly accept a reference to an array receive a complete copy of the array?

Keyway answered 17/6, 2012 at 21:34 Comment(0)
M
16

Copy on write as the name suggests means no variable is being copied until something is written; as long as not a single byte is changed in the variable passed around, PHP takes care of avoiding unnecesary duplicates automatically and without the need of using explicit references thanks to this mechanism.

This article explains in detail how is this implemented in the source code of PHP, and as the article suggests, using xdebug one can easily check the variables are not being duplicated with the function xdebug_debug_zval.

Additionally this answer here on SO has more on Copy-on-Write.

Metaphysic answered 17/6, 2012 at 22:18 Comment(2)
I have an additional question. What happens if you say: $a=array(...); $b=$a; $b[4000].="x"; Will the whole array be duplicated or just the one element with index 4000?Ras
So to answer my own question, the whole array will not be duplicated just that one index, you can see this with the code: php -r '$a=str_repeat("a",1024*1024*1024);$b=str_repeat("b",1024*1024*1024);$a=array($a,$b);$b=$a;$b[1].="x";echo "done\n";sleep(1000000);' while it is sleeping you can check with your favorite system monitor that it only uses 3GB of ram (would be 4 if the whole array were duplicated).Ras
L
2

If you don't change them, the arrays will not be copied.

Lorrin answered 17/6, 2012 at 21:35 Comment(1)
Thanks for your prompt answer, if you have the time I would be greatly thankful if you would elaborate a little more on the matter. Will the receiving arrays be working with pointers? Or could you maybe provide a link to more information that corroborates your answer?Keyway

© 2022 - 2024 — McMap. All rights reserved.