[swift-users] are large values (not passed through a protocol) ever put on the heap?

Joe Groff jgroff at apple.com
Wed Jan 4 12:12:35 CST 2017


> On Jan 3, 2017, at 10:04 PM, Ray Fix via swift-users <swift-users at swift.org> wrote:
> 
> 
> There was a great talk at WWDC 2016 about internals and Swift performance.  https://developer.apple.com/videos/play/wwdc2016/416/ <https://developer.apple.com/videos/play/wwdc2016/416/>
> 
> At one point, Arnold Schwaighofer says, "Copying of large values incurs heap allocation.”  What isn’t clear to me here is if he is still referring to just existential container types or in just large values in general.
> 
> My understanding is a value can get put on the heap if:
> 
> 1) it is passed through a protocol and exceeds small size (3 words of storage)
> 2) it is captured by an escaping closure, and needs to extend lifetime
> 
> Suppose I make a large struct, and pass it as a parameter, is there a point where the compiler says, “Okay wise-guy, you are going on the heap now.”  Can this be detected with MemoryLayout<BigStruct>.size?  I have tried a couple experiments where I make beefy (1kB) structs in the playground but I never see the size suddenly dropping to a pointer size.
> 
> Any insights you have are greatly appreciated! 😃✨

No, Swift currently never implicitly puts parts of large structs on the heap. The heap allocation would come from the struct needing to be stored in something heap-allocated, such as a protocol container or capture box, like you said.

-Joe

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170104/2d6e9e3a/attachment.html>


More information about the swift-users mailing list