New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SR-12928] .first
is less efficient than .first { _ in true }
on LazyMapSequence
#55374
Comments
Comment by Jarek Pendowski (JIRA) Any updates on that? I know - pandemic and all that - but that's quite serious. Why is it evaluating `compactMap` twice? |
See apple/swift#55374 `.first` has been broken since 2020.
This is still an issue on Swift 5.9 |
See apple/swift#55374 `.first` has been broken since 2020.
I looked into this and thought I'd share some insights. The swift/stdlib/public/core/FlatMap.swift Lines 41 to 51 in 23a0186
swift/stdlib/public/core/Collection.swift Lines 1100 to 1105 in 30984f5
swift/stdlib/public/core/Filter.swift Lines 140 to 147 in 30984f5
swift/stdlib/public/core/Map.swift Lines 120 to 123 in 30984f5
As you can see, LazyFilterSequence invokes the closure while finding the startIndex, and LazyMapSequence invokes the closure again when the startIndex subscripts the collection. It is a bit silly. |
Attachment: Download
Environment
Apple Swift version 5.2.2 (swiftlang-1103.0.32.6 clang-1103.0.32.51)
Target: x86_64-apple-darwin19.4.0
macOS 10.15.4 (19E287)
Xcode 11.4.1
Additional Detail from JIRA
md5: 38b32ecf66be259b2052152057060ea8
Issue Description:
Shouldn't .first be synonymous to .first { _ in true } in terms of not only result, but also effort?
Also, is lazy
being less efficient than eager
expected?
The text was updated successfully, but these errors were encountered: