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-413] AnySequence does not implement all methods on SequenceType #43030
Comments
I have rewritten the reported test case to compile with Swift 3 like this: struct MySequence: Sequence {
private static let array = [1,2,3]
func makeIterator() -> AnyIterator<Int> {
return AnyIterator(MySequence.array.makeIterator())
}
func suffix(_ maxLength: Int) -> AnySequence<Int> {
let r = MySequence.array.suffix(maxLength)
return AnySequence(r.map{$0 + 1})
}
}
let a = MySequence()
let b = AnySequence(a)
print("\(Array(a.suffix(2))) == \(Array(b.suffix(2)))?”) // [3, 4] == [3, 4]? Note: I had to split the I think this was fixed in Jan 6th 2016 in commit [commit 31f17e2, [stdlib] using static method dispatch instead of failable casts | 31f17e2#diff-d10f8c94467ccf15797e3610c67bb69c]. Marking as fixed. |
Mea culpa again, it works, my rewritten version incorrectly implemented |
Comment by Andrew Bennett (JIRA) I believe that the example on this issue was poorly constructed, the issue still exists, although you have managed to rewrite the example to avoid it. The issue is not whether you can write code that avoids calling `suffix` on To clarify: At the moment This is still an issue, and is unfixed, I'm going to mark it as unresolved again until |
I see. Doesn't the problem stem from the fact that the efficient As far as @dabrahams, would your |
Given all that… I think based on this issue’s title and the provided test case this one should be closed and another issue opened to deal with the requested performance optimization for I have tried testing this extension, but it is never invoked: extension Sequence where Self == CountableRange<Int> {
public func suffix(_ n: Int) -> AnySequence<Int> {
return AnySequence(self.suffix(n))
}
} |
Upon further research, my previous two comments are incorrect. bnut (JIRA User), you said:
I’ve tested this exact case and Profiling the test cases you’ve added in #7420, a different performance problem manifests itself when the remaining sequence is long, because the underlying iterator doesn’t get specialized and all elements are being vended through virtual dispatch on generic protocol. I really think we should resolve this bug as Done and file another for that remaining issue. |
I’ve created new issue to track the slow performance of iterating over elements of existential collections: SR-4499.
|
Additional Detail from JIRA
md5: f1f0d117a5204e887ab4187dd39016b6
relates to:
Issue Description:
TL;DR
At the moment
(0...1000000000).suffix(2)
is really efficient.However,
AnySequence(0...1000000000).suffix(2)
is not.Long version
AnySequence implements underestimateCount and generate, however it does not implement any of the other methods on SequenceType.
This means that if you've provided custom implementations of these methods it still uses the implementation from SequenceType's protocol extension.
For example:
Outputs:
but it should output:
This example is probably not something you would want to do, but it is representative of the problem. Other cases this may be an issue are when implementing infinite sequences, implementing suffix efficiently on a bidirectional indexed collection type, etc.
The text was updated successfully, but these errors were encountered: