Skip to content

bpo-46311: Clean up PyLong_FromLong and PyLong_FromLongLong - #30496

Merged
markshannon merged 3 commits into
python:mainfrom
mdickinson:cleanup-py-long-from-long
Mar 1, 2022
Merged

markshannon merged 3 commits into
python:mainfrom
mdickinson:cleanup-py-long-from-long

Conversation

@mdickinson

@mdickinson mdickinson commented Jan 9, 2022 •

Copy link
Copy Markdown
Member

PR #27832 inadvertently introduced a couple of changes to PyLong_FromLong that didn't make a lot of sense: an (unsigned long) cast was replaced with (twodigits), and a digit count variable (counting number of PyLong digits in a C long) had its type needlessly changed from int to Py_ssize_t.

  • The first change is a potential portability bug, but only on platforms with 128-bit longs. The (unsigned long) cast is obviously correct, while figuring out whether (twodigits) loses information takes some work.
  • The second change is merely a potential pessimization: there's no need to use what's typically a 64-bit integer to count the number of PyLong digits in a long.

This PR:

  • reverts those two changes
  • moves the check for medium values earlier in the function (immediately after the small values check), and simplifies that check
  • makes the code a bit less branchy (from experiments locally and on godbolt.org, the expression ival < 0 ? 0U-(unsigned long)ival : ival) gets compiled to something branchless on most platforms, as does ival < 0 ? -ndigits : ndigits
  • introduces parallel changes for PyLong_FromLongLong, which now has a fast path for medium-size values.

https://bugs.python.org/issue46311

@erlend-aasland

Copy link
Copy Markdown
Contributor

I don't see how this qualifies for skip issue. Perhaps I am misreading the devguide, but to me this PR seems inappropriately labelled. If I am mistaken; sorry for the noise.

@mdickinson

Copy link
Copy Markdown
Member Author

@erlend-aasland Yes, I think you're right. I'll add an issue.

@mdickinson mdickinson changed the title Clean up PyLong_FromLong and PyLong_FromLongLong Jan 9, 2022
Comment thread Objects/longobject.c
}

/* Create a new int object from a C long int */

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a minor consistency fix, following the Boy Scout Rule: the prevailing style in this file is to leave a blank line between the comment-description and the definition. But the change is not strictly necessary for this PR, and I can revert if reviewers prefer.

@markshannon

Copy link
Copy Markdown
Member

Thanks. Looks good on initial review.
There are a couple of warnings (lines 294 and 1118) that need fixing.
I re-review once those are fixed.

@markshannon
markshannon self-requested a review January 21, 2022 15:01

@mdickinson mdickinson left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two changes that should silence compiler warnings.

Comment thread Objects/longobject.c Resolved
Comment thread Objects/longobject.c Resolved
@mdickinson

Copy link
Copy Markdown
Member Author

Thanks, @markshannon. The compiler warnings have been silenced.

@markshannon markshannon self-assigned this Feb 28, 2022
@markshannon

Copy link
Copy Markdown
Member

Thanks

@markshannon
markshannon merged commit c60e6b6 into python:main Mar 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

5 participants