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.
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
gh-93476: Fraction.limit_denominator speed increase #93477
gh-93476: Fraction.limit_denominator speed increase #93477
Changes from 5 commits
599c55b
70ca7ad
a1a4fc8
3c06ded
e5ab32e
40cbe78
f5daa21
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
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.
Does this change make a significant difference to performance? If not, I'd suggest reverting it, both for readability and for the purposes of keeping the PR focused. From looking at the code, a simple
Fraction(self)
shouldn't end up doing any gcd calculation here.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 actually saves 30% on this case based on tests above -- however, I hadn't realized at the time that the
return self
was executed in this branch and no normalization was done. The speed increase is probably based on theisinstance()
check, which just keeps getting faster and faster in subsequent versions, so it might eventually be the faster route. I've removed the change.If Fractions are eventually to be truly immutable and hashable, then
return self
is the correct response here, but that is probably backwards incompatible with real code where people change_numerator
or_denominator
after instantiation.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 just realised that I'm not 100% convinced that the result here is always normalised. There are two parts to being normalised: the numerator must be relatively prime to the denominator, and the denominator must be positive. I'd thought about the first part (which is fine), but not about the second.
Is it clear that
bound2_d
andbound1_d
are positive at this point?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.
Answer: yes, it's true that
bound2_d
andbound1_d
are positive at this point, though perhaps not 100% clear. In the Euclidean loop above, on the very first iterationa
can be negative, but on all subsequent iterationsa
is nonnegative. And on the first iterationq1 = 0
, so that potentially negativea
does no harm, andq0
andq1
are guaranteed nonnegative at all times. Then it's easy to check that they must in fact be positive at the point where the loop exits.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.
That was my reading of the loop as well, but I didn't write the code (nor name the variables) so I didn't want to change this without another set of eyes; thanks!