Cache structs.fields() result for non-generic structs#997
Closed
Siyet wants to merge 1 commit intojcrist:mainfrom
Closed
Cache structs.fields() result for non-generic structs#997Siyet wants to merge 1 commit intojcrist:mainfrom
Siyet wants to merge 1 commit intojcrist:mainfrom
Conversation
Store the computed tuple of FieldInfo on the class as __struct_field_info__ to avoid expensive get_class_annotations() and FieldInfo recreation on every call. Generic aliases are not cached since type parameters affect annotations. Closes #963
Collaborator
Author
|
Resolved in the community fork msgspec-arise: PR #13, released in v0.20.2. |
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
fields()result tuple on the struct class as__struct_field_info__get_class_annotations()call andFieldInfoobject recreation on every invocationMyStruct[int]) are not cached since type parameters affect annotationsCloses #963
Test plan
Ubuntu 22.04 (WSL2, x86_64, gcc 11.4, Python 3.10):
Ubuntu 24.04 (WSL2, x86_64, gcc 13.3, Python 3.12):
New tests:
test_fields_cached- second call returns the same object (is)test_fields_cached_instance- class and instance share the same cachetest_fields_generic_not_cached_across_params-Example[str]andExample[int]return different resultsBenchmark (1M calls, 3 fields):
Ubuntu 22.04, Python 3.10:
dataclasses.fields()msgspec.structs.fields()Ubuntu 24.04, Python 3.12:
dataclasses.fields()msgspec.structs.fields()