From 225976029a605816dfc14d51bb517c02ddf8e6ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bern=C3=A1t=20G=C3=A1bor?= Date: Wed, 3 Jun 2026 07:31:12 -0700 Subject: [PATCH] gh-150860: Skip the whitespace scan in json.loads() when there is none decode() ran a whitespace-skipping regex at both ends of every document even though most have none. Skip the leading match when the document does not start with whitespace and the trailing match when the parse already consumed the whole string. Documents with surrounding whitespace keep the original behavior and output is unchanged. --- Lib/json/decoder.py | 12 +++++++++--- .../2026-06-03-07-31-11.gh-issue-150860.8VbmHU.rst | 3 +++ 2 files changed, 12 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-06-03-07-31-11.gh-issue-150860.8VbmHU.rst diff --git a/Lib/json/decoder.py b/Lib/json/decoder.py index 364e44d40cc3073..42caf1f8fada9b1 100644 --- a/Lib/json/decoder.py +++ b/Lib/json/decoder.py @@ -355,10 +355,16 @@ def decode(self, s, _w=WHITESPACE.match): containing a JSON document). """ - obj, end = self.raw_decode(s, idx=_w(s, 0).end()) - end = _w(s, end).end() + # Skip the WHITESPACE.match() call (and its match-object allocation) + # for the common case where there is no leading whitespace. + idx = _w(s, 0).end() if s and s[0] in ' \t\n\r' else 0 + obj, end = self.raw_decode(s, idx=idx) + # Likewise avoid the trailing-whitespace match when the parse already + # consumed the whole string. if end != len(s): - raise JSONDecodeError("Extra data", s, end) + end = _w(s, end).end() + if end != len(s): + raise JSONDecodeError("Extra data", s, end) return obj def raw_decode(self, s, idx=0): diff --git a/Misc/NEWS.d/next/Library/2026-06-03-07-31-11.gh-issue-150860.8VbmHU.rst b/Misc/NEWS.d/next/Library/2026-06-03-07-31-11.gh-issue-150860.8VbmHU.rst new file mode 100644 index 000000000000000..f1bf6c7070d4e5f --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-06-03-07-31-11.gh-issue-150860.8VbmHU.rst @@ -0,0 +1,3 @@ +Speed up :func:`json.loads` for documents without leading or trailing +whitespace by skipping the whitespace scan in that common case. Patch by Bernát +Gábor.