FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-159702: Add fast-path for float in _json encoder by vstinner · Pull Request #159826 · python/cpython · GitHub

Repository navigation

gh-159702: Add fast-path for float in _json encoder - #159826

Open
vstinner wants to merge 2 commits into
python:mainfrom
vstinner:json_float
Open

vstinner wants to merge 2 commits into
python:mainfrom
vstinner:json_float

Conversation

vstinner commented Oct 20, 2026 •
edited by bedevere-app Bot
Loading

Copy link
Copy Markdown
Member

Optimize json encoder for floats: up to 1.3x faster.

Avoid creating a temporary Unicode object when writing the JSON representation of a float: decode directly the ASCII-encoded string (PyOS_double_to_string()) into the writer.

Optimize json encoder for floats: up to 1.3x faster.

Avoid creating a temporary Unicode object when writing the JSON
representation of a float: decode directly the ASCII-encoded string
(PyOS_double_to_string()) into the writer.
bedevere-app Bot added the type-feature A feature request or enhancement label Oct 20, 2026

Copy link
Copy Markdown
Member Author

Benchmark:

import pyperf
runner = pyperf.Runner()
for list_len in (1, 10, 100, 1000):
    runner.timeit(f'encode list of {list_len} floats',
        setup=f'import json; L=[1.0] * {list_len}; dumps=json.dumps',
        stmt='dumps(L)')

Results:

Benchmark main writer
encode list of 1 floats 1.34 us 1.28 us: 1.05x faster
encode list of 10 floats 2.65 us 2.34 us: 1.13x faster
encode list of 100 floats 11.6 us 9.20 us: 1.26x faster
encode list of 1000 floats 94.5 us 71.3 us: 1.33x faster
Geometric mean (ref) 1.19x faster

Comment thread Modules/_json.c Outdated

Copy link
Copy Markdown
Contributor

I compared the performance on jsonyx (because I didn't want to re-build CPython):

case before after difference
0.0 122.32 ns 67.52 ns 1.81x faster
1e-05 327.62 ns 275.35 ns 1.19x faster
5e-324 578.92 ns 528.56 ns 1.10x faster
1.7976931348623157e+308 2378.23 ns 2326.70 ns 1.02x faster

Co-authored-by: Maxim Martynov <martinov_m_s_@mail.ru>

Copy link
Copy Markdown
Member Author

cc @serhiy-storchaka

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting core review type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants


Back | FazBrowse Home | New Git URL