Swift compilation time with nil coalescing operator

限于喜欢 提交于 2020-02-03 08:12:51

问题


After reading of the article about swift compiling time. I am interested in why usage of more than 2 sequence coalescing operator increase compilation time significantly.

Example: Compilation time 3.65 sec.

 func fn() -> Int {    
        let a: Int? = nil
        let b: Int? = nil
        let c: Int? = nil

        return 999 + (a ?? 0) + (b ?? 0) + (c ?? 0)
 }

Compilation time 0.09 sec.

func fn() -> Int {

    let a: Int? = nil
    let b: Int? = nil
    let c: Int? = nil

    var res: Int = 999

    if let a = a {
        res += a
    }

    if let b = b {
        res += b
    }

    if let c = c {
        res += c
    }

    return res
}  

回答1:


I'm almost certain that this has to do with type inference. When interpreting all of those + and ?? operators the compiler is doing a lot of work behind the scenes to infer the types of those arguments. There are around thirty overloads for the + operator alone and when you chain several of them together you are making the compiler's job much more complicated than you might think.



来源:https://stackoverflow.com/questions/37099982/swift-compilation-time-with-nil-coalescing-operator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!