<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Interesting, I guess it’s anything with multiple possible function overloads then! It looks pretty trivial though to figure out the two components before trying to figure out which function, which should lead to the result in a reasonable time. There must be something else going on. Good work on filing the bug! Hope this gets fixed soon.<div class=""><br class=""></div><div class="">Geordie<br class=""><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">Am 06.06.2017 um 22:49 schrieb Jens Persson <<a href="mailto:jens@bitcycle.com" class="">jens@bitcycle.com</a>>:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">You're right that it's not functionally eq, sorry about that.<div class="">I filed <a href="https://bugs.swift.org/browse/SR-5093" class="">https://bugs.swift.org/browse/SR-5093</a> containing a reduced example program to demonstrate the issue..<div class="">Haven't tested it with anything later than the 2017-06-02 snapshot.</div><div class="">But it shows that it's not just max that is the problem.</div><div class="">/Jens</div></div></div><div class="gmail_extra"><br class=""><div class="gmail_quote">On Tue, Jun 6, 2017 at 4:51 AM, Geordie Jay <span dir="ltr" class=""><<a href="mailto:geojay@gmail.com" target="_blank" class="">geojay@gmail.com</a>></span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I doubt it'll be "fixed" in the next release (which came out less than 24 hours ago), but I gather type checker speed optimisations and fixing crashes are ongoing goals of the compiler team. With no guarantees or expectations, you may find your code compiles in an acceptable time with Swift 4 (Xcode 9).<br class=""><br class="">Cheers,<br class="">Geordie<div class="HOEnZb"><div class="h5"><br class=""><div class="gmail_quote"><div dir="ltr" class="">On Tue 6. Jun 2017 at 12:29, <<a href="mailto:www.hbu.cn@163.com" target="_blank" class="">www.hbu.cn@163.com</a>> wrote:<br class=""></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr" class=""><div class="gmail_default" style="font-family:simsun,serif;font-size:small">thanks, guys! dp array should be var, not let, but the compile run too slow. It indeed a bug. Optimizing the code for the compiling time is really a headache for coders. I am using the Xcode 8.1. Hoping it can be fixed in next release.</div></div><div class="gmail_extra"><br clear="all" class=""><div class=""><div class="m_-6105332695149802101m_-5767762612312787658gmail_signature" data-smartmail="gmail_signature"><blockquote class=""><i class=""><font face="georgia, serif" class=""><br class=""></font></i></blockquote><i class=""><font face="georgia, serif" class="">best wishes for you </font></i></div></div></div><div class="gmail_extra">
<br class=""><div class="gmail_quote">2017-06-06 8:57 GMT+08:00 Geordie J <span dir="ltr" class=""><<a href="mailto:geojay@gmail.com" target="_blank" class="">geojay@gmail.com</a>></span>:<br class=""><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=""><br class=""><div class=""><span class=""><blockquote type="cite" class=""><div class="">Am 06.06.2017 um 09:02 schrieb Jens Persson via swift-users <<a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a>>:</div><br class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class="">When compiling that from the command line, I get the following (after about 6 seconds):<br class=""></div><div class=""><br class=""></div><div class=""><div class="">test.swift:7:18: error: cannot assign through subscript: 'dp' is a 'let' constant</div><div class=""> dp[0][0] = 0</div><div class=""> ~~ ^</div><div class="">/.../</div></div><div class=""><br class=""></div><div class="">After fixing that (changing let dp to var dp), it will compile successfully, still taking a very long time though. This usually means that some expression(s) in the code happen to be a bit hard on the type checker (in its current state).</div><div class="">I tried the latest dev snapshot and it is a bit faster, perhaps 3 s instead of 6.</div><div class=""><br class=""></div><div class="">Anyway, here is a logically equivalent rewrite which will compile faster:</div><div class=""><br class=""></div><div class=""><div class="">class Solution {</div><div class=""> func rob(nums: [Int]) -> Int {</div><div class=""> guard nums.count > 0 else { return 0 }</div><div class=""> var dp = Array.init(repeating: Array.init(repeating: 0, count: nums.count),</div><div class=""> count: 2)</div><div class=""> dp[0][0] = 0</div><div class=""> dp[0][1] = nums[0]</div><div class=""> for i in 1 ..< nums.count {</div><div class=""> <span style="white-space:pre-wrap" class=""> </span>let dp_iMinus1_0 = dp[i - 1][0] </div><div class=""> <span style="white-space:pre-wrap" class=""> </span>let dp_iMinus1_1 = dp[i - 1][1] </div><div class=""> dp[i][0] = max(dp_iMinus1_0, dp_iMinus1_1)</div><div class=""> dp[i][1] = dp_iMinus1_0 + nums[i]</div></div></div></div></blockquote><div class=""><br class=""></div></span><div class="">Just a nitpick: this isn’t functionally equivalent to the original code (dp[i][1] = dp[i][0] + nums[i]), because dp[i][0] might have actually changed on the previous line. But you can return this line to the original version without any effect on the compile time (less than 250ms on my machine).</div><div class=""><br class=""></div><div class="">I think the compile time issue is that max() is a generic function that takes any Comparable, so the type checker seems to go berserk trying to ensure the term is satisfiable.</div><div class=""><br class=""></div><div class="">If you create a non-generic replacement for max in the same file:</div><div class=""><br class=""></div><div class=""><div class="">private func myMax(_ x1: Int, _ x2: Int) -> Int {</div><div class=""> return (x1 >= x2) ? x1 : x2</div><div class="">}</div></div><div class=""><br class=""></div><div class="">… and replace the assignment to <i class="">max(dp[i - 1][0], ……)</i> in the for loop with <i class="">myMax(dp[i - 1][0], ….)</i></div><div class=""><br class=""></div><div class="">The compile time will be just as fast as the one that swaps out the internal elements (on my machine it’s actually about 10% faster with the non-generic <i class="">max</i>).</div><div class=""><br class=""></div><div class="">Regards,</div><div class="">Geordie</div><span class=""><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class=""><div class=""> }</div><div class=""> return 0</div><div class=""> }</div><div class="">}</div></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div></div><div class="gmail_extra"><br class=""><div class="gmail_quote">On Mon, Jun 5, 2017 at 2:32 PM, Hbucius Smith via swift-users <span dir="ltr" class=""><<a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a>></span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr" class=""><div class="gmail_default" style="font-family:simsun,serif;font-size:small">Hi Swift-Users,</div><div class="gmail_default" style="font-family:simsun,serif;font-size:small"><br class=""></div><div class="gmail_default" style="font-family:simsun,serif;font-size:small"> when I compiled the code, Xcode cannot stop, I do not know why. It is very strange. Can anyone help ? Here is the code. I am using Xcode 8.1</div><div class="gmail_default" style="font-family:simsun,serif;font-size:small"><br class=""></div><div class="gmail_default" style="font-family:simsun,serif;font-size:small"><p class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-p1"><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s2">class</span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s1"> Solution {</span><br class=""><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s1"></span></p><p class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-p2"><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s1"> </span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s2">func</span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s1"> rob(nums: [</span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s3">Int</span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s1">]) -> </span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s3">Int</span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s1"> {</span></p><p class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-p2"><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s1"> </span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s2">guard</span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s1"> nums.count > </span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s4">0</span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s1"> </span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s2">else</span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s1"> { </span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s2">return</span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s1"> </span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s4">0</span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s1"> }</span></p><p class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-p2"><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s1"> </span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s2">let</span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s1"> dp = Array.</span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s2">init</span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s1">(repeating: Array.</span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s2">init</span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s1">(repeating: </span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s4">0</span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s1">, count: nums.count),</span></p><p class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-p2"><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s1"> count: </span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s4">2</span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s1">)</span></p><p class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-p2"><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s1"> dp[</span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s4">0</span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s1">][</span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s4">0</span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s1">] = </span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s4">0</span></p><p class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-p2"><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s1"> dp[</span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s4">0</span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s1">][</span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s4">1</span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s1">] = nums[</span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s4">0</span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s1">]</span></p><p class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-p2"><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s1"> </span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s2">for</span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s1"> i </span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s2">in</span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s1"> </span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s4">1</span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s1"> ..< nums.count {</span></p><p class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-p2"><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s1"> dp[i][</span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s4">0</span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s1">] = max(dp[i - </span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s4">1</span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s1">][</span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s4">0</span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s1">], dp[i - </span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s4">1</span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s1">][</span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s4">1</span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s1">])</span></p><p class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-p2"><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s1"> dp[i][</span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s4">1</span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s1">] = dp[i - </span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s4">1</span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s1">][</span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s4">0</span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s1">] + nums[i]</span></p><p class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-p2"><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s1"> }</span></p><p class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-p2"><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s1"> </span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s2">return</span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s1"> </span><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s4">0</span></p><p class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-p2"><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s1"> }</span></p><p class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-p2"><span class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail-s1">}</span></p></div><div class="gmail_default" style="font-family:simsun,serif;font-size:small"><br class=""></div><div class="gmail_default" style="font-family:simsun,serif;font-size:small"><br class=""></div><br clear="all" class=""><div class=""><div class="m_-6105332695149802101m_-5767762612312787658m_-2579627832302497835m_9138524867132845885gmail_signature"><i class=""><font face="georgia, serif" class="">best wishes for you </font></i></div></div>
</div>
<br class="">______________________________<wbr class="">_________________<br class="">
swift-users mailing list<br class="">
<a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank" class="">https://lists.swift.org/<wbr class="">mailman/listinfo/swift-users</a><br class="">
<br class=""></blockquote></div><br class=""></div>
______________________________<wbr class="">_________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-users" target="_blank" class="">https://lists.swift.org/<wbr class="">mailman/listinfo/swift-users</a><br class=""></div></blockquote></span></div><br class=""></div></blockquote></div><br class=""></div></blockquote></div>
</div></div></blockquote></div><br class=""></div>
</div></blockquote></div><br class=""></div></div></body></html>