<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div>Great question!</div><div id="AppleMailSignature"><br></div><div id="AppleMailSignature">I'm using Redux in my Swift app too! My state struct is tiny though.&nbsp;</div><div id="AppleMailSignature"><br></div><div id="AppleMailSignature">Anyway, just wanted to say hello and welcome to the Redux club. :)</div><div id="AppleMailSignature"><br></div><div id="AppleMailSignature">R+</div><div id="AppleMailSignature"><br>Sent from my iPhone</div><div><br>On 22 Jan 2016, at 22:52, Daniel Tartaglia via swift-users &lt;<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>&gt; wrote:<br><br></div><blockquote type="cite"><div><meta http-equiv="Content-Type" content="text/html charset=utf-8"><blockquote type="cite" class="">On Jan 22, 2016, at 4:35 PM, Joe Groff &lt;<a href="mailto:jgroff@apple.com" class="">jgroff@apple.com</a>&gt; wrote:<br class=""></blockquote><div><blockquote type="cite" class=""><br class="Apple-interchange-newline"><div class=""><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><blockquote type="cite" class=""><div class=""><br class="Apple-interchange-newline">On Jan 22, 2016, at 11:22 AM, Daniel Tartaglia via swift-users &lt;<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div class="">If I have a large struct with lots of sub-structs and I assign to just one field of one of the sub-structs, will the system make a deep copy of the struct or a shallow copy where the unmodified portions of the object still point the same memory as the original struct?</div><div class=""><br class=""></div><div class="">In other words, given this code:</div><div class=""><br class=""></div><div class=""><div class="" style="margin: 0px; line-height: normal; font-family: Menlo;"><div class="" style="margin: 0px; line-height: normal;"><span class="" style="color: rgb(187, 44, 162);">struct</span><span class="Apple-converted-space">&nbsp;</span>SubStruct {</div><div class="" style="margin: 0px; line-height: normal;">&nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(187, 44, 162);">var</span><span class="Apple-converted-space">&nbsp;</span>a:<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(112, 61, 170);">Int</span><span class="Apple-converted-space">&nbsp;</span>=<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(39, 42, 216);">0</span></div><div class="" style="margin: 0px; line-height: normal;">&nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(187, 44, 162);">var</span><span class="Apple-converted-space">&nbsp;</span>b:<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(112, 61, 170);">Int</span><span class="Apple-converted-space">&nbsp;</span>=<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(39, 42, 216);">0</span></div><div class="" style="margin: 0px; line-height: normal;">}</div><div class="" style="margin: 0px; line-height: normal; min-height: 14px;"><br class=""></div><div class="" style="margin: 0px; line-height: normal;"><span class="" style="color: rgb(187, 44, 162);">struct</span><span class="Apple-converted-space">&nbsp;</span>VeryLarge {</div><div class="" style="margin: 0px; line-height: normal;">&nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(187, 44, 162);">var</span><span class="Apple-converted-space">&nbsp;</span>subStructA =<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(79, 129, 135);">SubStruct</span>()</div><div class="" style="margin: 0px; line-height: normal;">&nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(187, 44, 162);">var</span><span class="Apple-converted-space">&nbsp;</span>subStructB =<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(79, 129, 135);">SubStruct</span>()</div><div class="" style="margin: 0px; line-height: normal;">&nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(187, 44, 162);">var</span><span class="Apple-converted-space">&nbsp;</span>subStructC =<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(79, 129, 135);">SubStruct</span>()</div><div class="" style="margin: 0px; line-height: normal; color: rgb(0, 132, 0);"><span class="">&nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span></span>// lots of other stuff</div><div class="" style="margin: 0px; line-height: normal;">}</div><div class="" style="margin: 0px; line-height: normal; min-height: 14px;"><br class=""></div><div class="" style="margin: 0px; line-height: normal; color: rgb(79, 129, 135);"><span class="" style="color: rgb(187, 44, 162);">func</span><span class=""><span class="Apple-converted-space">&nbsp;</span>bar(</span><span class="" style="color: rgb(187, 44, 162);">var</span><span class=""><span class="Apple-converted-space">&nbsp;</span>vl:<span class="Apple-converted-space">&nbsp;</span></span>VeryLarge<span class="">) -&gt;<span class="Apple-converted-space">&nbsp;</span></span>VeryLarge<span class=""><span class="Apple-converted-space">&nbsp;</span>{</span></div><div class="" style="margin: 0px; line-height: normal;">&nbsp; &nbsp; vl.<span class="" style="color: rgb(79, 129, 135);">subStructA</span>.<span class="" style="color: rgb(79, 129, 135);">a</span><span class="Apple-converted-space">&nbsp;</span>=<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(39, 42, 216);">5</span></div><div class="" style="margin: 0px; line-height: normal;">&nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(187, 44, 162);">return</span><span class="Apple-converted-space">&nbsp;</span>vl</div><div class="" style="margin: 0px; line-height: normal;">}</div><div class="" style="margin: 0px; line-height: normal; min-height: 14px;"><br class=""></div><div class="" style="margin: 0px; line-height: normal;"><span class="" style="color: rgb(187, 44, 162);">let</span><span class="Apple-converted-space">&nbsp;</span>vl1 =<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(79, 129, 135);">VeryLarge</span>()</div><div class="" style="margin: 0px; line-height: normal;"><span class="" style="color: rgb(187, 44, 162);">let</span><span class="Apple-converted-space">&nbsp;</span>vl2 =<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(49, 89, 93);">bar</span>(<span class="" style="color: rgb(79, 129, 135);">vl1</span>)</div><div class=""><br class=""></div></div></div><div class="" style="margin: 0px; line-height: normal; font-family: Menlo; min-height: 14px;">Will vl2.subStructB be a copy of vl1.subStructB, or an entirely new object?</div><div class=""><br class=""></div><div class="">I’m worried about performance when making small changes to large objects.</div></div></div></blockquote><br class=""></div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">Semantically, vl2 is always a distinct value, though how that ends up manifesting depends on the optimizer. By default, struct fields are stored in-line, like C structs, so sizeof(VeryLarge) will be sizeof(SubStruct) * 3 + sizeof(lots of other stuff), and a copy will be a full copy. If you want copy-on-write behavior, you currently have to implement it yourself, or build from already-implemented COW value types like Array. When structs pass a certain size threshold, the calling convention changes over to passing and returning them indirectly, though 'bar' will still naively introduce a copy to transfer the modified value from the argument to the result buffer. However, in optimized builds, if bar is inlined, then I'd expect this to ultimately reduce down to an in-place modification without copies, since vl1 is never used after assigning vl2.</div></div></blockquote><br class=""></div><div>Thanks for the reply. I’m asking because I am considering using a Redux system for an app I’m working on. One of the basic principles of Redux is that the entire app state be held in one struct, and I was worried about the system making a deep copy of the state struct every time I change any little variable.</div><div class=""><br class=""></div><br class=""></div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>swift-users mailing list</span><br><span><a href="mailto:swift-users@swift.org">swift-users@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-users">https://lists.swift.org/mailman/listinfo/swift-users</a></span><br></div></blockquote></body></html>