Skip deep type-validation on state var hot paths#6738
Open
Alek99 wants to merge 3 commits into
Open
Conversation
Assigning a state var and reading a computed var both ran _isinstance(value, type, nested=1), walking every element of list/dict values only to gate a diagnostic log. The computed var check also ran on every access, including cache hits. - Validate computed var return types only when the value is recomputed (sync and async), not on cache hits. - Validate one container level deep only in dev mode; production now checks just the outer type (the check never gates behavior, it only logs). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01G8cXh3TUbNtbjm2ERqE62X
Contributor
Greptile SummaryThis PR reduces type-validation work on state hot paths. The main changes are:
Confidence Score: 5/5This looks safe to merge.
Important Files Changed
Reviews (3): Last reviewed commit: "Honor runtime env mode changes in _valid..." | Re-trigger Greptile |
Merging this PR will not alter performance
Comparing Footnotes
|
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01G8cXh3TUbNtbjm2ERqE62X
Address review feedback: instead of caching the first observed mode forever, re-read the raw REFLEX_ENV_MODE value on every call and cache the depth per raw value, so in-process mode changes take effect immediately at negligible hot-path cost. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01G8cXh3TUbNtbjm2ERqE62X
4 tasks
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.
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:
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)
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_typeno longer on the cache-hit path) and in prod the whole workload is 538 function calls / 0.003s.Type of change
Changes To Core Features:
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.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