[swift-evolution] Improved value and move semantics

Brent Royal-Gordon brent at architechies.com
Tue Aug 2 16:29:25 CDT 2016


> On Aug 2, 2016, at 12:06 PM, Dave Abrahams via swift-evolution <swift-evolution at swift.org> wrote:
> 
> If it says that, it's... not quite right.  There are things we could do
> to make some value copies more optimal.  For example, any value type
> containing multiple class references—or multiple other value types (such
> as arrays or strings or dictionaries) that contain class references—will
> cost more to copy than a single class reference does.  At the cost of
> some allocation and indirection, we could reduce the copying cost of
> such values.  It's an optimization we've considered making, but haven't
> prioritized.  
> 
> You can put a CoW wrapper around your value to do it manually.  I hacked
> one up using ManagedBuffer for someone at WWDC but I don't seem to have
> saved the code, sadly.

Slightly off-topic, but one day I would like to see `indirect` turned into a generalized COW feature:

* `indirect` can only be applied to a value type (or at least to a type with `mutating` members, so reference types would have to gain those).
* The value type is boxed in a reference type.
* Any use of a mutating member (and thus, use of the setter) is guarded with `isKnownUniquelyReferenced` and a copy.
* `indirect` can be applied to an enum case with a payload (the payload is boxed), a stored property (the value is boxed), or a type (the entire type is boxed).

Then you can just slap `indirect` on a struct whose copying is too complicated and let Swift transparently COW it for you. (And it would also permit recursive structs and other such niceties.)

-- 
Brent Royal-Gordon
Architechies



More information about the swift-evolution mailing list