Skip to content

Memoize the options snapshot#21354

Merged
JukkaL merged 2 commits into
python:masterfrom
KevinRK29:options-snapshot-memoize
Jun 12, 2026
Merged

Memoize the options snapshot#21354
JukkaL merged 2 commits into
python:masterfrom
KevinRK29:options-snapshot-memoize

Conversation

@KevinRK29

@KevinRK29 KevinRK29 commented Apr 28, 2026

Copy link
Copy Markdown
Collaborator

The options_snapshot is about 6% of CPU on incremental runs in a very large codebase.

This caches the (platform, hash_digest) tuple keyed by id() of the cloned Options, and keeps each cloned object alive in a parallel _options_snapshot_keepalive list so id() cannot be reused by the GC after a transient clone is collected.

This improved the overall time by roughly 5%, the time taken in the function dropped by ~67% on a warm run, and the ~110k calls dropped to ~220 distinct cloned Options without any change in output

@github-actions

This comment has been minimized.

Comment thread mypy/build.py Outdated
write_json_value(buf, cast(JsonValue, values))
return {"platform": platform_opt, "other_options": hash_digest(buf.getvalue())}
cache = manager.options_snapshot_cache
key = id(cloned)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can you use just cloned as the key?

Comment thread mypy/build.py Outdated
buf = WriteBuffer()
write_json_value(buf, cast(JsonValue, values))
cached = (platform_opt, hash_digest(buf.getvalue()))
cache[key] = cached

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think unstructured globs can never be cached, since they are always recreated, so the cache will never be hit (but I'm not 100% sure).

@KevinRK29 KevinRK29 marked this pull request as ready for review June 12, 2026 06:38
@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. ✅

@KevinRK29 KevinRK29 changed the title [WIP] Memoize the options snapshot Memoize the options snapshot Jun 12, 2026
@JukkaL JukkaL merged commit f8bf7ab into python:master Jun 12, 2026
25 checks passed
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