When I was evaluating the performance of some of the code I was using, I noticed some weird performance issues due to type inference, specifically between two UIView methods:
UIView.convert(_ rect: CGRect, from coordinateSpace: UICoordinateSpace) UIView.convert(_ rect: CGRect, from view: UIView?)
The first method converts between any two coordinate spaces, which
UIView conforms to, and the second method evaluates from one view to another. The first method is, approximately 14 times slower than the second, though they both have the same final outcome. This seems to be because the coordinate-based method must be able to convert between any two coordinate spaces, rather than assuming that the views are in the same hierarchy.
The issue arises, however, in how the compiler determines which method to fire between the two, when you pass it a non-optional view. Which method should the compiler choose?
The compiler chooses to go to the non-optional variable, which is the first one, rather than the second, which would be more efficient. We can work around this by casting the view to UIView?, which will then cue the compiler to choose the second method.
Are there any cleaner methods to determine which method will be chosen? And do you think I should consider submitting this as a bug on UIKit to recommend they consider the implications on apps which may be unwittingly being hit with an artificial 14x performance penalty?