Nil coalescing operator exponential compile times

Originator:keithbsmiley
Number:rdar://23695200 Date Originated:30-Nov-2015 02:45 PM
Status:Open Resolved:
Product:Developer Tools Product Version:Swift 2.1
Classification:Other Bug Reproducible:Always
 
Summary:

When using Swift's nil coalescing operator, compile time seems to be
exponential based on the number of uses in a statement.

Steps to reproduce:

1. Attempt to compile the attached example

OR

1. Create a swift file with a single function that returns an optional
2. Chain ~10 calls to the function separated by `??`

Expected results:

The file is compiled in a reasonable amount of time

Actual results:

The compile takes an insane amount of time (on my 2014 rMPB the process
was killed after 59 minutes with 9 calls to the function). Seemingly
this compile time is exponential.

Comments

case where the issue doesn't happen

If the right hand side of ?? is non-optional, there is no compile time issue:

    _ = f(f(f(f(f(f(f(f(f("")
        ?? "")
        ?? "")
        ?? "")
        ?? "")
        ?? "")
        ?? "")
        ?? "")
        ?? "")
        ?? ""

So the workaround is only needed when the right-hand side of the operator is an optional.

By adigitalknight at Dec. 5, 2016, 4:57 p.m. (reply...)

workaround

Issue is still happening a year later with Xcode 8.1 and Swift 3.

Assigning each intermediate value to a constant will workaround compile speed issue.

    let a1 = f()
    let a2 = a1 ?? f()
    let a3 = a2 ?? f()
    let a4 = a3 ?? f()
    let a5 = a4 ?? f()
    let a6 = a5 ?? f()
    let a7 = a6 ?? f()
    let a8 = a7 ?? f()
    let a9 = a8 ?? f()
    let _ = a9 ?? f()
By adigitalknight at Dec. 5, 2016, 3:37 p.m. (reply...)

https://github.com/keith/radars/tree/master/NilCoalescingOperatorExponentialCompile

By keithbsmiley at Nov. 30, 2015, 10:47 p.m. (reply...)

Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!