Significantly optimise checking of repeatedly-instantiated TypedDict objects - #4489
Open
alexander-beedie wants to merge 1 commit into
Open
Significantly optimise checking of repeatedly-instantiated TypedDict objects#4489alexander-beedie wants to merge 1 commit into
TypedDict objects#4489alexander-beedie wants to merge 1 commit into
Conversation
TypedDictsTypedDicts objects
TypedDicts objectsTypedDict objects
Contributor
|
This pull request has been imported. If you are a Meta employee, you can view this in D115363524. (Because this pull request was imported automatically, there will not be any future comments.) |
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
Contributor
Contributor
|
TypedDict stuff would normally be under my wheelhouse but I don't have any capacity in the near future so I'll let Sam handle this |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Identified a somewhat niche hotspot relating to
TypedDict; with this PR in place, type-checkingpydanticgets ~2x faster (measured ~60% less CPU time), and two other packages get a ~6-10% improvement. Pretty much a no-op otherwise.Problem
Instantiating
TypedDictfields rebuilt a fullSmallMapof field types on every call, with each rebuild deep-copying every field type; callers that only iterate paid that cost repeatedly.Solution
Solver, in such a way that repeat calls share one allocation (instead of re-cloning each field's type tree).protocol_cacherule of caching only Var-free types).FieldOutcome).InitVaron aTypedDictmember (it's not valid and blocked caching).ClassFieldjust to read it.Test Plan
All existing unit tests pass without modification.
Several new unit tests added.
Benchmarks1
Huge positive impact on
pydantic(thepydantic_core.core_schemamodule is an absolute wall ofTypedDict) 🚀The speedup tracks with repeated instantiation of the same
TypedDict, not the overallTypedDictcount, and it seems that's not a common pattern; only a couple of packages show much benefit (aside frompydanticthe only other solid gains I spotted are fornumpyandnarwhals, which both get a bit faster).CPU time (user+sys)
My machine was a bit contended, so I recorded CPU-time instead of wall-clock as it was much more stable (measurements are standard multithreaded runs):
Footnotes
Test machine: Apple Silicon M3 Max (16 cores). ↩
@MarcoGorelli, "you're welcome" 🤣 ↩