Skip to content

Speed up options snapshot calculation#20797

Merged
JukkaL merged 1 commit intomasterfrom
optimize-options-snapshot-2
Feb 13, 2026
Merged

Speed up options snapshot calculation#20797
JukkaL merged 1 commit intomasterfrom
optimize-options-snapshot-2

Conversation

@JukkaL
Copy link
Copy Markdown
Collaborator

@JukkaL JukkaL commented Feb 13, 2026

The options_snapshot function was using up to 19% CPU in small incremental runs in a very large codebase, when not using orjson. Micro-optimize it, and use binary serialization instead of json.

We can still make this faster, but first let's see how much this helps.

The `options_snapshot` function was using up to 19% CPU in small
incremental runs in a very large codebase, when not using orjson.
Micro-optimize it, and use binary serialization instead of json.

We can still make this faster, but first let's see how much this
helps.
@JukkaL JukkaL requested a review from ilevkivskyi February 13, 2026 11:39
Copy link
Copy Markdown
Member

@ilevkivskyi ilevkivskyi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, let's see if this will help.

return result
# Process most options quickly, since this is performance critical.
buf = WriteBuffer()
write_json_value(buf, cast(JsonValue, values))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is somewhat surprising. When I measured it a while ago write_json_value() gave only marginal win compared to json_dumps() when using orjson (i.e. [faster-cache] option from pyproject.toml).

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it's only marginally faster than orjson based on microbenchmarking, but a lot faster compared to json. Since orjson is an optional dependency, I think it makes sense to prefer write_json_value (and it's still slightly faster than orjson probably).

@github-actions
Copy link
Copy Markdown
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@JukkaL JukkaL merged commit a7e66a6 into master Feb 13, 2026
23 checks passed
@JukkaL JukkaL deleted the optimize-options-snapshot-2 branch February 13, 2026 12:20
@JukkaL
Copy link
Copy Markdown
Collaborator Author

JukkaL commented Feb 13, 2026

Based on a CPU profile, this made the calculation reasonably faster compared to using stdlib json. I can't give exact numbers, since CPython and mypy were compiled using different C compiler flags, but I'm still convinced that this was worth it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants