Skip to content

gh-140009: Optimize JSON parsing with object_pairs_hook via PyTuple_FromArray - #144772

Open
andrewloux wants to merge 2 commits into
python:mainfrom
andrewloux:pytuple-json-objectpairs-fromarray
Open

gh-140009: Optimize JSON parsing with object_pairs_hook via PyTuple_FromArray#144772
andrewloux wants to merge 2 commits into
python:mainfrom
andrewloux:pytuple-json-objectpairs-fromarray

Conversation

@andrewloux

@andrewlouxandrewloux commented Feb 13, 2026

Copy link
Copy Markdown

Summary

This PR optimizes the object_pairs_hook path in Modules/_json.c (e.g., used by json.loads(s, object_pairs_hook=list)) by replacing PyTuple_Pack with PyTuple_FromArray.

PyTuple_Pack processes arguments variadically via va_list, while PyTuple_FromArray performs a direct memcpy from a stack-allocated array. For a fixed-size-2 tuple constructed on every key-value pair in the hot path, this eliminates unnecessary overhead.

Benchmarks (PGO+LTO)

Validated using pyperf in --rigorous mode on a full production build. Results were reproduced across multiple independent sessions.

  • Platform: macOS arm64 (Apple M-series)
  • Build:--enable-optimizations --with-lto
  • Tool: pyperf (--rigorous mode)
  • Baseline:upstream/main
  • Candidate:pytuple-json-objectpairs-fromarray (92d3f1a)
BenchmarkBaseline (Mean ± Std Dev)Candidate (Mean ± Std Dev)Speedup
json_pairs_hook_dense111 ms ± 9 ms108 ms ± 3 ms1.02x faster
json_pairs_hook_control89.2 ms ± 2.1 ms89.5 ms ± 1.9 msNeutral

Geometric mean: 1.01x faster

Benchmark script and repro commands

Repro commands (using bench_json_pairs_hook.py):

# Target: parsing with object_pairs_hook=list
python -m pyperf command --rigorous --name json_pairs_hook_dense
./python.exe bench_json_pairs_hook.py pairs 320 64 64
# Control: standard parsing (no hook)
python -m pyperf command --rigorous --name json_pairs_hook_control
./python.exe bench_json_pairs_hook.py control 320 64 64

bench_json_pairs_hook.py:

importjsonimportsysdefbuild_payload(objects, fields):
obj="{"+",".join(f'"k{i}":{i}'foriinrange(fields)) +"}"return"["+",".join(objfor_inrange(objects)) +"]"defmain():
mode, loops, objects, fields=sys.argv[1], int(sys.argv[2]), int(sys.argv[3]), int(sys.argv[4])
payload=build_payload(objects, fields)
for_inrange(loops):
ifmode=="pairs":
json.loads(payload, object_pairs_hook=list)
else:
json.loads(payload)
if__name__=="__main__":
main()

Analysis

The json_pairs_hook_dense benchmark parses a large JSON array of objects using object_pairs_hook=list. In this scenario, every key-value pair requires a size-2 tuple. The switch to the array-based API yields a ~2% speedup on this specific codepath, with a conservative geometric mean of ~1% across both benchmarks.

Notably, the candidate also shows a significant reduction in variance (±9 ms → ±3 ms), suggesting more deterministic performance on the optimized path.

The json_pairs_hook_control case confirms that the standard JSON decoding path (using the default decoder) is unaffected.

@python-cla-bot

python-cla-botBot commented Feb 13, 2026

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@andrewloux
andrewloux marked this pull request as ready for review February 13, 2026 01:55
@andrewlouxandrewloux changed the title gh-140009: Use PyTuple_FromArray in _json object_pairs_hook pathgh-140009: Optimize JSON parsing with object_pairs_hook via PyTuple_FromArrayFeb 13, 2026
@caje731

Copy link
Copy Markdown
Contributor

I could actually see speedups for both on my Apple M1 Pro:

Benchmarkbasecandidate
json_pairs_hook_control158 ms155 ms: 1.01x faster
json_pairs_hook_dense170 ms161 ms: 1.05x faster

👍

@picnixz

Copy link
Copy Markdown
Member

Can your benchmarks not include the time for constructing the payload please? because this benchmark is not relevant otherwise (noise from constructing the payload for instance)

@markshannonmarkshannon mentioned this pull request Feb 26, 2026
@github-actions

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

@github-actionsgithub-actionsBot added the stale Stale PR or inactive for long period of time. label May 6, 2026
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviewstaleStale PR or inactive for long period of time.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@andrewloux@caje731@picnixz