<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><meta http-equiv="Content-Type" content="text/html charset=us-ascii" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Nov 30, 2016, at 6:25 PM, Jiho Choi via swift-dev &lt;<a href="mailto:swift-dev@swift.org" class="">swift-dev@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">Thanks for clarifications.&nbsp; I have a couple of follow-up questions.<div class=""><br class=""><div class="">1. Could you please provide more information (e.g. source code location) about the optimization applying non-atomic reference counting?&nbsp; What's the scope of the optimization?&nbsp; Is it method-based?</div><div class=""><br class=""></div><div class="">2. Looking at the source code, I assume Swift implements immediate reference counting (i.e. immediate reclamation of dead objects) &nbsp;without requiring explicit garbage collection phase for techniques, such as deferred reference counting or coalescing multiple updates.&nbsp; Is it right?&nbsp; If so, is there any plan to implement such techniques?</div></div></div></div></blockquote><div class=""><br class=""></div><div class="">We do coalesce multiple updates although late and at the LLVM level and in a very limited way. The current work in ARC that is being done is creating the ability to represent ARC pairings in SIL. This will allow us to perform ARC optimizations in a much simpler manner with a much simpler algorithm. This is the Semantic SIL work.</div><div class=""><br class=""></div><div class="">Michael</div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class=""><div class=""><br class=""><div class="gmail_quote"><div dir="ltr" class="">On Wed, Nov 30, 2016 at 11:41 AM John McCall &lt;<a href="mailto:rjmccall@apple.com" class="">rjmccall@apple.com</a>&gt; wrote:<br class=""></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class="gmail_msg"><div class="gmail_msg"><blockquote type="cite" class="gmail_msg"><div class="gmail_msg">On Nov 30, 2016, at 8:33 AM, Jiho Choi via swift-dev &lt;<a href="mailto:swift-dev@swift.org" class="gmail_msg" target="_blank">swift-dev@swift.org</a>&gt; wrote:</div><div class="gmail_msg"><div dir="ltr" class="gmail_msg">Hi,<div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">I am new to Swift, and I have several questions about how ARC works in Swift.</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">1. I read from one of the previous discussions in the&nbsp;swift-evolution list (<a href="https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160208/009422.html" class="gmail_msg" target="_blank">https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160208/009422.html</a>) that ARC operations are currently not atomic as Swift has no memory model and concurrency model.&nbsp; Does it mean that the compiler generates non-atomic instructions for updating reference counts (e.g. using incrementNonAtomic() instead of increment() in RefCount.h)?</div></div></div></blockquote><div class="gmail_msg"><br class="gmail_msg"></div></div></div><div style="word-wrap:break-word" class="gmail_msg"><div class="gmail_msg">No.&nbsp; We have the ability to do non-atomic reference counting as an optimization, but we only trigger it when we can prove that an object hasn't escaped yet.&nbsp; Therefore, at the user level, retain counts are atomic.</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">Swift ARC is non-atomic in the sense that a read/write or write/write race on an individual property/variable/whatever has undefined behavior and can lead to crashes or leaks.&nbsp; This differs from Objective-C ARC only in that a (synthesized) atomic strong or weak property in Objective-C does promise correctness even in the face of race conditions.&nbsp; But this guarantee is not worth much in practice because a failure to adequately synchronize accesses to a class's instance variables is likely to have all sorts of other unpleasant effects, and it is quite expensive, so we decided not to make it in Swift.</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg"></div></div><div style="word-wrap:break-word" class="gmail_msg"><div class="gmail_msg"><blockquote type="cite" class="gmail_msg"><div class="gmail_msg"><div dir="ltr" class="gmail_msg"><div class="gmail_msg">2. If not, when does it use non-atomic ARC operations? Is there an optimization pass to recognize local objects?</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">3. Without the concurrency model in the language, if not using GCD (e.g. all Swift benchmark applications), I assume Swift applications are single-threaded.&nbsp; Then, I think we can safely use non-atomic ARC operations.&nbsp; Am I right?</div></div></div></blockquote><div class="gmail_msg"><br class="gmail_msg"></div></div></div><div style="word-wrap:break-word" class="gmail_msg"><div class="gmail_msg">When we say that we don't have a concurrency model, we mean that (1) we aren't providing a more complete language solution than the options available to C programmers and (2) like C pre-C11/C++11, we have not yet formalized a memory model for concurrency that provides formal guarantees about what accesses are guaranteed to not conflict if they do race. &nbsp;(For example, we are unlikely to guarantee that accesses to different properties of a struct can occur in parallel, but we may choose to make that guarantee for different properties of a class.)</div></div><div style="word-wrap:break-word" class="gmail_msg"><div class="gmail_msg"><br class="gmail_msg"><blockquote type="cite" class="gmail_msg"><div class="gmail_msg"><div dir="ltr" class="gmail_msg"><div class="gmail_msg">4. Lastly, is there a way to measure the overhead of ARC (e.g. a compiler flag to disable ARC)?</div></div></div></blockquote><div class="gmail_msg"><br class="gmail_msg"></div></div></div><div style="word-wrap:break-word" class="gmail_msg">No, because ARC is generally necessary for correctness.</div><div style="word-wrap:break-word" class="gmail_msg"><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">John.</div></div></blockquote></div></div></div></div>
_______________________________________________<br class="">swift-dev mailing list<br class=""><a href="mailto:swift-dev@swift.org" class="">swift-dev@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-dev" class="">https://lists.swift.org/mailman/listinfo/swift-dev</a><br class=""></div></blockquote></div><br class=""></div></body></html>