python / python/cpython

Avoid boolean objects when branching in the JIT

Offen
#149,238 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

interpreter-core performance topic-JIT
Vorherrschende Sprache
Python
Sterne
77.2k
Forks
35.9k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

We currently specialize TO_BOOL for many common types.
This avoids the overhead of API calls, but we still need to load either True or False, then test against True or False

The additional cost of having to load and compare with Py_True and Py_False is expensive for what are often quite simple operations. E.g _TO_BOOL_LIST is 10 instructions (AArch64 linux) but only half of that is performing the comparison.

We can breakdown _TO_BOOL_FOO into _TO_BOOL_BIT_FOO; _BIT_TO_BOOL
and then optimize _BIT_TO_BOOL; _GUARD_IS_TRUE_POP to _GUARD_IS_TRUE_BIT_POP.

Where the "bit" versions produce a single bit boolean (0 for False, 1 for True).

Whereas _TO_BOOL_LIST is 10 instructions, hypothetical _TO_BOOL_BIT_LIST` would only be 5 instructions.

We already optimize _GUARD_IS_TRUE_BIT_POP to _GUARD_BIT_IS_SET_POP reducing the number of machine instructions from 5 to 2, but replacing it with _GUARD_IS_TRUE_BIT_POP would reduce it to a single machine instruction and remove the need for the replication in _GUARD_BIT_IS_SET_POP.

We can also replace many of the comparisons with a "bit" form, e.g. replacing _COMPARE_OP_FLOAT with _COMPARE_OP_BIT_FLOAT would reduce the code size from 19 to 13 instructions (21 to 14 accounting for the following guard as well).
[ Specializing for the actual operation, can further reduce the stencil size to 8 instructions ]


All instructions sizes are for the variant with all inputs in outputs in registers.

Linked PRs
  • gh-149418

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 damit, die JIT-Spezialisierungspfade für TO_BOOL, _BIT_TO_BOOL, _GUARD_IS_TRUE_POP und _GUARD_BIT_IS_SET_POP zu lesen, und prüfe anschließend den verknüpften PR gh-149418. Als erledigt gilt die Aufgabe, wenn boolesche Operationen, wo angebracht, Ein-Bit-Formen verwenden und die Anzahl der generierten Instruktionen wie beschrieben reduziert wird.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
compilers, performance
Issue-Typ
Refactoring
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Muss geklärt werden
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

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