Skip to content

gh-162350: Use AC vectorcall for _json.make_encoder to speed up json.dumps - #162376

Open
cmaloney wants to merge 1 commit into
python:mainfrom
cmaloney:landable/ac-vc-json
Open

cmaloney wants to merge 1 commit into
python:mainfrom
cmaloney:landable/ac-vc-json

Conversation

@cmaloney

@cmaloney cmaloney commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

json.dumps() builds a new encoder on every call, so this is per-call overhead for small payloads.

@vectorcall requires an immutable type: setting attributes on _json.Encoder now raises TypeError. Bad separators are now named ("argument 'key_separator'") instead of "argument 5".

pyperformance, PGO+LTO:

Benchmark main branch
json_dumps 4.24 ms 4.00 ms: 1.06x faster

pyperf microbenchmarks, PGO+LTO:

Benchmark main branch
json.dumps(1) 456 ns 409 ns: 1.12x faster
json.dumps(small, separators=(',', ':')) 1.22 us 1.13 us: 1.07x faster
json.dumps(small) 850 ns 794 ns: 1.07x faster
json.dumps(small, sort_keys=True) 1.31 us 1.23 us: 1.07x faster

json.dumps() builds a new encoder on every call, so this is per-call
overhead for small payloads.

@vectorcall requires an immutable type: setting attributes on
_json.make_encoder now raises TypeError. Bad separators are now named
("argument 'key_separator'") instead of "argument 5".

pyperformance, PGO+LTO:

| Benchmark  | main    | branch                |
|------------|:-------:|:---------------------:|
| json_dumps | 4.24 ms | 4.00 ms: 1.06x faster |

pyperf microbenchmarks, PGO+LTO:

| Benchmark                                | main    | branch                |
|------------------------------------------|:-------:|:---------------------:|
| json.dumps(1)                            | 456 ns  | 409 ns: 1.12x faster  |
| json.dumps(small, separators=(',', ':')) | 1.22 us | 1.13 us: 1.07x faster |
| json.dumps(small)                        | 850 ns  | 794 ns: 1.07x faster  |
| json.dumps(small, sort_keys=True)        | 1.31 us | 1.23 us: 1.07x faster |

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant