-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Make IteratorSize(::Iterators.Cycle) not always IsInfinite add to docstring as example
#54187
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
base: master
Are you sure you want to change the base?
Conversation
Co-authored-by: adienes <[email protected]>
base/iterators.jl
Outdated
| # IsInfinite() would be false if iterator ever becomes empty | ||
| IteratorSize(I) === IsInfinite() ? IsInfinite() : SizeUnknown() | ||
| end | ||
| IteratorSize(it::Cycle) = isempty(it.xs) ? HasLength() : IsInfinite() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have other examples where IteratorSize(it) and IteratorSize(typeof(it)) (potentially) disagree? Makes me a bit uncomfortable.
And if it.xs is stateful, it seems that IsInfinite might turn out to be wrong, because isempty(it.xs) might become true.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have other examples where IteratorSize(it) and IteratorSize(typeof(it)) (potentially) disagree? Makes me a bit uncomfortable.
No, this is the first case, and changes the docs to suggest the possibility.
And if it.xs is stateful, it seems that IsInfinite might turn out to be wrong, because isempty(it.xs) might become true.
IMO that's because Iterators.cycle on stateful iterators is broken. It is explicitly documented to be infinite for non-empty arguments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am facing the same issue in JuliaAlgebra/MultivariatePolynomials.jl#343. There are probably many cases where an iterator is infinite except in degenerate cases. We could also document that when an iterator type says that it is IsInfinite, it means that it may be infinite and calling IteratorSize on an instance may (it could also be difficult to know in advance whether it is infinite, say for instance that you define an iterator for this iteration that stops when it reaches 1) distinguish whether it is infinite or not. These iterators are also said to be infinite but they may still end if the corresponding input closes I guess.
If we decide that IsInfinite only means "may be infinite", then the IteratorSize(::Type{Cycle}) should probably return IsInfinite in all cases, and never SizeUnknown.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My thoughts regarding defining IteratorSize on instances of a type:
-
I interpret the iteration interface like so:
-
Callers of
IteratorSize(::Any)should be able to assume the call is "free", in the sense it gets constant folded and does not exist at run time when everything is concretely inferred. -
The intention behind the
IteratorSize(x) = IteratorSize(typeof(x))is as a mere convenience for making it unnecessary to calltypeof. So this method is supposed to be the only method ofIteratorSizethat accepts non-Type. -
Callers should be able to assume
IteratorSize(x) === IteratorSize(typeof(x)), if!isa(x, Type). -
If one wants a trait like
IteratorSize, but defined on instances, they should create a new function instead of adding methods toIteratorSize.
-
-
Another point possibly worth considering is abstract inference: when the argument types to a call are not concretely known, Julia will possibly consider a set of more than one matching method. Thus adding a methods to a callable can negatively affect the code generation (and affinity to invalidation) for unrelated loaded packages. This is especially bad if the method is of an uncommon type.
|
This PR includes:
Would it be possible to separate this into multiple PRs? |
|
This PR is blocked by design decisions around how we want to handle iterators with interesting size/length availability characteristics. Questions for triage:
|
|
on second look, it seems important that
basically all the "passthroughs" like ALL of these would no longer be able to determine was it considered yet to add a type parameter to |
|
From Triage: |
|
We can't add a type parameter to track this because
|
IteratorSize(::Iterators.Cycle) not always IsInfinite and document whyIteratorSize(::Iterators.Cycle) not always IsInfinite add to docstring as example
|
There seems to be a problem with CI with |
|
Ah, that julia> z = zip([1,2,5], Iterators.cycle(()))
zip([1, 2, 5], Base.Iterators.Cycle{Tuple{}}(()))
julia> length(z)
3
julia> collect(z)
3-element Vector{Union{}}:
#undef
#undef
#undef
julia> z = zip([1,2,5], Iterators.cycle(Int[]))
zip([1, 2, 5], Base.Iterators.Cycle{Vector{Int64}}(Int64[]))
julia> collect(z)
3-element Vector{Tuple{Int64, Int64}}:
(281473195310576, 281473195310672)
(281473195310736, 281473195310800)
(281473195310864, 281473164575856) |
|
Making length(::Zip) handle SizeUnkown as if it were SizeInfinite would preserve the aformentioned bug and introduce it in other SizeUnknown cases. For the most part, I think this PR is fine and correct. If an unknown size is present, We could special case We could also have a way for IMO the best course of action is to adjust the test in CI, run PkgEval, and merge as is. |
|
I support |
|
Looking at the actual CI failures
I find "merge as is" somewhat unacceptable. I'll go with special case Cycle components of Zip and see how that goes. |
|
We should add methods for IteratorSize of Cycle of empty and non-empty tuples, which we know are empty and infinite, respectively. |
|
Also arguably Zip should not try to call |
Fixes #53169
Fixes #43235
Deletes an "XXX: this is false if iterator ever becomes empty" comment