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-127119: Faster check for small ints in long_dealloc by eendebakpt · Pull Request #127620 · python/cpython

Conversation

@eendebakpt

@eendebakpt eendebakpt commented Dec 4, 2024 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor
  • Use a faster check for small ints in long_dealloc
  • Remove check for small ints in the free-threaded build
  • Add reference to PEP 683

Microbenchmark from #127120:

bench_long: Mean +- std dev: [main_pgo_bm_long] 195 ns +- 5 ns -> [pr_v2_pgo_bm_long] 191 ns +- 4 ns: 1.02x faster
bench_alloc: Mean +- std dev: [main_pgo_bm_long] 425 us +- 37 us -> [pr_v2_pgo_bm_long] 394 us +- 20 us: 1.08x faster

Benchmark hidden because not significant (1): bench_collatz

Geometric mean: 1.04x faster 

(benchmark is with PGO, without PGO I see no performance improvement)

  • Issue: Remove redundant small int check in long_dealloc #127119

Sorry, something went wrong.

Uh oh!

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

All reactions
@eendebakpt
Faster check for small ints in long_dealloc
cfb70cb
@bedevere-app bedevere-app Bot mentioned this pull request Dec 4, 2024
Remove redundant small int check in long_dealloc #127119
Closed
@bedevere-app bedevere-app Bot added the awaiting review label Dec 4, 2024
@blurb-it
📜🤖 Added by blurb_it.
426dd09
markshannon
markshannon reviewed Dec 5, 2024
View reviewed changes

@markshannon markshannon 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.

This is technically not legal C.
Pointer comparisons between separately allocated blocks of memory are undefined.

Which is a shame, because this is clearly faster.

Sorry, something went wrong.

Uh oh!

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

All reactions
eendebakpt added 2 commits December 5, 2024 20:39
@eendebakpt
Use int immortality bit for small int check
4639642
@eendebakpt
Merge branch 'small_int_immortal_v2' of github.com:eendebakpt/cpython…
d9c26d3
… into small_int_immortal_v2
@eendebakpt

eendebakpt commented Dec 5, 2024 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor Author

Pointer comparisions are indeed not always well-defined. For reference: see https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf, section 6.5.8.6, or https://www.gnu.org/software/c-intro-and-ref/manual/html_node/Pointer-Comparison.html.

I updated the PR to use the immortality bit instead.

(maybe there are some more places where we need to update documentation on the immortality bit)

All reactions

Sorry, something went wrong.

Uh oh!

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

eendebakpt added 4 commits December 5, 2024 20:40
@eendebakpt
Merge branch 'main' into small_int_immortal_v2
2f73d47
@eendebakpt
fix compiler warnings
3b6e1fe
@eendebakpt
Merge branch 'small_int_immortal_v2' of github.com:eendebakpt/cpython…
03184a7
… into small_int_immortal_v2
@eendebakpt
compiler warnings
5eca812
picnixz
picnixz reviewed Dec 5, 2024
View reviewed changes
Comment thread Objects/longobject.c Resolved

Uh oh!

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

Comment thread Misc/NEWS.d/next/Core_and_Builtins/2024-12-04-22-14-40.gh-issue-127119._hpyFE.rst
@@ -0,0 +1 @@
Slightly optimize the :class:`int` deallocator.

@picnixz picnixz Dec 5, 2024

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.

Maybe include the benchmarks results (it's a 4% improvement which is still noticable IMO). You should mention that it only concerns PGO builds as well.

Sorry, something went wrong.

Uh oh!

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

All reactions
eendebakpt and others added 3 commits December 5, 2024 21:04
@eendebakpt
some documentation updates
fedc102
@eendebakpt @picnixz
Update Objects/longobject.c
c733d25
Co-authored-by: Bénédikt Tran <[email protected]>
@eendebakpt
Merge branch 'small_int_immortal_v2' of github.com:eendebakpt/cpython…
829a595
… into small_int_immortal_v2
eendebakpt
eendebakpt commented Dec 5, 2024
View reviewed changes
Comment thread Tools/gdb/libpython.py Resolved

Uh oh!

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

eendebakpt added 3 commits December 5, 2024 21:06
@eendebakpt
Update Tools/gdb/libpython.py
39da0ea
@eendebakpt
tests!
5058e53
@eendebakpt
tests!
8a3d00f
@eendebakpt
eendebakpt marked this pull request as draft December 5, 2024 22:08
@bedevere-app bedevere-app Bot removed the awaiting review label Dec 5, 2024
@eendebakpt
update _PyLong_IsNonNegativeCompact
878207a
@eendebakpt

eendebakpt commented Dec 5, 2024

Copy link
Copy Markdown
Contributor Author

Turns out the immortal bit was assumed to be zero in _PyLong_IsNonNegativeCompact. The updated _PyLong_IsNonNegativeCompact makes some of the operations with ints a (tiny) bit slower, so we have to measure performance a bit more carefull.

All reactions

Sorry, something went wrong.

Uh oh!

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

@eendebakpt
eendebakpt marked this pull request as ready for review December 5, 2024 22:50
@bedevere-app bedevere-app Bot added the awaiting review label Dec 5, 2024
chris-eibl
chris-eibl reviewed Dec 6, 2024
View reviewed changes
Comment thread Objects/longobject.c Resolved

Uh oh!

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

@chris-eibl

chris-eibl commented Dec 7, 2024

Copy link
Copy Markdown
Member

Turns out the immortal bit was assumed to be zero in _PyLong_IsNonNegativeCompact. The updated _PyLong_IsNonNegativeCompact makes some of the operations with ints a (tiny) bit slower, so we have to measure performance a bit more carefull.

@eduardo-elizondo already spotted that back then #102464 (comment) and created #103403, especially to target long_dealloc, see #103403 (comment).

All reactions

Sorry, something went wrong.

Uh oh!

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

@eendebakpt

eendebakpt commented Dec 7, 2024

Copy link
Copy Markdown
Contributor Author

@chris-eibl Thanks for that little bit of history.

@markshannon @eduardo-elizondo This PR is more or less identical to #103403 which has been closed. Unless you feel different about it now, I suggest we close this.

All reactions

Sorry, something went wrong.

Uh oh!

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

@chris-eibl

chris-eibl commented Dec 8, 2024 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member

But your microbenchmarks for the initial version seemed promising.
Do you have numbers for the current version of the PR as well?

The results of the pyperformance fleet would be interesting, too, since you had to touch _PyLong_IsNonNegativeCompact.

I like that in your code the comment about Accidental De-Immortalizing is now exactly before _Py_SetImmortal.

In the previous version, I had to read carefully, especially because it starts with "This should never get called", which of course does not comment on the invocation of long_dealloc itself.

Every none-small-int will be deallocated, and for all of them we pay the price of _PyLong_IsCompact. If it is a compact int (very likely), then a bunch more code is executed. Especially for small int values we have to check whether they match the singletons to care about accidential de-immortaliziation. And we have to go down to the if (pylong == small_pylong) for all small int values for everything that derives from int.

I don't care much about IntEnums or their "predecessors" class _NamedIntConstant(int) , etc, since those most likely aren't deallocated before the end of the programm. But e.g. extensions that derive from int.

All reactions

Sorry, something went wrong.

Uh oh!

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

@eendebakpt
Merge branch 'main' into small_int_immortal_v2
d91b6e3
markshannon
markshannon requested changes Dec 20, 2024
View reviewed changes

@markshannon markshannon 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.

A few very minor issues

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread Modules/_testcapi/immortal.c Resolved

Uh oh!

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

Comment thread Modules/_testcapi/immortal.c Resolved

Uh oh!

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

Comment thread Include/cpython/longintrepr.h Resolved

Uh oh!

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

Comment thread Objects/longobject.c Outdated
_PyLong_ExactDealloc(PyObject *self)
{
assert(PyLong_CheckExact(self));
#ifndef Py_GIL_DISABLED

@markshannon markshannon Dec 20, 2024

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.

I don't think we should excluding the no-gil build.

Sorry, something went wrong.

Uh oh!

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

All reactions

@eendebakpt eendebakpt Dec 22, 2024

Copy link
Copy Markdown
Contributor Author

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.

You are right. According to https://peps.python.org/pep-0683/#stable-abi the no-gil implementation can work with older stable API extensions.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread Objects/longobject.c Outdated
_Py_FREELIST_FREE(ints, self, PyObject_Free);
return;
}
#ifndef Py_GIL_DISABLED

@markshannon markshannon Dec 20, 2024

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.

Likewise

Sorry, something went wrong.

Uh oh!

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

All reactions
@bedevere-app bedevere-app Bot added awaiting changes and removed awaiting review labels Dec 20, 2024
@bedevere-app

bedevere-app Bot commented Dec 20, 2024

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.

eendebakpt and others added 12 commits December 22, 2024 16:40
@eendebakpt @markshannon
Update Modules/_testcapi/immortal.c
ebc7e17
Co-authored-by: Mark Shannon <[email protected]>
@eendebakpt @markshannon
Update Modules/_testcapi/immortal.c
aaf110b
Co-authored-by: Mark Shannon <[email protected]>
@eendebakpt @markshannon
Update Include/cpython/longintrepr.h
8fcc1d0
Co-authored-by: Mark Shannon <[email protected]>
@eendebakpt
Merge branch 'main' into small_int_immortal_v2
aff8812
@eendebakpt
review comments
ebb0bca
@eendebakpt
include pycore_long.h
8d8e794
@eendebakpt
header
4a07ba1
@eendebakpt
include pycore_long.h
1d5b2e0
@eendebakpt
debug build failure
cfb9120
@eendebakpt
Merge branch 'main' into small_int_immortal_v2
b606c09
@eendebakpt
fix bug when copying the immortal bit
b4c8444
@eendebakpt
add assert to make sure the immortal bit is not set on subclasses
f76c0e2
@eendebakpt

eendebakpt commented Dec 23, 2024

Copy link
Copy Markdown
Contributor Author

@markshannon Adding the small int check for the free-threaded build uncovered a bug: we have to prevent copying the immortal bit when subclassing int. I addresed the bug in long_subtype_new and added an assert to _long_is_small_int to detect similar issues.

All reactions

Sorry, something went wrong.

Uh oh!

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

@eendebakpt

eendebakpt commented Jan 8, 2025

Copy link
Copy Markdown
Contributor Author

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-app bedevere-app Bot added awaiting change review and removed awaiting changes labels Jan 8, 2025
@bedevere-app

bedevere-app Bot commented Jan 8, 2025

Copy link
Copy Markdown

Thanks for making the requested changes!

@markshannon: 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-app
bedevere-app Bot requested a review from markshannon January 8, 2025 21:33
@eendebakpt eendebakpt mentioned this pull request Jan 28, 2025
gh-127119: Remove check on accidental deallocation of immortal objects for free-threaded build #127120
Closed
markshannon
markshannon approved these changes Jan 29, 2025
View reviewed changes

@markshannon markshannon 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.

Looks good

Sorry, something went wrong.

Uh oh!

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

All reactions
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting change review labels Jan 29, 2025
@markshannon
markshannon merged commit a292216 into python:main Jan 29, 2025
@bedevere-app bedevere-app Bot removed the awaiting merge label Jan 29, 2025
@eendebakpt eendebakpt mentioned this pull request May 1, 2025
gh-84436: Implement Immortal Objects #19474
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@picnixz picnixz picnixz left review comments
@chris-eibl chris-eibl chris-eibl left review comments
@markshannon markshannon markshannon approved these changes
+1 more reviewer
@TeamSpen210 TeamSpen210 TeamSpen210 left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

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

Assignees

No one assigned

Labels

None yet

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.

6 participants

@eendebakpt @chris-eibl @eduardo-elizondo @markshannon @TeamSpen210 @picnixz
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.