Unintentional Swift struct copy and destroy
<p>Can you help me find out why unboxing and accessing a property of a swift struct would lead to a copy and destroy of said struct? The struct is quite large, and since this happens in a loop, this unexpected behaviour drains quite a bit of performance.</p>

<p>A co-worker and I have written an open source library <a href="https://github.com/skylib/SnapPageableArray" rel="nofollow noreferrer">SnapPageableArray</a> that contains an Example project that exhibits this behaviour. The example uses the SnapPageableArray as a data set backing a collection view, and every 5 seconds it <a href="https://github.com/skylib/SnapPageableArray/blob/master/Example/ViewController.swift#L21" rel="nofollow noreferrer">adds a bit more data</a>. According to Instruments, the topup causes <a href="https://github.com/skylib/SnapPageableArray/blob/master/SnapPageableArray/PageableArray.swift#L50" rel="nofollow noreferrer">line 50 of PageableArray</a> to take a very long time, and it indicates that at this place LocalData is copied and destroyed:</p>

<p><a href="https://i.stack.imgur.com/sO0Xw.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/sO0Xw.png" alt="Code from Instruments"></a>
<a href="https://i.stack.imgur.com/rEVA8.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rEVA8.png" alt="Trace from Instruments"></a>
(<a href="https://www.dropbox.com/s/tkorukcku1bpy0l/FullInstrumentsScreen.png?dl=0" rel="nofollow noreferrer">full Instruments screen here</a>)</p>

<p><a href="https://github.com/skylib/SnapPageableArray/blob/master/SnapPageableArray/PageableArray.swift#L7" rel="nofollow noreferrer">self.elements</a> is an array of the class <a href="https://github.com/skylib/SnapPageableArray/blob/master/SnapPageableArray/ElementWithState.swift" rel="nofollow noreferrer">ElementWithState&lt;T&gt;</a> where T is my struct LocalData, and the properties in ElementWithState are a state enum and the element in question (the LocalData struct).</p>

<p>Why is my struct copied? I unbox and look up references only, there is no assignment, right?</p>

<p>What can I do to avoid the copying and following destruction?</p>

<p>A few PS'es:</p>

<ul>
<li><a href="https://github.com/skylib/SnapPageableArray/blob/master/Example/LocalData.swift" rel="nofollow noreferrer">LocalData</a> is a large struct equal to structs we use in our app. We
would like to not convert it into a class as the struct is a better
fit for the rest of our application</li>
<li>The full Instruments trace is <a href="https://www.dropbox.com/s/yzqnjrwy4kdx3s5/Instruments8.trace.zip?dl=0" rel="nofollow noreferrer">available here</a></li>
<li>The trace was done using an iPhone 5, but the issue is there just as much on the iPhone 6S and iPad Pro</li>
</ul>