-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Make IteratorSize(::Iterators.Cycle)
not always IsInfinite
and document why
#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]>
# 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 ofIteratorSize
that 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? |
function IteratorSize(::Type{Cycle{I}}) where I | ||
# TODO: find a better way of communicating the size of a cycle | ||
# IsInfinite() would be false if iterator ever becomes empty | ||
IteratorSize(I) === IsInfinite() ? IsInfinite() : 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.
A refinement, to improve precision:
IteratorSize(I) === IsInfinite() ? IsInfinite() : SizeUnknown() | |
c = IteratorSize(I) | |
((c === IsInfinite()) || (c === HasShape{0}())) ? IsInfinite() : SizeUnknown() |
The idea is that there are two cases when we know the number of elements is not zero (and thus infinite):
- when
IteratorSize(I) === IsInfinite()
, already accounted for, just listing it for completeness - when
IteratorSize(I) === HasShape{0}()
, because a zero-dimensional shape implies a length of exactly one
Fixes #53169
Deletes an "XXX: this is false if iterator ever becomes empty" comment