Which value types in Swift supports copy-on-write?

我们两清 提交于 2019-12-19 07:16:10

问题


I read about copy-on-write implementation for Array in Swift here.

Arrays, like all variable-size collections in the standard library, use copy-on-write optimization. Multiple copies of an array share the same storage until you modify one of the copies. When that happens, the array being modified replaces its storage with a uniquely owned copy of itself, which is then modified in place. Optimizations are sometimes applied that can reduce the amount of copying.

I was wondering if you have any information about which structure supports copy-on-write.


回答1:


Copy-on write is supported for String and all collection types - Array, Dictionary and Set.

Besides that, compiler is free to optimize any struct access and effectively give you copy-on-write semantics, but it is not guaranteed.



来源:https://stackoverflow.com/questions/45253202/which-value-types-in-swift-supports-copy-on-write

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