Skip to content

[ty] Reuse expression cache for TypedDict union inference#25643

Merged
charliermarsh merged 1 commit into
mainfrom
charlie/fix-recursive-typed-dict-union-performance
Jun 4, 2026
Merged

[ty] Reuse expression cache for TypedDict union inference#25643
charliermarsh merged 1 commit into
mainfrom
charlie/fix-recursive-typed-dict-union-performance

Conversation

@charliermarsh

Copy link
Copy Markdown
Member

Summary

When contextually checking a dictionary literal against a union of TypedDicts, we speculatively infer the literal once for each candidate. Recursive fields often give the same nested expression the same type context for every candidate, causing us to repeat the entire nested inference at every level...

This reuses our existing shared expression cache across the candidate checks. We already use the same mechanism during generic-call multi-inference.

The linked issue drops from roughly 1.5s to 0.03s locally.

Closes astral-sh/ty#3663.

@charliermarsh charliermarsh added the performance Potential performance improvement label Jun 4, 2026
@astral-sh-bot astral-sh-bot Bot added the ty Multi-file analysis & type inference label Jun 4, 2026
@astral-sh-bot

astral-sh-bot Bot commented Jun 4, 2026

Copy link
Copy Markdown

Typing conformance results

No changes detected ✅

Current numbers
The percentage of diagnostics emitted that were expected errors held steady at 92.13%. The percentage of expected errors that received a diagnostic held steady at 87.18%. The number of fully passing files held steady at 92/134.

@astral-sh-bot

astral-sh-bot Bot commented Jun 4, 2026

Copy link
Copy Markdown

Memory usage report

Memory usage unchanged ✅

@astral-sh-bot

astral-sh-bot Bot commented Jun 4, 2026

Copy link
Copy Markdown

ecosystem-analyzer results

No diagnostic changes detected ✅

Full report with detailed diff (timing results)

@charliermarsh charliermarsh marked this pull request as ready for review June 4, 2026 20:54
Comment on lines +5 to +6
Contextually checking nested collection literals against a recursive union of `TypedDict`s should
not repeat the same inference for every union arm at every nesting level.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Does the behavior of this test differ on main vs in this PR? This comment (and the PR description) makes it seem like a pure optimization. An mdtest like this won't prevent perf regressions; a benchmark would.

@charliermarsh charliermarsh force-pushed the charlie/fix-recursive-typed-dict-union-performance branch from e25216a to bfe3737 Compare June 4, 2026 21:53
@charliermarsh charliermarsh merged commit a70f81f into main Jun 4, 2026
59 checks passed
@charliermarsh charliermarsh deleted the charlie/fix-recursive-typed-dict-union-performance branch June 4, 2026 22:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance Potential performance improvement ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Slow contextual checking of nested literals against recursive TypedDict unions

3 participants