Wednesday, October 10, 2018

Swift Nil-coalescing Performance Trap

Ben Cohen (via Ole Begemann):

?? [] is a significant performance and correctness trap.

Not because [] creates an array unnecessarily (it doesn’t, the empty array is a static singleton in the standard library via a performance hack that gives me heartburn).

It’s because when the array isn’t nil, the presence of ?? [] affects the type checker in ways you don’t expect[…]

[…]

So what does maybeHugeRange?.reversed() ?? [] do? The ReversedCollection answer won’t type check, because the rhs of ?? can’t be one. So instead it falls back to the version on forward-only collections. That returns an array. So now, just because of that ?? [], we are attempting to allocate and fill an array of size Int.max. Which blows up.

SE-0231 (Swift Evolution):

This proposal introduces optional iteration (for?) and hence the possibility to use optional sequences as the corresponding attribute in for-in loops.

[…]

The ? notation here is a semantic emphasis rather than a functional unit: there is no for!. Syntactically marking an optional iteration is redundant, however, in constrast to switch, nil values are skipped silently. Swift strives to follow a style where silent handling of nil is acknowledged via the ? sigil, distinctly reflected in optional chaining. This decision was primarily based on inconsistency and potential confusion that an otherwise left without syntactic changes for-in loop could potentially lead to (“clarity over brevity”).

Comments RSS · Twitter

Leave a Comment