Will copy-on-write prevent data duplication on arrays?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 12:31:05

问题


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?


回答1:


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.




回答2:


If you don't change them, the arrays will not be copied.



来源:https://stackoverflow.com/questions/11074970/will-copy-on-write-prevent-data-duplication-on-arrays

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!