Why is this closure slower the second time it is used?

Yes, I'm aware of that, and I usually avoid top level code, like for example in SR-7023, but as I just realized, an (ugly but interesting) workaround that worked for SR-7023, also works for SR-6983, so they are related, and they might well have originated from two different real world situations (followed by isolation/reduction-sessions) where the underlying issue was the same.

In the other test case, it doesn't matter for the result if you stick it in a function:

import AppKit

func test() {
    let a = Double(1)
    var checksum = UInt64(0)
    for _ in 0 ..< 5 {
        let t0 = CACurrentMediaTime()
        for _ in 0 ..< 1_000_000_000 {
            let v = a.nextDown
            checksum = checksum &+ v.bitPattern
        }
        let t1 = CACurrentMediaTime()
        print(t1 - t0, "seconds, checksum:", checksum)
    }
}
test()

It is still slow. I think I reduced the original issue to this form first, and then tried if it was the same if I removed the enclosing func, which it was, so I reported it like that because it was an even simpler program.


But it does make a difference for this test case.

1 Like