写时复制会防止阵列上的数据重复吗? [英] Will copy-on-write prevent data duplication on arrays?

查看:36
本文介绍了写时复制会防止阵列上的数据重复吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 PHP 编写一个 Web API 客户端,它将 CSV 数据解析为关联数组,我想在使用这些数组时保护我的用户免受数据重复.

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).

现在我的问题是...如果我的用户将这些数组作为参数传递给方法,PHP 的写时复制机制是否会阻止数据重复,或者任何不明确接受对数组的引用的方法都会接收数组的完整副本?

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?

推荐答案

Copy on write 顾名思义是指在写入内容之前不会复制任何变量;只要传递的变量中没有一个字节发生变化,PHP 就会自动避免不必要的重复,而无需使用显式引用,这要归功于这种机制.

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.

这篇文章 详细解释了这是如何在 PHP 的源代码中实现的,正如文章所建议的,使用 xdebug 可以轻松检查变量是否与函数 xdebug_debug_zval 重复.

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.

此外,这个答案在 SO 上有更多关于 Copy-on-Write 的内容.

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

这篇关于写时复制会防止阵列上的数据重复吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆