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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: nicolas dot grekas+php at gmail dot com
New email:
PHP Version: OS:

 

 [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-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 16:01:27 2024 UTC