Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-127119: Faster check for small ints in long_dealloc#127620

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
markshannon merged 39 commits intopython:mainfromeendebakpt:small_int_immortal_v2
Jan 29, 2025

Conversation

@eendebakpt
Copy link
Contributor

@eendebakpteendebakpt commentedDec 4, 2024
edited
Loading

  • Use a faster check for small ints inlong_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 fasterbench_alloc: Mean +- std dev: [main_pgo_bm_long] 425 us +- 37 us -> [pr_v2_pgo_bm_long] 394 us +- 20 us: 1.08x fasterBenchmark hidden because not significant (1): bench_collatzGeometric mean: 1.04x faster

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

Copy link
Member

@markshannonmarkshannon left a 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.

@eendebakpt
Copy link
ContributorAuthor

eendebakpt commentedDec 5, 2024
edited
Loading

Pointer comparisions are indeed not always well-defined. For reference: seehttps://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf, section 6.5.8.6, orhttps://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)

@@ -0,0 +1 @@
Slightly optimize the:class:`int` deallocator.
Copy link
Member

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.

eendebakptand others added3 commitsDecember 5, 2024 21:04
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
@eendebakpteendebakpt marked this pull request as draftDecember 5, 2024 22:08
@eendebakpt
Copy link
ContributorAuthor

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.

@eendebakpteendebakpt marked this pull request as ready for reviewDecember 5, 2024 22:50
@chris-eibl
Copy link
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 targetlong_dealloc, see#103403 (comment).

@eendebakpt
Copy link
ContributorAuthor

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

@chris-eibl
Copy link
Member

chris-eibl commentedDec 8, 2024
edited
Loading

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 thepyperformance 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 oflong_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 theif (pylong == small_pylong) for all small int values for everything that derives fromint.

I don't care much aboutIntEnums 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 fromint.

Copy link
Member

@markshannonmarkshannon left a 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

_PyLong_ExactDealloc(PyObject*self)
{
assert(PyLong_CheckExact(self));
#ifndefPy_GIL_DISABLED
Copy link
Member

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.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

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

_Py_FREELIST_FREE(ints,self,PyObject_Free);
return;
}
#ifndefPy_GIL_DISABLED
Copy link
Member

Choose a reason for hiding this comment

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

Likewise

@bedevere-app
Copy link

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 phraseI 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.

@eendebakpt
Copy link
ContributorAuthor

@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 inlong_subtype_new and added an assert to_long_is_small_int to detect similar issues.

@eendebakpt
Copy link
ContributorAuthor

I have made the requested changes; please review again

@bedevere-app
Copy link

Thanks for making the requested changes!

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

Copy link
Member

@markshannonmarkshannon left a comment

Choose a reason for hiding this comment

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

Looks good

@markshannonmarkshannon merged commita292216 intopython:mainJan 29, 2025
46 of 49 checks passed
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@picnixzpicnixzpicnixz left review comments

@chris-eiblchris-eiblchris-eibl left review comments

@markshannonmarkshannonmarkshannon approved these changes

+1 more reviewer

@TeamSpen210TeamSpen210TeamSpen210 left review comments

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

6 participants

@eendebakpt@chris-eibl@eduardo-elizondo@markshannon@TeamSpen210@picnixz

[8]ページ先頭

©2009-2025 Movatter.jp