Repository navigation
gh-162752: Skip stop-the-world when a type slot update changes nothing - #162763
Conversation
…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.
Avoid looking at the type dict, since it can release TYPE_LOCK. Add slot_update_is_noop() helper and use that.
|
This looks safe to me. It might be good to add a new threaded test in |
|
🤖 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. |
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
left a comment
There was a problem hiding this comment.
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.
|
Thanks @nascheme for the PR 🌮🎉.. I'm working now to backport this PR to: 3.14, 3.15. |
|
Sorry, @nascheme, I could not cleanly backport this to Please backport manually with cherry_picker, see the devguide for more information. |
|
Sorry, @nascheme, I could not cleanly backport this to Please backport manually with cherry_picker, see the devguide for more information. |
|
GH-162848 is a backport of this pull request to the 3.15 branch. |
|
GH-162849 is a backport of this pull request to the 3.14 branch. |
… 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
… 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>
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.