Does existential containers for protocol type use Copy on Write in Swift?

(This question was posted in stack overflow.)

I am considering use cases of both generics and protocol type in Swift.

The disadvantage of protocol type is heap allocation when the target is more than three words of memory footprint.

I want to avoid heap allocation for each function call, whose arguments include protocol type, in general. However, if existential containers use Copy on Write, heap allocation must not occur unless inout argument since caller has the original instance for arguments in stack or heap.

Does existential containers use Copy on Write in Swift or not?
Or does heap allocation occur when you call a function whose arguments include protocol type or not?

Thanks.

1 Like