natural_comparison_key: faster sort keys for names without digits

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

I maintainer di solito rispondono entro 1 giorno

@dexhunter ci sta già lavorando.

Dal 7/11/2026.

  • #311 di @dexhunter — aperta

Valutazione

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

Direzione di ricerca

Inizia da natural_comparison_key in src/graphql/pyutils/natural_compare.py e leggi tests/pyutils/test_natural_compare.py. Verifica che il percorso rapido proposto e l’assegnazione di slice preservino le chiavi esistenti, quindi esegui il file di test indicato. Il lavoro è completato quando i test passano e l’ottimizzazione mantiene invariate le chiavi.

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

Descrizione

natural_comparison_key in src/graphql/pyutils/natural_compare.py runs for every name when lexicographic_sort_schema sorts a schema. It is also used by suggestion_list and sort_value_node.

Problem

The function always splits the name with a regex. Then it walks the parts with zip and itertools.cycle inside a generator. Most GraphQL names (userName, createdAt) have no digits. For these names, this work always gives the same one-item tuple (name,).

Numbers

I measured the current code and a small change with timeit (CPython 3.12, Linux). The table shows the time to compute the keys for each list of names.

Situation Current Proposed Change
GitHub schema names (3323 names) 2.11 ms 0.34 ms 84% faster
Letters only, userName (5000 names) 3.00 ms 0.38 ms 88% faster
Underscore names, USER_NAME (5000 names) 3.09 ms 1.83 ms 41% faster
Names with numbers, field12x3 (5000 names) 5.61 ms 3.72 ms 34% faster

Proposed fix

Return (key,) early when key.isalpha() is true, because such a name cannot contain a decimal digit. For other names, keep the regex split and replace the digit parts in one slice assignment instead of the zip/cycle generator. The keys stay exactly the same, and the existing tests in tests/pyutils/test_natural_compare.py pass.

Environment

  • graphql-core main at 1d505c1
  • CPython 3.12, Linux x86_64
Lingua principale
Python
Stelle
534
Fork
159
Merge medio
1g 18h
PR unite (30g)
9

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.