Skip to content

improve delta perf - #6162

Open
benedikt-bartscher wants to merge 9 commits into
reflex-dev:mainfrom
benedikt-bartscher:faster-delta
Open

benedikt-bartscher wants to merge 9 commits into
reflex-dev:mainfrom
benedikt-bartscher:faster-delta

Conversation

@benedikt-bartscher

@benedikt-bartscher benedikt-bartscher commented Mar 6, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • build_delta and BaseState.dict() read a field that is stored on the state straight from the instance __dict__, instead of calling get_value() for every var.
  • A computed var, or a field whose default was never stored, still goes through get_value().
  • The result is the same. For a field declared on the state's own class, get_value() reads the same __dict__ entry. A field inherited from a parent state is stored on the parent instance, so it is not in the child's __dict__ and takes the old path.

Measurement

  • BaseState.dict() on a state with 20 fields: 5.3 → 2.85 µs.
  • dict() on a tree of 20 substates: 27.9 → 20.4 µs.
  • CodSpeed: test_hydration_snapshot[200] +35%, test_hydration_snapshot[20] +30%, test_computed_dependency_fanout[0] +10%.

Test plan

  • test_build_delta_reads_stored_fields_from_instance_dict, test_build_delta_stores_default_of_unread_field and test_build_delta_sends_inherited_var_under_its_owner cover the fast path, the default and the inherited fallback in build_delta
  • test_dict_reads_stored_fields_from_instance_dict covers BaseState.dict()
  • uv run pytest tests/units
  • ruff check, ruff format --check, pyright

🤖 Generated with Claude Code

@codspeed

codspeed Bot commented Mar 6, 2026 •

Copy link
Copy Markdown

Merging this PR will improve performance by 24.46%

3 warnings

⚠️ 17 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

⚠️ 3 benchmarks with a significant change ran on different hardware

Their base and head results were measured on machines with a different CPU, core count or memory, so the changes might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

⚠️ 3 benchmarks with a significant change ran in a different software environment

Their base and head results were measured with a different OS or toolchain, such as compiler, runtime or library versions, so the changes might come from this difference rather than from the code. Open an affected benchmark to see what changed.

See affected benchmarks

⚡ 3 improved benchmarks
✅ 143 untouched benchmarks
⏩ 8 skipped benchmarks1

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ test_hydration_snapshot[200] 8 ms 5.9 ms +35.3%
⚡ test_hydration_snapshot[20] 867 µs 667.7 µs +29.84%
⚡ test_computed_dependency_fanout[0] 2 ms 1.9 ms +9.76%

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing benedikt-bartscher:faster-delta (86e9dea) with main (df7f157)

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. ↩

@greptile-apps

greptile-apps Bot commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no actionable issue remains in the reviewed changes.

Summary

This PR reduces the work needed to build state updates and full snapshots.

  • State updates and full dictionaries read stored fields directly.

No new actionable issues were found. Tests were inspected but not run.

Reviews (8) · Last reviewed commit: "Read stored fields straight from the ins..." · Reviewed by Greptile

Re-apply the delta fast path on main's restructured get_delta loop:
read the raw field value instead of get_value, which wraps and
immediately unwraps mutable vars in a MutableProxy.

Add the missing news fragment.
@masenf masenf added the perf Performance-improving changes label Sep 29, 2026
Comment thread reflex/istate/delta.py Resolved
Comment thread news/6162.performance.md Resolved
Upstream moved the per-var get_value call in build_delta into the branches
of the loop, so the PR's __dict__ fast path now applies in the plain-var
else branch; uncached computed vars keep upstream's get_value/async handling.
Pin down that stored fields are read from the instance dict while
computed vars and unstored defaults still go through get_value, and
reword the fragment and comment now that get_value no longer wraps
values in a MutableProxy.
Full hydrates went through get_value for every field. Like the delta fast
path, dict() now reads a field already stored on the instance from its
__dict__; a default not stored yet and the computed vars still go through
get_value. base_vars only holds the state's own fields, so none lives on an
ancestor.

dict() on a state with 20 fields: 5.3 us -> 2.85 us; a tree of 20 substates
with 2 fields each: 27.9 us -> 20.4 us.
@benedikt-bartscher
benedikt-bartscher marked this pull request as ready for review November 22, 2026 13:33
@benedikt-bartscher
benedikt-bartscher requested a review from a team as a code owner November 22, 2026 13:33

@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 5 files

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

Comment thread reflex/istate/delta.py Resolved

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

perf Performance-improving changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants