perf: skip redundant state var type checks on the hot path

Merged
FA
2mo ago
+260-23

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 #6743Status on main
Dirty-propagation frontierCovered by #7312: Field.__set__ calls _mark_dirty((name,)), so a write walks only the dependents of the var that changed.
_interval_computed_varsCovered by #7025 (_interval_computed_var_names).
Return-type check on recompute onlyThis PR.
Shallow type check in prodThis PR.
Per-field MutableProxy cacheLeft out. See below.

Changes

  1. A cached computed var checks its return type only when it recomputes (sync and async). Before, every read ran _check_deprecated_return_type with nested=1, even on a cache hit, in every mode.
  2. 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 from REFLEX_ENV_MODE on 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_types short-circuits first.

Measurements

Micro-benchmarks, in-process, py3.14, median of 15:

mainthis PR
Read a cached computed var returning about 250 dicts (any mode)321 µs0.43 µs
Assign a list of 500 dicts (prod)248 µs2.6 µs
Assign a list of 500 dicts (dev)247 µs248 µs
Assign an int (prod)2.5 µs2.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:

Handlermain p50 RTTthis PR p50 RTTCPU per event
Reassign a 500-row list + dependent computed var2651 [2541–3099] µs2156 [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.py fail 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.