cache get_type_hints for heavy init subclass stuff - improve import performance#6118
Merged
adhami3310 merged 4 commits intoreflex-dev:mainfrom Feb 13, 2026
Merged
Conversation
Contributor
Greptile OverviewGreptile SummaryThis PR adds caching to the
The caching strategy is sound because:
Confidence Score: 5/5
Important Files Changed
Sequence DiagramsequenceDiagram
participant App as Application Code
participant BS as BaseState
participant Cache as functools.cache
participant GTH as get_type_hints
participant Types as types.is_backend_base_variable
Note over App,Types: Initial State Class Import/Initialization
App->>BS: Define State subclass
BS->>BS: _check_overridden_basevars()
BS->>Cache: Call _get_type_hints()
Cache->>Cache: Check if cached for this class
alt Not Cached
Cache->>GTH: get_type_hints(cls, localns)
GTH-->>Cache: Return type hints dict
Cache->>Cache: Store in cache
end
Cache-->>BS: Return cached hints
Note over App,Types: Backend Variable Check Flow
App->>BS: Access backend_vars during init
BS->>Types: is_backend_base_variable(name, mixin_cls)
Types->>BS: Call cls._get_type_hints()
BS->>Cache: Retrieve from cache
Cache-->>BS: Return cached hints (no recomputation)
BS-->>Types: Return hints
Types-->>BS: Return bool result
Note over Cache: Performance Improvement: Subsequent calls skip get_type_hints computation
|
35a2450 to
a83bdf9
Compare
adhami3310
previously approved these changes
Feb 12, 2026
Member
|
in a followup, we should also add diff --git a/reflex/state.py b/reflex/state.py
index 89698cfa..b87f2e4a 100644
--- a/reflex/state.py
+++ b/reflex/state.py
@@ -569,14 +569,14 @@ class BaseState(EvenMoreBasicBaseState):
new_backend_vars = {
name: value if not isinstance(value, Field) else value.default_value()
- for mixin_cls in [*cls._mixins(), cls]
+ for mixin_cls in (*cls._mixins(), cls)
for name, value in list(mixin_cls.__dict__.items())
if types.is_backend_base_variable(name, mixin_cls)
}
# Add annotated backend vars that may not have a default value.
new_backend_vars.update({
name: cls._get_var_default(name, annotation_value)
- for mixin_cls in [*cls._mixins(), cls]
+ for mixin_cls in (*cls._mixins(), cls)
for name, annotation_value in mixin_cls._get_type_hints().items()
if name not in new_backend_vars
and types.is_backend_base_variable(name, mixin_cls)
@@ -764,7 +764,7 @@ class BaseState(EvenMoreBasicBaseState):
return getattr(cls, unique_var_name)
@classmethod
- def _mixins(cls) -> list[type]:
+ def _mixins(cls) -> list[type[BaseState]]:
"""Get the mixin classes of the state.
Returns:to improve type safety around those functions that call this |
masenf
reviewed
Feb 12, 2026
Collaborator
masenf
left a comment
There was a problem hiding this comment.
do you get the same performance improvements by pulling the get_type_hints out of the for loop without adding the functools cache?
Contributor
Author
no, 4,103s total vs 7,244s total |
Contributor
Author
|
@adhami3310 thanks for your input, i just migrated it to tuple and applied stricter typing |
adhami3310
approved these changes
Feb 13, 2026
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.
For one of my apps this reduces the app import time over 50%