Skip to content

Short-circuit failed TypedDict dictionary equivalence - #11771

Merged
Rich Chiodo (rchiodo) merged 1 commit into
mainfrom
rchiodo-performance-regression-investigation
Sep 15, 2026
Merged

Rich Chiodo (rchiodo) merged 1 commit into
mainfrom
rchiodo-performance-regression-investigation

Conversation

@rchiodo

Copy link
Copy Markdown
Collaborator

Summary

Return immediately from getTypedDictDictEquivalent when a field is read-only, required, or fails the existing invariant-assignability check. Previously, evaluation continued through every remaining field, repeatedly constructing unions even though the result was already known to be undefined.

The equivalence rules and successful return types are unchanged. This implements the early-exit optimization discussed in #11759; the separate synthesized update() tuple-union optimization is not included.

Credit

Credit to Wizzerinus | Alex K. (@Wizzerinus), the author of #11759, for identifying the unnecessary work, proposing the early-exit solution, and providing the hetero_500.py reproduction.

Performance

Using the unmodified 500-field attachment from #11759, with the same local build setup before and after this change, three separate CLI runs produced:

Type-checking time (--stats Check) Before After
Median 3.72 s 0.47 s
Range 3.40–4.47 s 0.47–0.48 s

This is approximately 7.9× faster, or an 87% reduction in checking time for this reproduction. Measurements exclude startup overhead and are local reproduction measurements, not a general performance guarantee. All runs reported zero errors and zero warnings.

Validation

  • TypeScript build passed.
  • All 13 existing TypedDictClosed and TypedDictReadOnly cases in typeEvaluator5.test.ts passed; existing expectations were unchanged.
  • Targeted Prettier, ESLint, and git diff --check passed.

Related to #11759. This is separate from the overload-materialization regression in #11751 and its rollback in #11739.

Fixes: #11751

Return immediately when a field makes dictionary equivalence fail, avoiding unnecessary union construction and assignability checks for remaining fields.

Credit @Wizzerinus for identifying this optimization in #11759.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@bschnurr

Copy link
Copy Markdown
Member

🔒 Automated review in progress — Bill Schnurr (@bschnurr) is auto-reviewing this PR.

@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. ✅

@bschnurr

Copy link
Copy Markdown
Member

Result: ⚠️ needs-more-tests

Verification details

Verification: Isolated verification observed failures that were not classified as caused by this PR: Dependency and test discovery, 500-field short-circuit CLI reproduction. The relevant tests could not be fully run in the isolated environment; this review is not fully verified.

Summary: The 13 targeted TypedDict tests passed, covering closed, required, and read-only cases, and the TypeScript build passed. No tests were added by the PR. A generated 500-field performance reproduction could not be executed because my compiled CLI invocation was malformed, so the claimed short-circuit performance improvement remains unverified. Existing tests validate unchanged diagnostics but do not assert that processing stops after the first failed field.

Test runs: 3 passed, 2 failed, 1 not run

  • ❌ Failed | unrelated to this PR | Dependency and test discovery | printf 'SANDBOX_PROFILE=%s\n' "${AUTOMATION_SANDBOX_PROFILE:-}"; git diff --name-status HEAD^ HEAD; git diff --stat HEAD^ HEAD; test -d node_modules && echo root_node_modules=present || echo root_node_modules=missing; test -d packages/pyright-internal/node_modules && echo package_node_modules=present || echo package_node_modules=missing; node --version; cat package.json | sed -n '1,160p'; printf '\n--- pyright-internal scripts ---\n'; node -e "const p=require('./packages/pyright-internal/package.json'); console.log(JSON.stringify(p.scripts,null,2))"; printf '\n--- changed test references ---\n'; git diff --unified=0 HEAD^ HEAD -- packages/pyright-internal/src/tests packages/pyright-internal/src/tests/samples || true
  • ⚠️ Not run | Initial 500-field CLI reproduction attempt | node -e "const fs=require('fs'); let s='from typing import Literal, Never, NotRequired, TypedDict\n\nclass Heterogeneous(TypedDict, extra_items=Never):\n required_head: int\n'; for(let i=0;i<500;i++) s += ' field_'+i+': NotRequired[Literal['+i+']]\n'; s += '\ndef exercise(x: Heterogeneous) -> None:\n x.update({})\n'; fs.writeFileSync('/tmp/pyright-short-circuit.py',s)" && node ./out/packages/pyright-internal/src/pyright.js /tmp/pyright-short-circuit.py --stats
  • ❌ Failed | unrelated to this PR | 500-field short-circuit CLI reproduction | node -e "const fs=require('fs'); let s='from typing import Literal, Never, NotRequired, TypedDict\n\nclass Heterogeneous(TypedDict, extra_items=Never):\n required_head: int\n'; for(let i=0;i<500;i++) s += ' field_'+i+': NotRequired[Literal['+i+']]\n'; s += '\ndef exercise(x: Heterogeneous) -> None:\n x.update({})\n'; fs.writeFileSync('/tmp/pyright-short-circuit.py',s)" && node -e "process.argv.splice(1,0,'pyright'); require('./out/packages/pyright-internal/src/pyright.js').main()" /tmp/pyright-short-circuit.py --stats
  • ✅ Passed | TypedDictClosed and TypedDictReadOnly evaluator tests | node ./node_modules/jest/bin/jest.js src/tests/typeEvaluator5.test.ts --runInBand --runTestsByPath -t 'TypedDict(Closed|ReadOnly)'
  • ✅ Passed | pyright-internal TypeScript build | pnpm build
  • ✅ Passed | Offline dependency bootstrap | pnpm install --offline --frozen-lockfile
❌ Dependency and test discovery diagnostic output
SANDBOX_PROFILE=typescript
error: Could not access 'HEAD^'
error: Could not access 'HEAD^'
root_node_modules=missing
package_node_modules=missing
v24.15.0
⚠️ Initial 500-field CLI reproduction attempt diagnostic output
The compiled module exited without invoking its exported main function and produced no statistics.
❌ 500-field short-circuit CLI reproduction diagnostic output
File or directory "file:///workspace/packages/pyright-internal/pyright" does not exist

@bschnurr Bill Schnurr (bschnurr) left a comment

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.

Approved via Review Center.

@rchiodo
Rich Chiodo (rchiodo) merged commit 8854cff into main Sep 15, 2026
20 checks passed
@bschnurr Bill Schnurr (bschnurr) added the review-auto:approved Automated review: no blocking findings (approval posted). label Sep 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

review-auto:approved Automated review: no blocking findings (approval posted).

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Degraded performance in versions >=1.1.412, worse with multiple threads

2 participants