Title: gh-150820: Speed up json.dumps() for small documents by gaborbernat · Pull Request #150827 · python/cpython · GitHub
Open Graph Title: gh-150820: Speed up json.dumps() for small documents by gaborbernat · Pull Request #150827 · python/cpython
X Title: gh-150820: Speed up json.dumps() for small documents by gaborbernat · Pull Request #150827 · python/cpython
Description: json.dumps() runs through the pure-Python iterencode() wrapper on every call before the C encoder does the heavy lifting, and that wrapper builds a float-formatting helper closure each time — even though the common C fast path never uses it. For the small documents that dominate real traffic (an API response, a single record, a config fragment), that per-call closure construction is a measurable slice of the total work. This builds the float helper only inside the branch that runs the pure-Python encoder, so the C fast path no longer allocates a closure it never calls. The wrapper stays in Python, so the win lands on the default build; output is byte-identical. Over 20 small documents, json.dumps() is about 8% faster, consistently across float-heavy, mixed, and no-float payloads: Benchmark base patched dumps 20 mixed small docs 22.1 µs 20.6 µs: 8% faster dumps 20 float-heavy docs 32.7 µs 31.1 µs: 5% faster dumps 20 no-float docs 16.5 µs 14.9 µs: 11% faster Benchmark (pyperf) Run base vs patched by swapping Lib/json/encoder.py on the same interpreter. The script is self-contained. import json, pyperf small = [{"id": i, "name": f"item{i}", "ok": i % 2 == 0, "tags": ["a","b"], "v": i*1.5} for i in range(20)] floaty = [{"lat": 51.5074+i, "lon": -0.1278-i, "alt": i*3.3, "err": i/7.0} for i in range(20)] plain = [{"id": i, "name": f"n{i}", "ok": True} for i in range(20)] runner = pyperf.Runner() runner.bench_func("dumps 20 mixed small docs", lambda: [json.dumps(o) for o in small]) runner.bench_func("dumps 20 float-heavy docs", lambda: [json.dumps(o) for o in floaty]) runner.bench_func("dumps 20 no-float docs", lambda: [json.dumps(o) for o in plain]) The decoder whitespace-skip change that originally accompanied this has moved to a separate PR, since it is more contentious (it depends on the outcome of gh-117397 and shows mixed results on large documents); this PR is now the encoder change alone. Resolves #150820. Issue: gh-150820
Open Graph Description: json.dumps() runs through the pure-Python iterencode() wrapper on every call before the C encoder does the heavy lifting, and that wrapper builds a float-formatting helper closure each time — even ...
X Description: json.dumps() runs through the pure-Python iterencode() wrapper on every call before the C encoder does the heavy lifting, and that wrapper builds a float-formatting helper closure each time — even ...
Opengraph URL: https://github.com/python/cpython/pull/150827
X: @github
Domain: github.com
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:430fc637-5589-a8e2-4162-f025dc394c56 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | ADA8:4D9F2:19D5BC4:2242BFF:6B02199C |
| html-safe-nonce | c6d8a39fb0be09d74c936f9a9ff5bef8354d5e5fc4055d6e7ab6566b310a4f3d |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBREE4OjREOUYyOjE5RDVCQzQ6MjI0MkJGRjo2QjAyMTk5QyIsInZpc2l0b3JfaWQiOiI2MDM5MjYxOTg2MDk5OTYwMjIwIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | c42f3ed55a6cf27ec4565e3dcadf7a9153a5ced0e53b2da6d2964512bd16295d |
| hovercard-subject-tag | pull_request:3791748286 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/python/cpython/pull/150827/files |
| twitter:image | https://avatars.githubusercontent.com/u/690238?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/690238?s=400&v=4 |
| og:image:alt | json.dumps() runs through the pure-Python iterencode() wrapper on every call before the C encoder does the heavy lifting, and that wrapper builds a float-formatting helper closure each time — even ... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width