将写时复制可防止数组上的数据复制? [英] Will copy-on-write prevent data duplication on arrays?

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

问题描述

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



我的用户永远不会写这些数组(理论上他们可能在实践中没有任何意义)。



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

解决方案

按名称建立复制意味着在写东西之前没有复制变量;只要传递的变量不改变单个字节,PHP就会自动避免不必要的重复,并且不需要使用明确的引用。



这篇文章详细解释了如何在PHP的源代码中实现,如文章所示,使用xdebug可以轻松地检查变量不会与函数xdebug_debug_zval重复。



另外,这里的这个答案还有更多的写入副本。


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

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

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.

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

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

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