python / python/cpython

Lock contention inside `_PyType_LookupRef`

Offen
#132,380 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

interpreter-core performance topic-free-threading type-feature
Vorherrschende Sprache
Python
Sterne
77.2k
Forks
35.9k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Feature or enhancement

Proposal:

As part of adding free-threading support to LibCST, we noticed there is a lot of lock contention on TYPE_LOCK inside the _PyType_LookupRef function. In the LibCST, the common "visitor" pattern is used. For example, in the _visitors.py module there is the code:

visit_func = getattr(self, f"visit_{type(node).__name__}", None)

The second argument to getattr() is a non-interned string and it causes the cached and lock-free path of _PyType_LookupRef() never to be taken. Instead, the TYPE_LOCK mutex is acquired on each lookup. This obviously scales very badly if there are multiple threads looking up class methods using this pattern.

Testing was done with Python 3.13 but I believe the same issue exists with 3.14.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs
  • gh-132381
  • gh-131174
  • gh-132651
  • gh-132652
  • gh-133669
  • gh-135112

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit der Lektüre von _PyType_LookupRef und des TYPE_LOCK-Pfads in CPython und untersuche anschließend das Besuchermuster in LibCSTs _visitors.py. Vergleiche das Verhalten unter Python 3.13 und 3.14 und prüfe die verknüpften PRs; zum Abschluss sollte eine abgestimmte Änderung gehören, die die Lock-Konkurrenz behebt, sowie ein Nachweis, dass der Lookup-Pfad besser skaliert.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
c, python
Bereich
backend, performance
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
20/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.