php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #76150 Don't trigger copy-on-write when assigning same value
Submitted: 2018-03-26 07:44 UTC Modified: -
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: nicolas dot grekas+php at gmail dot com Assigned:
Status: Open Package: Scripting Engine problem
PHP Version: master-Git-2018-03-26 (Git) OS:
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2018-03-26 07:44 UTC] nicolas dot grekas+php at gmail dot com
Description:
------------
When the key of an array is assigned an exact same value, copy-on-write is triggered, while this could be avoided.

By "exact same", I mean "exact same memory address" - not "===" (which could require an array traversal to run the comparison).

Here is an example in Symfony where this logic is implemented in userland (but has to use `===` because there is no other way to do so in userland), and where it makes a difference when measuring the memory usage:
https://github.com/symfony/symfony/blob/4a602ca6529090197e09555796b27500111aee31/src/Symfony/Component/DependencyInjection/Compiler/AbstractRecursivePass.php#L60-L62

Test script:
---------------
$a = $someExistingArray;

foreach ($a as $k => $v) {
    // [...] conditionnaly process $v, let's say it were unaffected;
    // then reassigning triggers COW, even if $v points to the exact same memory address/object

    $a[$k] = $v;
}

Expected result:
----------------
no COW


Patches

Add a Patch

Pull Requests

Add a Pull Request

 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Sat Nov 28 08:01:23 2020 UTC