-
Notifications
You must be signed in to change notification settings - Fork 33
Fix type instability for exponent iterator with no variable #343
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
Open
blegat
wants to merge
5
commits into
master
Choose a base branch
from
bl/exp_no_var
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
IteratorSize
should be define on the type, not the value.IteratorSize(::Iterators.Cycle)
not alwaysIsInfinite
and document why JuliaLang/julia#54187 (comment)This method, defined on the value, is not consistent with the above method, defined on the type.
Maybe just return
SizeUnknown()
? That's not precise, but it's accurate and type stable.Uh oh!
There was an error while loading. Please reload this page.
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.
It is but if you look at the implementation of
collect(::Any)
, it is callingIteratorSize
on the value which then redirect to callingIteratorSize
on the type so it this implementation actually passes all my tests checking consistency withcollect
etc...So the only issue will be that users that call
IteratorSize
on the type in the case they created the exponent iterator with a list of 0 variables will getBase.IsInfinite
while in fact the iterator only has one element. The fact that this givesIsInfinite
will just prevent some code to work while it could have worked if it was finite but that code is already not working when there is more than 0 variables so I don't see a use case being broken by that. I actually needIsInfinite
for the use with. Indeed,SizeUnknown
would be a bit safer here but on the other hand, the case with zero variable is kind of a corner case andIsInfinite
may also be understood asMaybeIsInfinite
, not sure if there is any code that relies on it being actually infinite :/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.
Maybe the good compromise it to just return
SizeUnknown
when it's called on the typeThere 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.
Ah this is exactly what's done for
Iterators.Cycle
, thanks for the link, this is exactly the same case, interestingThere 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 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 resistance to invalidation) for unrelated loaded packages. This is especially bad if the method is of an uncommon type.