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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
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

Pull Requests

 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jan 07 12:01:29 2025 UTC