Xcode 7.2b2 (7C46t): [Swift] Using function name as closure argument is slower than using closure literal in generic context
| Originator: | janoschhildebrand | ||
| Number: | rdar://23428804 | Date Originated: | 06-Nov-2015 |
| Status: | Open | Resolved: | |
| Product: | Developer Tools | Product Version: | Xcode 7.2b2 |
| Classification: | Performance | Reproducible: | Always |
Summary:
The following snippet demonstrates my issue:
func fast<E : Comparable >(var elements: [E]) {
while elements.count > 0 {
elements.sortInPlace() { $0 < $1 }
elements.popLast()
}
}
func slow<E : Comparable>(var elements: [E]) {
while elements.count > 0 {
elements.sortInPlace(<)
elements.popLast()
}
}
I would expect both of these function to have equal performance, however slow() is much slower than fast().
(Some profiling indicates that at least in this case partial applies seem to be involved)
If the functions are not generic, performance is equally, as expected. Same results if the comparison closures are passed into the generic functions from a non-generic context.
Using a generic function instead of an operator leads to similar results.
Attached is a small example project that demonstrates this issue by printing the runtimes of these functions for some random input.
Steps to Reproduce:
1. Open the attached project
2. Build & Run
3. The application should print the runtimes for the execution of slow() and fast()
Expected Results:
The two runtimes should be relatively close, indicating equal performance.
Actual Results:
The results indicate that there is a significant performance difference.
Version:
Xcode 7.2b2 (7C46t)
Apple Swift version 2.1.1 (swiftlang-700.1.101.11 clang-700.1.79)
OS X 10.11.1 (15B42)
Comments
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!