Skip to content

Conversation

@benedikt-bartscher
Copy link
Contributor

@benedikt-bartscher benedikt-bartscher commented Feb 7, 2026

For one of my apps this reduces the app import time over 50%

@codspeed-hq
Copy link

codspeed-hq bot commented Feb 7, 2026

CodSpeed Performance Report

Merging this PR will not alter performance

Comparing benedikt-bartscher:perf/cache-get-type-hints (a83bdf9) with main (5d1d6db)

Summary

✅ 8 untouched benchmarks

@benedikt-bartscher benedikt-bartscher marked this pull request as ready for review February 7, 2026 13:59
@greptile-apps
Copy link
Contributor

greptile-apps bot commented Feb 7, 2026

Greptile Overview

Greptile Summary

This PR adds caching to the _get_type_hints() method using @functools.cache, significantly improving import performance (over 50% reduction for the author's app). The changes are well-structured:

  • Added @functools.cache decorator to BaseState._get_type_hints() to cache type hints per class
  • Updated _check_overridden_basevars() to use the cached method instead of calling get_type_hints() directly
  • Added proper type annotation (type[BaseState]) to is_backend_base_variable() function signature
  • Removed defensive checking in is_backend_base_variable() since all callers are guaranteed to pass BaseState subclasses

The caching strategy is sound because:

  • Type hints are static after class definition and don't change at runtime
  • The pattern follows existing usage of @functools.cache with classmethods in the codebase (e.g., Component.get_props())
  • Each class gets its own cache entry, so inheritance works correctly
  • The method already handles dynamic substates properly via __original_module__

Confidence Score: 5/5

  • This PR is safe to merge with no identified risks
  • The changes are simple, focused, and follow established patterns in the codebase. Caching get_type_hints() is safe because type hints are immutable after class definition. The removal of defensive checking is justified by type annotations ensuring only BaseState subclasses are passed. No edge cases or potential bugs identified.
  • No files require special attention

Important Files Changed

Filename Overview
reflex/state.py Added @functools.cache decorator to _get_type_hints() method and updated _check_overridden_basevars() to use cached result - improves performance with no side effects
reflex/utils/types.py Added type annotation for BaseState, removed defensive check in is_backend_base_variable(), and simplified to always call cls._get_type_hints() - safe given all callers pass BaseState subclasses

Sequence Diagram

sequenceDiagram
    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
Loading

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

2 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

@benedikt-bartscher benedikt-bartscher force-pushed the perf/cache-get-type-hints branch from 35a2450 to a83bdf9 Compare February 7, 2026 20:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant