Swift中哪些值类型支持写时复制? [英] Which value types in Swift supports copy-on-write?
问题描述
我在此处中了解了有关Array的写时复制实现的信息。 / p>
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.
推荐答案
String
支持复制写操作,并且所有集合类型- Array
, Dictionary
和 Set
。
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.
这篇关于Swift中哪些值类型支持写时复制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!