python / python/cpython

Avoid redundant whitespace scan in json.loads() for documents without surrounding whitespace

Offen
#150,860 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

performance stdlib type-feature
Vorherrschende Sprache
Python
Sterne
77.2k
Forks
36k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Decoding a JSON document with json.loads() runs a whitespace-skipping regular expression at the start and end of the document through the pure-Python decode() wrapper, on every call, before and after the C scanner does the actual parsing. The overwhelming majority of documents have no leading or trailing whitespace, so both matches scan zero characters yet still pay for the call and the match-object allocation. For the small documents that dominate real traffic, that fixed overhead is a meaningful fraction of the total decode time.

Skipping the leading match when the document does not begin with whitespace, and the trailing match when the parse already consumed the whole string, removes that overhead from the common case. On a tiny document it is roughly 1.5x faster; documents that do have surrounding whitespace keep the original behavior, and large documents are unaffected.

This overlaps with the broader rewrite proposed in gh-117397; it is filed separately so the small, self-contained version can be evaluated on its own.

Linked PRs
  • gh-150861

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 bei json.loads() und dem dazugehörigen reinen Python-Wrapper decode(), vergleiche dann die Behandlung von Whitespace mit dem im Issue beschriebenen C-Scanner und der umfassenderen Überarbeitung in gh-117397. Abgeschlossen ist die Arbeit, wenn Dokumente ohne umgebenden Whitespace redundante Scans vermeiden, während Dokumente mit Whitespace ihr bisheriges Verhalten beibehalten; gh-150861 zeigt, dass die Arbeit bereits im Gange ist.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
performance
Issue-Typ
Refactoring
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

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