gh-150820: Speed up json.dumps() for small documents (GH-150827)

serhiy-storchaka <[email protected]> Tue, 11 Aug 2026 11:52:56 -0400 (EDT)
Newsgroups gmane.comp.python.cvs
Message-ID <[email protected]>
https://github.com/python/cpython/commit/4d3075655e8bfddb6f5015b43aade43b1265bd65
commit: 4d3075655e8bfddb6f5015b43aade43b1265bd65
branch: main
author: Bernát Gábor <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2026-08-11T18:52:43+03:00
summary:

gh-150820: Speed up json.dumps() for small documents (GH-150827)

Only create the float formatting helper when the pure Python encoder is used.

files:
A Misc/NEWS.d/next/Library/2026-06-02-15-45-00.gh-issue-150820.W7tpO7.rst
M Lib/json/encoder.py

diff --git a/Lib/json/encoder.py b/Lib/json/encoder.py
index 718b3254241c565..8768b63a3f80417 100644
--- a/Lib/json/encoder.py
+++ b/Lib/json/encoder.py
@@ -223,29 +223,6 @@ def iterencode(self, o, _one_shot=False):
         else:
             _encoder = encode_basestring
 
-        def floatstr(o, allow_nan=self.allow_nan,
-                _repr=float.__repr__, _inf=INFINITY, _neginf=-INFINITY):
-            # Check for specials.  Note that this type of test is processor
-            # and/or platform-specific, so do tests which don't depend on the
-            # internals.
-
-            if o != o:
-                text = 'NaN'
-            elif o == _inf:
-                text = 'Infinity'
-            elif o == _neginf:
-                text = '-Infinity'
-            else:
-                return _repr(o)
-
-            if not allow_nan:
-                raise ValueError(
-                    "Out of range float values are not JSON compliant: " +
-                    repr(o))
-
-            return text
-
-
         if self.indent is None or isinstance(self.indent, str):
             indent = self.indent
         else:
@@ -256,6 +233,28 @@ def floatstr(o, allow_nan=self.allow_nan,
                 self.key_separator, self.item_separator, self.sort_keys,
                 self.skipkeys, self.allow_nan)
         else:
+            def floatstr(o, allow_nan=self.allow_nan,
+                    _repr=float.__repr__, _inf=INFINITY, _neginf=-INFINITY):
+                # Check for specials.  Note that this type of test is processor
+                # and/or platform-specific, so do tests which don't depend on
+                # the internals.
+
+                if o != o:
+                    text = 'NaN'
+                elif o == _inf:
+                    text = 'Infinity'
+                elif o == _neginf:
+                    text = '-Infinity'
+                else:
+                    return _repr(o)
+
+                if not allow_nan:
+                    raise ValueError(
+                        "Out of range float values are not JSON compliant: " +
+                        repr(o))
+
+                return text
+
             _iterencode = _make_iterencode(
                 markers, self.default, _encoder, indent, floatstr,
                 self.key_separator, self.item_separator, self.sort_keys,
diff --git a/Misc/NEWS.d/next/Library/2026-06-02-15-45-00.gh-issue-150820.W7tpO7.rst b/Misc/NEWS.d/next/Library/2026-06-02-15-45-00.gh-issue-150820.W7tpO7.rst
new file mode 100644
index 000000000000000..ae9858f5294183a
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2026-06-02-15-45-00.gh-issue-150820.W7tpO7.rst
@@ -0,0 +1 @@
+Speed up :func:`json.dumps` for small documents. Patch by Bernát Gábor.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]