GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-101773: Optimize creation of Fractions in private methods by skirpichev · Pull Request #101780 · python/cpython

Conversation

@skirpichev

@skirpichev skirpichev commented Feb 10, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member
  • removed private _normalize argument of the Fraction constructor
  • added _from_coprime_ints() helper to make fractions from a pair of coprime ints
  • Issue: Optimize creation of unnormalized Fraction's in private methods (arithmetics, etc) #101773

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bedevere-bot bedevere-bot added the awaiting review label Feb 10, 2023
@bedevere-bot bedevere-bot mentioned this pull request Feb 10, 2023
Optimize creation of unnormalized Fraction's in private methods (arithmetics, etc) #101773
Closed
@AlexWaygood AlexWaygood added the performance Performance or resource usage label Feb 10, 2023
@skirpichev
pythongh-101773: Optimize creation of Fraction's in private methods
9cc62f7
@skirpichev
skirpichev force-pushed the opt-fractions-new branch from 130db33 to 9cc62f7 Compare February 10, 2023 09:41
@skirpichev
Merge branch 'main' into opt-fractions-new
836d3a6
eendebakpt
eendebakpt reviewed Feb 10, 2023
View reviewed changes
Comment thread Lib/fractions.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

eendebakpt
eendebakpt reviewed Feb 10, 2023
View reviewed changes
Comment thread Lib/fractions.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

eendebakpt
eendebakpt reviewed Feb 10, 2023
View reviewed changes
Comment thread Lib/fractions.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@skirpichev
skirpichev requested a review from eendebakpt February 10, 2023 12:02
@skirpichev

skirpichev commented Feb 11, 2023

Copy link
Copy Markdown
Member Author

Probably, from_float/decimal methods should be adapted to use this. Decimal.as_integer_ratio is in lowest terms. I guess, that same holds for the float method, but that's undocumented, unfortunately.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

mdickinson
mdickinson reviewed Feb 11, 2023
View reviewed changes
Comment thread Lib/fractions.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

mdickinson
mdickinson reviewed Feb 11, 2023
View reviewed changes
Comment thread Lib/fractions.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@mdickinson

mdickinson commented Feb 11, 2023

Copy link
Copy Markdown
Member

@skirpichev

I guess, that same holds for the float method, but that's undocumented, unfortunately

It does indeed. It would be reasonable to document it, I think.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@skirpichev skirpichev mentioned this pull request Feb 11, 2023
as_integer_ratio docstring's #101825
Closed
skirpichev added 4 commits February 11, 2023 15:13
@skirpichev
Adjust signature of _from_pair() and add docstring
3625d34
@skirpichev
Use new helper in from_float/Decimal
9e66b5a
@skirpichev
Restore back _normalize kwarg for the Fraction.__new__()
4324391
@skirpichev
Amend 3625d34
b0dca38
@skirpichev

skirpichev commented Feb 11, 2023 via email

Copy link
Copy Markdown
Member Author
On Sat, Feb 11, 2023 at 04:53:50AM -0800, Mark Dickinson wrote: I think you can and should assume that all core developers are very familiar with PEP 8. :-)
Here I do care more about users knowledge. The single leading underscore as a stigmate of private (internaly used) entity is more or less common convention across the python ecosystem. Sometimes it's enforced (e.g. help() hides such methods, this stuff is not included in star-imports and so on). I wonder if we shouldn't do same with _-prefixed kwargs (at least in help()).
That's a rather dogmatic approach. On CPython, we should prefer a more pragmatic approach: if we have reasonable grounds to believe that a change might break user code then we should take reasonable steps to avoid that breakage. It's not friendly to knowingly break existing code and then tell people "well, it was your fault for using something undocumented".
But the Python is a dynamical language and with great introspection capabilities... In this way we can end with supporting everything as part of the API, because people actually *can* use everything.
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mdickinson
mdickinson requested review from eendebakpt and mdickinson and removed request for eendebakpt February 12, 2023 11:02
eendebakpt
eendebakpt approved these changes Feb 12, 2023
View reviewed changes
@bedevere-bot bedevere-bot added awaiting core review and removed awaiting review labels Feb 12, 2023
@merwok merwok changed the title gh-101773: Optimize creation of Fraction's in private methods gh-101773: Optimize creation of Fractions in private methods Feb 19, 2023
@skirpichev

skirpichev commented Feb 24, 2023

Copy link
Copy Markdown
Member Author

@mdickinson, _normalize part was reverted. Does it make sense now?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mdickinson

mdickinson commented Feb 26, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

In this way we can end with supporting everything as part of the API.

Indeed. :-) As with most aspects of software development, careful judgement is required, evaluating the context and balancing the trade offs within that context. Few things are black and white. The fact that _normalize was intended to be private, and has a leading underscore, certainly means that removing it is easier than if it were public, but it's still something that shouldn't be done without thinking it through first. Hyrum's law is a real thing.

@skirpichev

Does it make sense now?

Hmm, not really. :-) I don't want to leave a _normalize flag that's unused and untested, indefinitely. I think we have three options:

  1. Leave this alone.
  2. Introduce _from_pair (possibly after renaming - see below) and immediately remove the _normalize flag
  3. Introduce _from_pair (ditto) and deprecate the _normalize flag.

Of those options, option 3 just seems like too much code churn for too little benefit.

There was a recent discuss.python.org thread in which the meaning of _normalize was misinterpreted, which pushes me a bit further towards option 2. Arguably, _normalize is too discoverable right now (since it shows up in IDE completions and tooltips), and there's a temptation to guess what it means. _from_pair would also be somewhat discoverable, but at least we get to add a docstring that clearly indicates that it's private, and the expected constraints for people who decide to use it anyway.

Screenshot 2023-02-26 at 12 52 41

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

mdickinson
mdickinson reviewed Feb 26, 2023
View reviewed changes
Comment thread Lib/fractions.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot bedevere-bot added awaiting changes and removed awaiting core review labels Feb 26, 2023
@bedevere-bot

bedevere-bot commented Feb 26, 2023

Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mdickinson mdickinson self-assigned this Feb 26, 2023
skirpichev added 4 commits February 27, 2023 06:14
@skirpichev
Nitpick on docstring (int's -> ints)
3a8506d
@skirpichev
Revert "Restore back _normalize kwarg for the Fraction.__new__()"
13262af
This reverts commit 4324391.
@skirpichev
Rename: _from_pair -> _from_coprime_ints
4e8977a
@skirpichev
Merge branch 'main' into opt-fractions-new
57ac373
@skirpichev

skirpichev commented Feb 27, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

Arguably, _normalize is too discoverable right now (since it shows up in IDE completions and tooltips)

That is a more generic problem: we have PEP8's convention where _single_leading_underscore is weak “internal use” indicator. On another hand, these arguments are available for introspection and shown in the help() output, IDE tooltips and so on.

Probably, all above use the signature() function. Maybe it does make sense to add a private=False option for this function to hide private kwargs per default?

There was a recent discuss.python.org thread in which the meaning of _normalize was misinterpreted

(Probably it was about this thread.) Indeed, there was an impression that all methods do support unnormalized fractions.

offtopic On another hand 1. there is no doubts that the argument was private 2. most methods actually do support this (arithmetics) and 3. much more can if we do a cheap normalization (ensure that denominator is positive)

So, I think the proposal might have some sense and it's possible to implement it with a little overhead for canonicalized fractions. E.g. we can mark unnormalized fractions with a sticky flag (or flags) and do special steps to deal with this case in some methods. Not sure it worth to have this in the stdlib: to really support unnormalized fractions almost every method should be rewritten (e.g. arithmetic methods do some normalization of the output, which we probably want to avoid for unnormalized fractions).

PS:
I have made the requested changes; please review again.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot bedevere-bot added awaiting change review and removed awaiting changes labels Feb 27, 2023
@bedevere-bot

bedevere-bot commented Feb 27, 2023

Copy link
Copy Markdown

Thanks for making the requested changes!

@mdickinson: please review the changes made to this pull request.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot
bedevere-bot requested a review from mdickinson February 27, 2023 04:08
@eendebakpt

eendebakpt commented Feb 27, 2023

Copy link
Copy Markdown
Contributor

@skirpichev @mdickinson The changes look good to me. With the removal of the (semi-private) argument _normalize, we could update the whatsnew entry with a note stating that it is removed.

AlexWaygood and mdickinson reacted with thumbs up emoji
All reactions
  • 👍 2 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

eendebakpt
eendebakpt reviewed Feb 27, 2023
View reviewed changes
Comment thread Misc/NEWS.d/next/Library/2023-02-10-11-59-13.gh-issue-101773.J_kI7y.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

skirpichev and others added 2 commits February 27, 2023 12:24
@skirpichev @eendebakpt
Update Misc/NEWS.d/next/Library/2023-02-10-11-59-13.gh-issue-101773.J…
4b4a386
…_kI7y.rst

Co-authored-by: Pieter Eendebak <[email protected]>
@skirpichev
+1
580b1be
@skirpichev
skirpichev force-pushed the opt-fractions-new branch from 4339283 to 580b1be Compare February 27, 2023 09:38
@mdickinson
Tweak docstring wording
53d5e94
mdickinson
mdickinson approved these changes Feb 27, 2023
View reviewed changes

@mdickinson mdickinson left a comment

Copy link
Copy Markdown
Member

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.

LGTM. I tweaked the docstring wording on _from_coprime_ints slightly.

I'll merge when CI completes.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bedevere-bot bedevere-bot added awaiting merge and removed awaiting change review labels Feb 27, 2023
@mdickinson
mdickinson merged commit 4f3786b into python:main Feb 27, 2023
@bedevere-bot bedevere-bot removed the awaiting merge label Feb 27, 2023
@skirpichev
skirpichev deleted the opt-fractions-new branch February 28, 2023 02:23
@skirpichev

skirpichev commented Feb 28, 2023

Copy link
Copy Markdown
Member Author

Thanks to all reviewers.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

carljm added a commit to carljm/cpython that referenced this pull request Feb 28, 2023
@carljm
Merge branch 'main' into inlinecomp2
73dc0ed
* main: (67 commits)
  pythongh-99108: Add missing md5/sha1 defines to Modules/Setup (python#102308)
  pythongh-100227: Move _str_replace_inf to PyInterpreterState (pythongh-102333)
  pythongh-100227: Move the dtoa State to PyInterpreterState (pythongh-102331)
  pythonGH-102305: Expand some macros in generated_cases.c.h (python#102309)
  Migrate to new PSF mailgun account (python#102284)
  pythongh-102192: Replace PyErr_Fetch/Restore etc by more efficient alternatives (in Python/) (python#102193)
  pythonGH-90744: Fix erroneous doc links in the sys module (python#101319)
  pythongh-87092: Make jump target label equal to the offset of the target in the instructions sequence (python#102093)
  pythongh-101101: Unstable C API tier (PEP 689) (pythonGH-101102)
  IDLE: Simplify DynOptionsMenu __init__code (python#101371)
  pythongh-101561: Add typing.override decorator (python#101564)
  pythongh-101825: Clarify that as_integer_ratio() output is always normalized (python#101843)
  pythongh-101773: Optimize creation of Fractions in private methods (python#101780)
  pythongh-102251: Updates to test_imp Toward Fixing Some Refleaks (pythongh-102254)
  pythongh-102296 Document that inspect.Parameter kinds support ordering (pythonGH-102297)
  pythongh-102250: Fix double-decref in COMPARE_AND_BRANCH error case (pythonGH-102287)
  pythongh-101100: Fix sphinx warnings in `types` module (python#102274)
  pythongh-91038: Change default argument value to `False` instead of `0` (python#31621)
  pythongh-101765: unicodeobject: use Py_XDECREF correctly (python#102283)
  [doc] Improve grammar/fix missing word (pythonGH-102060)
  ...
scoder added a commit to scoder/quicktions that referenced this pull request Mar 19, 2023
@scoder
Speed up the internal creation of result Fractions if the calculated …
c5b6a2e
…numerator/denominator pair is already normalised.

Follows python/cpython#101780
bmwiedemann pushed a commit to bmwiedemann/openSUSE that referenced this pull request Mar 21, 2023
dgarcia @bmwiedemann
Update python-quicktions to version 1.14 / rev 7 via SR 1073017
ee9ccd6
https://build.opensuse.org/request/show/1073017
by user dgarcia + dimstar_suse
- Enable python 3.11 build again, now is supported
- Update to 1.14
  - Implement __format__ for Fraction, following python/cpython#100161
  - Implement Fraction.is_integer(), following python/cpython#100488
  - Fraction.limit_denominator() is faster, following
    python/cpython#93730
  - Internal creation of result Fractions is about 10% faster if the
    calculated numerator/denominator pair is already normalised,
    following python/cpython#101780
  - Built using Cython 3.0.0b1.
- 1.13
  - Parsing very long numbers from a fraction string was very slow,
    even slower than fractions.Fraction. The parser is now faster in
    all cases (and still much faster for shorter numbers).
  - Fraction did not implement __int__.
    https://bugs.python.org/issue44547
- 1.12
  - Faster and more spa
@hauntsaninja hauntsaninja mentioned this pull request Apr 28, 2023
[NO ISSUE] Adds __eq__ support for non-normalized fractions #103959
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@eendebakpt eendebakpt eendebakpt approved these changes
+1 more reviewer
@mdickinson mdickinson mdickinson approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@mdickinson mdickinson

Labels

performance Performance or resource usage

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@skirpichev @mdickinson @bedevere-bot @eendebakpt @AlexWaygood
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.