is_printable_as_block_string checks each character in a Python loop

Aperta Adatta ai principianti
#312 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.

  • #313 di @dexhunter — aperta

Valutazione

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

Direzione di ricerca

Leggi is_printable_as_block_string() e i quattro controlli corrispondenti in GraphQL.js, poi esamina tests/language/test_block_string.py per individuare la copertura del comportamento esistente. Sostituisci il ciclo sui caratteri con i controlli descritti basati sui metodi delle stringhe, mantenendo invariati i risultati per tutti gli input. Esegui quel file di test e confronta i casi misurati; il lavoro è concluso quando il comportamento resta invariato e i casi con descrizioni lunghe mostrano l’accelerazione indicata.

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

Descrizione

print_schema() and print_ast() call is_printable_as_block_string() once for each description. The function walks the string one character at a time in a Python for loop. It keeps four boolean flags for each character. For long descriptions, this loop is a large part of the time that print_schema() uses.

Measured with Python 3.12 on one isolated CPU core (best of 7 runs):

Situation Now With str methods Change
One-line description (30 chars) 1405 ns 184 ns 87% less time
Description with 12 lines 19.2 µs 2.7 µs 86% less time
print_schema() of the GitHub schema fixture 25.5 ms 16.3 ms 36% less time
String that starts with a new line 106 ns 427 ns 0.3 µs slower

The last row is the only slower case. The old loop stops at the first character there. Such strings are rare in real schemas.

Proposed fix: keep the same checks, but use str methods instead of the character loop. A str.isprintable() pre-check and a regex find control characters. Then str.split("\n") gives the lines for the leading, trailing and indent checks. The result stays the same for all inputs, and tests/language/test_block_string.py passes.

This is a Python-specific change only. It keeps the four rules of isPrintableAsBlockString in GraphQL.js as four short checks with the same comments, so a later sync with upstream stays easy.

Environment: graphql-core main (1d505c1), CPython 3.12.13, 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.