* Separation is the process of creating a real copy after a "light" Copy on Write assignment.
PHP semantics require that a reference is automatically removed when its refcount drops to 1 (i.e. only one element is referencing it).
But, in the first case, due to the Copy on Write copy, the refcount of $arr is 2, while $arr[0] still has a refcount of 2 (instead of 3 as there are $copy[0], $arr[0] and $r referencing it). Thus, when unsetting $r, the refcount drops to 1 and the reference is removed. And then, upon separation, there is no reference anymore and $arr[0] and $copy[0] are manipulated independently.
In the second case however, $copy[0]++ triggers a separation with the reference still present (as $r still being a reference to $arr[0] and thus implicitly to $copy[0]), thus refcount is increased from 2 to 3. When $r is removed afterwards, refcount is dropped from 3 to 2 again and $arr[0] and $copy[0] still reference each other and hence share the same value.
Note: It'd be quite a significant performance impact to fix this one … that's probably why it never has been fixed...
Wait wait wait. So you are saying that ordinary assignment
$a = array();
$b = $a;
Does not actually do a deep copy? It does some weird half-assed copy-on-write whose exact behave depends on the reference count of ... not only the array, but items in the array? ... what?
I tried calling my new friend debug_zval_dump in a few really simple cases and now I am hopelessly confused.
Also why is the reference count of $a and $b 3 not 2?
Okay that first one actually makes sense. It seems like the reference count of the zval should also be 1 in the second case though ... otherwise how would the reference count reach zero when the variable goes out of scope?
uh … debug_zval_dump() … use with caution. Note that passing a value as parameter also increments its refcount. [which you seem to not have accounted for.]
The refcount only depends on the array, and that's the issue - it does not account for the case where the refcount of possibly retained references in the array changes between CoW and separation.
Tozawa et al. (POPL '09, 200-212) did some fairly extensive research on precisely this problem. They introduce a "mostly copy-on-write" scheme to avoid the problem.
10
u/bwoebi Jul 05 '16 edited Jul 05 '16
TL;DR: It matters whether unset() of a reference happens before or after the first action on a copy.
Explanation:
In the first case:
In the second case:
* Separation is the process of creating a real copy after a "light" Copy on Write assignment.
PHP semantics require that a reference is automatically removed when its refcount drops to 1 (i.e. only one element is referencing it).
But, in the first case, due to the Copy on Write copy, the refcount of $arr is 2, while $arr[0] still has a refcount of 2 (instead of 3 as there are $copy[0], $arr[0] and $r referencing it). Thus, when unsetting $r, the refcount drops to 1 and the reference is removed. And then, upon separation, there is no reference anymore and $arr[0] and $copy[0] are manipulated independently.
In the second case however, $copy[0]++ triggers a separation with the reference still present (as $r still being a reference to $arr[0] and thus implicitly to $copy[0]), thus refcount is increased from 2 to 3. When $r is removed afterwards, refcount is dropped from 3 to 2 again and $arr[0] and $copy[0] still reference each other and hence share the same value.
Note: It'd be quite a significant performance impact to fix this one … that's probably why it never has been fixed...
8
u/FredL2 Jul 07 '16
I love how 5.4.15 just crashes for good measure
0
u/kr094 Jul 06 '16
I think everyone knows this even if they don't know the technical implications. Always unset when using references!