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-139951: Do not track immutable tuples in GC#140204

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

Conversation

@efimov-mikhail
Copy link
Member

@efimov-mikhailefimov-mikhail commentedOct 16, 2025
edited by bedevere-appbot
Loading

@efimov-mikhailefimov-mikhail marked this pull request as draftOctober 16, 2025 11:09
@efimov-mikhail
Copy link
MemberAuthor

This is just an expirement to improve Sergey's PR to solve this issue#139951

@efimov-mikhailefimov-mikhail changed the titleChange in young object counting[gh-139951] Change in young GC object counting w/ no tracking immutable tuplesOct 16, 2025
@efimov-mikhailefimov-mikhail changed the title[gh-139951] Change in young GC object counting w/ no tracking immutable tuplesgh-139951: Change in young GC object counting w/ no tracking immutable tuplesOct 16, 2025
@efimov-mikhailefimov-mikhail marked this pull request as ready for reviewOctober 16, 2025 12:21
@efimov-mikhail
Copy link
MemberAuthor

@efimov-mikhailefimov-mikhail changed the titlegh-139951: Change in young GC object counting w/ no tracking immutable tuplesgh-139951: Count only tracked objects in GC, don't track immutable tuplesOct 16, 2025
efimov-mikhailand others added3 commitsOctober 16, 2025 15:49
Co-authored-by: Victor Stinner <vstinner@python.org>
Co-authored-by: Victor Stinner <vstinner@python.org>
@efimov-mikhailefimov-mikhail changed the titlegh-139951: Count only tracked objects in GC, don't track immutable tuplesgh-139951: Do not track immutable tuples in GCOct 16, 2025
@efimov-mikhail
Copy link
MemberAuthor

Is it correct thatpack(1, None) crashes for now intest_tuple_pack?

Copy link
Member

@vstinnervstinner left a comment

Choose a reason for hiding this comment

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

Ok, the PR looks complete now. It's maybe time to run benchmark to measure the performance overhead of this change!

@vstinner
Copy link
Member

Is it correct that pack(1, None) crashes for now in test_tuple_pack?

That's because test C_testlimitedcapi function replaces PythonNone with CNULL, andPyTuple_Pack() arguments must not beNULL.

efimov-mikhail reacted with thumbs up emoji

@eendebakpt
Copy link
Contributor

The approach here is to check for tuples of immutables at creation time. An alternative could be to do this inside the garbage collector (and untrack the tuple if needed). Was that considered?

The issue solved here is about a use case where many tuples are created in a tight loop. But there could potentially also be tuples of immutables in a normal workflow that visited many times by the GC.

One datapoint: after starting up my "normal" work and running this script

import gcgc.collect()def candidate(obj):   return all( not gc.is_tracked(x) for x in obj )for immutable_type in (tuple, frozenset):    number_of_objects_tracked  =0     number_of_candidates=0    number_of_immutable_candidates = 0        for obj in gc.get_objects():        number_of_objects_tracked += 1        if type(obj) is immutable_type:            number_of_candidates +=1                    if  candidate(obj):                number_of_immutable_candidates+=1        print(f'type {immutable_type}')    print(f'  {number_of_objects_tracked=}')    print(f'  {number_of_candidates=}')    print(f'  {number_of_immutable_candidates=}')

I get as output:

type <class 'tuple'>  number_of_objects_tracked=703468  number_of_candidates=82177  number_of_immutable_candidates=582type <class 'frozenset'>  number_of_objects_tracked=703478  number_of_candidates=4799  number_of_immutable_candidates=4787

So for me the amount of tuples would not be reduced a lot, but it would be interesting to apply the same approach to thefrozenset. Could someone run the above script on their "typical" workflow or application?

@sergey-miryanov
Copy link
Contributor

An alternative could be to do this inside the garbage collector (and untrack the tuple if needed). Was that considered?

This is already doing in the GC (seeuntrack_tuples ingc.c). Also, you may be interested in#139389

@efimov-mikhail
Copy link
MemberAuthor

I've created another PR with tests:
#140575

We can also adapt changes to functions_PyTuple_FromArraySteal,tuple_concat,tuple_repeat,tuple_subscript if we want to.
I have no strong opinion about it.

@efimov-mikhailefimov-mikhail marked this pull request as draftOctober 25, 2025 10:39
@vstinner
Copy link
Member

Is this change still relevant after commit0c01090 and commit88ad41f?

@sergey-miryanov
Copy link
Contributor

Is this change still relevant after commit0c01090 and commit88ad41f?

Only if we want to implement it to_PyTuple_FromArraySteal,tuple_concat,tuple_repeat,tuple_subscript as@efimov-mikhail said above.

We both haven't strong opinion on this.

@vstinner
Copy link
Member

You may create a new PR for these functions if you want to update these functions.

sergey-miryanov reacted with thumbs up emoji

@efimov-mikhail
Copy link
MemberAuthor

efimov-mikhail commentedOct 28, 2025
edited
Loading

I've decided to close this PR.
@sergey-miryanov, feel free to experiment with those functions if you want to.

sergey-miryanov reacted with thumbs up emoji

@sergey-miryanov
Copy link
Contributor

@efimov-mikhail Thanks!

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@sergey-miryanovsergey-miryanovsergey-miryanov left review comments

@vstinnervstinnervstinner left review comments

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

4 participants

@efimov-mikhail@vstinner@eendebakpt@sergey-miryanov

[8]ページ先頭

©2009-2026 Movatter.jp