perf: skip redundant state var type checks on the hot path
Summary
Supersedes #6743. That PR is stacked on #6735, which was closed without merging, and it conflicts with main. Most of it is already covered on main:
| Part of #6743 | Status on main |
|---|---|
| Dirty-propagation frontier | Covered by #7312: Field.__set__ calls _mark_dirty((name,)), so a write walks only the dependents of the var that changed. |
_interval_computed_vars | Covered by #7025 (_interval_computed_var_names). |
| Return-type check on recompute only | This PR. |
| Shallow type check in prod | This PR. |
Per-field MutableProxy cache | Left out. See below. |
Changes
- A cached computed var checks its return type only when it recomputes (sync and async). Before, every read ran
_check_deprecated_return_typewithnested=1, even on a cache hit, in every mode. - In prod mode, state var assignments and computed var results check only the outer type (
nested=0). The check only logs an error. Dev mode keeps the element check. The mode is read fromREFLEX_ENV_MODEon each call, with an exact comparison that matches the canonical enum parser.environment.REFLEX_ENV_MODE.get()costs about 2.4 µs, too much for every write. Scalar values never reach this check, because_plain_typesshort-circuits first.
Measurements
Micro-benchmarks, in-process, py3.14, median of 15:
| main | this PR | |
|---|---|---|
| Read a cached computed var returning about 250 dicts (any mode) | 321 µs | 0.43 µs |
| Assign a list of 500 dicts (prod) | 248 µs | 2.6 µs |
| Assign a list of 500 dicts (dev) | 247 µs | 248 µs |
Assign an int (prod) | 2.5 µs | 2.6 µs |
End to end, over a websocket: prod backend, one granian worker, server and client on separate pinned cores. There were 7 interleaved rounds of 2,000 serial events (600 for the reassign). Values are the median and [min–max] of the 7 rounds:
| Handler | main p50 RTT | this PR p50 RTT | CPU per event |
|---|---|---|---|
| Reassign a 500-row list + dependent computed var | 2651 [2541–3099] µs | 2156 [2114–2179] µs | −18%, 7/7 rounds |
count += 1, list append, nested mutation, substate | — | — | no change expected. Moves of ±5–12% in both directions are run-to-run noise, because this diff does not touch those paths. |
The win shows only for handlers that assign large containers or read cached computed vars. Typical scalar and in-place mutation events do not change.
Not included: the proxy cache
In the same A/B, caching the top-level MutableProxy per field saved about 1.1 µs per read. It gave −13% only in a handler that read self.items 50 times, and nothing on append or nested mutation. It also changes behavior: two reads return the same proxy, so overlapping async with self.items blocks now raise RuntimeError: Mutable proxy is already in an async context. It also needs an extra slot and an extra pop on every tracked write. Background tasks get no benefit, because StateProxy re-wraps on every read.
Tests
- New tests in
tests/units/reflex_base/vars/test_base.pyfail on main and pass here:- A sync or async cached computed var checks its return type once per recompute.
- The element-type error is logged in dev and not in prod.
- The outer-type error is logged in both modes.
uv run pytest tests/units: 11043 passed.- ruff, pyright and pre-commit are clean.