Skip to content

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

Merged
cmaloney merged 1 commit into
python:mainfrom
cmaloney:landable/ac-vc-json
Nov 26, 2026
Merged

cmaloney merged 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>
@nineteendo

Copy link
Copy Markdown
Contributor

Wouldn't it be a better solution to re-use the encoder accross calls?

@cmaloney

Copy link
Copy Markdown
Contributor Author

Wouldn't it be a better solution to re-use the encoder accross calls?

The object gets modified during encoding.

@nineteendo

Copy link
Copy Markdown
Contributor

You mean s->markers? That could be constructed per call and passed as argument.

@cmaloney

cmaloney commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor Author

I agree we could refactor json to work differently. I'm not trying to change the design decisions why decode does one path and encode does a different one (json.loads does cache a decoder with _default_decoder), my assumption is that was an intentional choice until proved otherwise. Goal here is just small refactor to use AC Vectorcall that has a measurable performance impact.

In general PyArg_ParseTupleAndKeywords is significantly slower and more hand-maintained code than using Argument Clinic, I think it's reasonable to refactor to use Argument Clinic. That it's a measurable performance improvement is a nice benefit on top.

@eendebakpt

Copy link
Copy Markdown
Contributor

Overall looks good.

Making the internal _json.Encoder immutable makes me slightly nervous (but is also good in a sense). @serhiy-storchaka

@cmaloney

cmaloney commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor Author

This apparently was made mutable in the move towards heaptypes, see gh-88074 in python 3.9/3.10. Both _json.Encoder and _json.Scanner were. This moves _json.Encoder back to immutable which everyone in that thread seemed to think was okay but didn't get changed. Given that history that makes me more confident here and that a separate small patch to make _json.Scanner immutable is likely good follow up.

edit:swap mutable for immutable in the first sentence 🤦‍♂️

@cmaloney
cmaloney merged commit b902174 into python:main Nov 26, 2026
63 checks passed
@cmaloney
cmaloney deleted the landable/ac-vc-json branch November 26, 2026 20:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

4 participants