coerce_input_literal spends extra time on type checks and non-null recursion

Aperta Adatta ai principianti
#302 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
78/100
Tipo di issue
Refactoring
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python
Ambito
performance

Direzione di ricerca

Leggi src/graphql/utilities/coerce_input_value.py e il benchmark in tests/benchmarks/test_execution_arguments.py. Verifica come coerce_input_literal gestisce i tipi NonNull e chiama i tre helper per il controllo dei tipi, quindi apporta le modifiche proposte ed esegui i test e il benchmark pertinenti. Il lavoro è completato quando il comportamento resta invariato e il benchmark mostra il miglioramento delle prestazioni segnalato.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

coerce_input_literal in src/graphql/utilities/coerce_input_value.py runs for every literal argument in a query. For example, it runs for multiplier: 2 once per item in tests/benchmarks/test_execution_arguments.py.

The function does two kinds of extra work:

  • For a NonNull type, it calls itself again with the inner type. The second call checks for a variable node and a non-null type again.
  • It uses the is_non_null_type, is_list_type and is_input_object_type helper functions. Each helper is one more function call around an isinstance check.

Measured on Python 3.12, median of 3 runs (time per call):

Situation Now With direct checks Change
Int! literal (multiplier: 2) 734 ns 479 ns 35% faster
[Int!] list of 10 ints 7557 ns 5089 ns 33% faster
input object {a: 1, b: "x"} 2355 ns 1717 ns 27% faster
variable $v 172 ns 156 ns no real change
execute_sync of the 1000-item benchmark query 5.32 ms 5.00 ms 6% faster

Proposed fix: unwrap the NonNull type in place instead of calling the function again. Use direct isinstance checks against GraphQLNonNull, GraphQLList and GraphQLInputObjectType in this one function. The results stay the same, and the full test suite passes with the change.

Environment: graphql-core main at 0aaea71, CPython 3.12.13, Linux x86_64.

Lingua principale
Python
Stelle
534
Fork
159
Merge medio
1g 10h
PR unite (30g)
11

Preparare l'ambiente

  • Nessun Dockerfile né file Docker Compose
  • Ha un modello di pull request
  • Nessuna guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di graphql-python/graphql-core

Tutte le issue di graphql-python/graphql-core

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.