Skip deep type-validation on state var hot paths
Linear: ENG-10093
Description
Two hot paths ran _isinstance(value, type, nested=1), which walks every element of list/dict values, only to gate a diagnostic log that never changes behavior:
BaseState.__setattr__validated the full container on every assignment.ComputedVar.__get__/AsyncComputedVar.__get__validated the return type on every access, including cache hits.
Changes:
- Computed var return types are now validated only when the value is actually recomputed (sync and async). Cache hits skip the check entirely.
- Element-wise validation depth is now
1in dev and0in prod (new cached_validation_depth()helper inreflex_base.utils.types), so production only checks the outer type. Dev keeps the exact same diagnostics as before.
Benchmarks (GitHub Actions runner, run, 2 passes each)
| Case | main | this PR | speedup |
|---|---|---|---|
| read cached computed var returning 10k dicts (dev) | 13.81 / 14.05 ms | 0.0015 / 0.0015 ms | ~9,200x |
| read cached computed var (prod) | 13.66 / 13.93 ms | 0.0013 / 0.0015 ms | ~9,300x |
| assign 100k-int list (prod) | 90.89 / 90.96 ms | 0.020 / 0.024 ms | ~4,000x |
| assign 100k-int list (dev) | 90.74 / 92.67 ms | 91.76 / 94.74 ms | unchanged (by design) |
cProfile (3 assigns + 20 cached reads, dev): main spends 3.06s in 10.5M calls dominated by _isinstance (510k calls); with this PR the cached-read side disappears (_check_deprecated_return_type no longer on the cache-hit path) and in prod the whole workload is 538 function calls / 0.003s.
Type of change
- Bug fix (non-breaking change which fixes an issue)
Changes To Core Features:
- Have you added an explanation of what your changes do and why you'd like us to include them?
- Have you written new tests for your core changes, as applicable?
tests/units/reflex_base/vars/test_base.py: return type checked on recompute, not on cache hits (sync, async, andcache=False).tests/units/reflex_base/utils/test_types.py:_validation_depth()is 0 in prod / 1 in dev.tests/units/test_state.py: wrong-typed assignment still logs an error in dev.
- Have you successfully ran tests with your changes locally?
Note: behavior-wise, a wrong-typed computed var now logs once per recompute instead of once per access, and prod no longer walks container elements for the log-only check. No exceptions or control flow depend on these checks.
🤖 Generated with Claude Code
https://claude.ai/code/session_01G8cXh3TUbNtbjm2ERqE62X
Generated by Claude Code