Skip to content

gh-162752: Skip stop-the-world when a type slot update changes nothing - #162763

Merged
nascheme merged 6 commits into
python:mainfrom
nascheme:gh-162752-slot-fastpath
Nov 30, 2026
Merged

nascheme merged 6 commits into
python:mainfrom
nascheme:gh-162752-slot-fastpath

Conversation

@nascheme

@nascheme nascheme commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

Assigning a special method to a class stopped the world on every assignment, even when no slot changed (e.g. replacing one Python __getitem__ function with another). With other threads contending, each pause could cost about 1 ms. Change so that replacing a Python function with another can usually avoid stopping the world when the type slots are already up to date.

…nothing

Assigning a special method to a class stopped the world on every
assignment, even when no slot changed (e.g. replacing one Python
__getitem__ function with another).  With other threads contending, each
pause could cost about 1 ms.  Before stopping the world, compute the new
slot values with TYPE_LOCK held and skip the pause if none would change.
@nascheme nascheme added type-bug An unexpected behavior, bug, or error performance Performance or resource usage topic-free-threading needs backport to 3.14 bugs and security fixes needs backport to 3.15 bugs and security fixes labels Nov 27, 2026
@nascheme
nascheme marked this pull request as ready for review November 27, 2026 21:20
Comment thread Objects/typeobject.c Resolved
Avoid looking at the type dict, since it can release TYPE_LOCK.  Add
slot_update_is_noop() helper and use that.
@cmaloney

cmaloney commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

This looks safe to me. It might be good to add a new threaded test in test_free_threading/test_type.py to try and help catch races this could enable.

@nascheme nascheme added the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Nov 28, 2026
@bedevere-bot

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @nascheme for commit d935e60 🤖

Results will be shown at:

https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F162763%2Fmerge

If you want to schedule another build, you need to add the 🔨 test-with-buildbots label again.

@bedevere-bot bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Nov 28, 2026
The slotdefs that fill the same slot all name the same generic function,
since that function has to dispatch to every one of their special
methods.  The check that lets a special method assignment skip the
stop-the-world depends on it: it compares a slot with the function of
the slotdef for the assigned name, while update_one_slot() stores that
of whichever slotdef it handled last.  If they ever differed, slots
that are up to date could fail the comparison and stop the world on
every assignment.  Assert it when the slotdefs are initialized.

Co-authored with Claude Fable 5.2
Assigning a Python function over another one now decides without
stopping the world whether any slot changes, by walking the subclasses
with the type lock held.  Race that walk against assignments that do
change the slots of a subclass and against __bases__ assignments that
remove the subclass and free its base's tp_subclasses, then check that
the slots still agree with the MRO.

Co-authored with Claude Fable 5.2
… slots

slots_are_generic() iterates tp_subclasses with a borrowed reference,
which is only safe while TYPE_LOCK is held: a __bases__ assignment in
another thread can empty and free that dict.  Nothing fails visibly if
the lock is dropped during the walk (the freed dict is usually reused),
so assert that the dict is still the one being iterated.  With this,
test_slot_update_unchanged_race fails if the walk blocks.

Co-authored with Claude Fable 5.2

@gpshead gpshead left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I added a threaded test and a couple of internal invariant assertions. not hitting merge as you may want to look those over. the threaded test has scenarios that are code i'd hope nothing ever writes but should trigger the various code paths and race conditions that could come up.

@nascheme
nascheme merged commit c52b7a8 into python:main Nov 30, 2026
63 checks passed
@miss-islington-app

Copy link
Copy Markdown

Thanks @nascheme for the PR 🌮🎉.. I'm working now to backport this PR to: 3.14, 3.15.
🐍🍒⛏🤖

@miss-islington-app

Copy link
Copy Markdown

Sorry, @nascheme, I could not cleanly backport this to 3.15 due to a conflict.

Please backport manually with cherry_picker, see the devguide for more information.

cherry_picker c52b7a85d4f5f015a3d684eb3bf3e62b6c571ab0 3.15

@miss-islington-app

Copy link
Copy Markdown

Sorry, @nascheme, I could not cleanly backport this to 3.14 due to a conflict.

Please backport manually with cherry_picker, see the devguide for more information.

cherry_picker c52b7a85d4f5f015a3d684eb3bf3e62b6c571ab0 3.14

@bedevere-app

bedevere-app Bot commented Nov 30, 2026

Copy link
Copy Markdown

GH-162848 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 bugs and security fixes label Nov 30, 2026
@bedevere-app

bedevere-app Bot commented Nov 30, 2026

Copy link
Copy Markdown

GH-162849 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Nov 30, 2026
nascheme added a commit that referenced this pull request Nov 30, 2026
… nothing (gh-162763) (gh-162848)

Assigning a special method to a class stopped the world on every
assignment, even when no slot changed (e.g. replacing one Python
__getitem__ function with another).  With other threads contending, each
pause could cost about 1 ms.  Before stopping the world, compute the new
slot values with TYPE_LOCK held and skip the pause if none would change.

(cherry picked from commit c52b7a8)

Co-authored-by: Gregory P. Smith greg@krypto.org
nascheme added a commit that referenced this pull request Nov 30, 2026
… nothing (gh-162763) (gh-162849)

Assigning a special method to a class stopped the world on every
assignment, even when no slot changed (e.g. replacing one Python
__getitem__ function with another).  With other threads contending, each
pause could cost about 1 ms.  Before stopping the world, compute the new
slot values with TYPE_LOCK held and skip the pause if none would change.

The check walks tp_subclasses with only TYPE_LOCK held, so also hold
TYPE_LOCK when type_dealloc() removes a type from the subclasses of its
bases.  That part of gh-151377 was not backported to 3.14.

(cherry picked from commit c52b7a8)

Co-authored-by: Gregory P. Smith <greg@krypto.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance Performance or resource usage topic-free-threading type-bug An unexpected behavior, bug, or error

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants