Skip to content

perf: re-render state readers only when a field they read changes - #7583

Merged
FarhanAliRaza merged 8 commits into
reflex-dev:mainfrom
FarhanAliRaza:farhan/field-level-state-subscriptions
Nov 20, 2026
Merged

FarhanAliRaza merged 8 commits into
reflex-dev:mainfrom
FarhanAliRaza:farhan/field-level-state-subscriptions

Conversation

@FarhanAliRaza

@FarhanAliRaza FarhanAliRaza commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Follow-up to #7441, which put all substates in one shared store.

Problem

A component that reads one field of a state re-renders when any field of that state changes. A state with 20 fields shown in 20 places re-renders all 20 places when one field changes.

Change

  • VarData.from_state(state, field) emits useStateContext("S", ["field_rx_state_"]). Without a field, the hook has no list, as before.
  • _render_hooks merges the hooks of a component into one useStateContext call per state, with the union of the fields (_merge_state_hooks). A hook without a list reads the whole substate and wins over the field lists.
  • useStateContext(name, fields) keeps the previous snapshot while the listed fields are unchanged, so useSyncExternalStore skips the render. The store does not change.
  • Var._var_set_state adds the whole-substate hook, because its JS expression can read any key.

Event handlers in the component read the same object. Their arguments are vars of the component, so their fields are in the list and their closures stay current.

Measured

Own small shop app (8 states, /big page with a 20-field state), prod build, React commit hook counting the function components that render per section. main = 5ae5dcd528.

Action main this PR
change 1 of 20 shown fields 20 renders 1
change last_echo (the echo button does not read it) echo buttons 3 + output 1 output 1
counter inc counter section 2 1
no-op event 0 0

Final values are the same on both. No page errors.

A second app (14 sections: cond, foreach, dict and list changes, handler arguments, a controlled input, a substate, rx.memo, two ComponentState instances, a yielding handler), prod build, 16 actions, base 2d71015b7: 401 renders on main, 75 with this PR, same final values, no console errors. Same result after merging main at a933f52c5.

Field coverage check on compiled output: for each compiled component, every stateVar.field read is in that component's field list.

  • shop app: 34 state hooks, all with a list
  • examples/playground: 216 state hooks, all with a list, no missing field

Hot reload in dev: adding a field, a text and an event to a mounted component shows the new default value, and both the new and the old fields update on click.

Known limit

A hand-written Var that reads a state field in raw JS, with no VarData.from_state for that field, gets no subscription of its own. Before this PR it was correct only when another var of the same state was in the same component. With this PR, that component re-renders only for the fields in its list, so the hand-written read can show a stale value.

View guided diff

Each state var hook now names its field, the compiler merges the hooks of a
component into one useStateContext call per state with the union of fields,
and useStateContext keeps the previous snapshot while those fields are
unchanged. A hook without a field list still reads the whole substate.
@FarhanAliRaza
FarhanAliRaza requested a review from a team as a code owner October 16, 2026 17:29
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-16T17:38:25.681604Z 4e5cd02 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@FarhanAliRaza FarhanAliRaza added the perf Performance-improving changes label Oct 16, 2026

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All reported issues were addressed across 9 files

Tip: Get a Slack DM when cubic finishes reviewing your PRs. Connect Slack

View guided diff | Re-trigger cubic

@greptile-apps

greptile-apps Bot commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no new actionable issues were found.

Summary

Components now re-render only when a state field they read changes.

  • State readers now re-render only when a field they use changes.

Reviews (6) · Last reviewed commit: "fix: key the field subscription memo onl..." · Reviewed by Greptile

Its expression is not a field name, so a field hook built from it could hold
a ']' the hook merge cannot parse, leaving two declarations of the state.
@codspeed

codspeed Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ 13 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 146 untouched benchmarks
⏩ 8 skipped benchmarks1


Comparing FarhanAliRaza:farhan/field-level-state-subscriptions (054714a) with main (1609525)

Open in CodSpeed

Footnotes

  1. 8 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4e5cd02fb9

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@FarhanAliRaza

Copy link
Copy Markdown
Contributor Author

A/B on real apps: main 5ae5dcd528 vs this PR dae7365a8c

Setup:

  • Apps: examples/playground (the benchmark app: many pages, a sqlite table with 2000 rows, forms) and a small shop app of my own.
  • Builds: prod builds of both sides from the same worktree. For main, main's versions of the 3 changed source files are checked out.
  • Counting: a React DevTools commit hook counts every function component that renders after each action.

Playground renders

Action main PR
events: sync +1 / args +5 / async +10 15 each 3 each
events: generator (progress updates) 92 51
events: clear 15 12
data: filter "lamp" 104 17
data: next page 370 282
data: reset 374 287
counter +1 3 3
forms submit 236 236

The counter and forms pages do not change, because their states have only one or two shown fields.

Shop app renders

Action main PR
change 1 of 20 shown fields 20 1
change a field the echo buttons send but do not show 4 3
change a field only the output shows 4 1

On both apps, every final value on the page is the same as on main, and there are no page errors.

React Doctor 0.9.18 (offline, on the compiled .web sources)

App main PR different findings
shop 2 2 none
playground 234 234 none

There is no finding on context-registry.js. The 234 playground findings are already on main: 230 exhaustive-deps on generated useCallback dependency lists that include addEvents and ReflexEvent without need, and 4 prefer-dynamic-import.

masenf commented Oct 16, 2026

Copy link
Copy Markdown
Collaborator

Possible hook-ordering issue in _merge_state_hooks: it looks like merging two field subscriptions can lose an INTERNAL position requirement and put the state declaration after a hook that reads it.

Compared main 5ae5dcd5281497b395bd9ec6b7b6ca229e293f39 with this head dae7365a8cfd8165ddfa63b5d633a99d7b613d30. The default compiler pipeline produced a component that rendered 2 on main, but threw ReferenceError: Cannot access 'reflex___state____state____main_____s' before initialization on this head in React 19.2.0 test-renderer.

Here is the compiler reproducer. Save as repro_order.py in a checkout with its Python dependencies installed, then run uv run python repro_order.py > generated.js on each revision (separate worktrees are convenient):

from dataclasses import dataclass
from typing import Callable

import reflex as rx
from reflex_base.constants.compiler import MemoizationMode
from reflex_base.plugins import CompileContext, CompilerHooks
from reflex_base.registry import RegistrationContext
from reflex.compiler.plugins import default_page_plugins
from reflex.compiler.compiler import compile_memo_components

@dataclass
class Page:
    route: str
    component: Callable
    title: str = ""
    description: str = ""
    image: str = ""
    meta: tuple = ()
    on_load: object = None
    _source_module: str | None = None

with RegistrationContext():
    class S(rx.State):
        a: int = 1
        b: int = 2

    class ParentHook(rx.el.Div):
        _memoization_mode = MemoizationMode(recursive=False)

        def add_hooks(self):
            return [rx.Var(f"const parentRead = {S.a}", _var_type=int)]

    def page():
        local = rx._x.client_state(
            "childValue", default=S.b, global_ref=False
        )
        return ParentHook.create(rx.el.div(local, local.value))

    ctx = CompileContext(
        pages=[Page(route="/p", component=page)],
        hooks=CompilerHooks(plugins=default_page_plugins()),
    )
    with ctx:
        ctx.compile()
    modules, _ = compile_memo_components(
        memos=tuple(ctx.auto_memo_components.values())
    )
    for _, source in modules:
        if "parentRead" in source:
            print(source)

On the PR, the relevant output is ordered like this (state identifier shortened):

const [childValue, setChildvalue] = useState(s.b_rx_state_)
const s = useStateContext("...", ["a_rx_state_", "b_rx_state_"])

Main declares s first. The parent's add_hooks dependency has the default/PRE_TRIGGER position; the child's state dependency requires INTERNAL. These now have different field-specific hook keys, and the merge keeps only the first hook's metadata. Preserving the earliest required position across the merged hooks might fix this. A mixed-position regression test would help; the current merge tests use INTERNAL for every input.

The S.a interpolation carries generated Var metadata. This uses the experimental client_state API, which accepts a Var default, and a supported nonrecursive memo boundary.

Validation scope: full default Python compiler pipeline plus the generated modules in real React/react-test-renderer 19.2.0, with imports remapped for standalone execution (Emotion jsx mapped to React.createElement). The renderer mounted the generated component under the actual StateStoreContext.Provider, with the actual store seeded with a_rx_state_: 1, b_rx_state_: 2. No full browser/Vite run. Existing diagnostic Python dependencies were reused; fresh-environment dependency installation was not tested.

Main now renders hooks in declaration order (reflex-dev#7586), so _sort_hooks is
gone. _render_hooks merges the field subscriptions to each state into the
first of them, which comes before every hook that reads that state.

Port the template tests to _merge_state_hooks and add a regression test
for a parent add_hooks Var and a child hook that read different fields
of one state: the merged subscription must render before both readers.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01A4NL3Z8zkVzRHmm7pAkaJu
The grid reads two fields of one state, so it collects a field
subscription for each. Compare against the hooks as they render, with
the subscriptions merged into one useStateContext call.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01A4NL3Z8zkVzRHmm7pAkaJu
@FarhanAliRaza FarhanAliRaza added the on deck PRs lined up to review / merge next label Nov 12, 2026
Resolve _render_hooks: keep the client state declaration check and render
the merged state hooks. Read context-registry.js from its new template path.

@masenf masenf left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

i really do like this PR, i think it will help us reduce superfluous re-renders throughout the builder and docs apps without having to pay a ton of attention about how one's state and UI tree are constructed

@FarhanAliRaza
FarhanAliRaza requested a review from masenf November 20, 2026 17:31
@FarhanAliRaza
FarhanAliRaza merged commit df7f157 into reflex-dev:main Nov 20, 2026
168 checks passed
benedikt-bartscher added a commit to benedikt-bartscher/reflex that referenced this pull request Nov 22, 2026
…inification

- reflex-dev#7583's field subscription goes on the state's local, which a resolver
  can prefix with `$rx_`; the two hook tests expect the field list.
- reflex-dev#7938's wait for the lost-and-found subscription runs after the per-SID
  setup, which has to happen before the connect's first await, and still
  before link_token_to_sid stores the socket record.
benedikt-bartscher added a commit to benedikt-bartscher/reflex that referenced this pull request Nov 22, 2026
Brings upstream to df7f157 (reflex-dev#7583) and the hook merge fix for `$rx_`
locals. reflex-dev#7583's hook names the state the way the frontend does and lists
each field by format_var_key, the key the resolver gives it in the store;
_var_set_state records the default state name like every other VarData.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

on deck PRs lined up to review / merge next perf Performance-improving changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants