Skip to content

perf(reconciler): coalesce ReactorState DP read in change handlers (#207) - #626

Merged
Alexandre Zollinger Chohfi (azchohfi) merged 3 commits into
mainfrom
azchohfi-coalesce-reactorstate-dp-reads
Jun 22, 2026
Merged

Alexandre Zollinger Chohfi (azchohfi) merged 3 commits into
mainfrom
azchohfi-coalesce-reactorstate-dp-reads

Conversation

@azchohfi

Copy link
Copy Markdown
Collaborator

Closes #207.

Problem

After PR #205, every change-event trampoline read the ReactorState attached DP twice per event:

  1. ChangeEchoSuppressor.ShouldSuppress(control) → fe.GetValue(ReactorAttached.StateProperty)
  2. immediately after, Reconciler.GetElementTag(control) → a second GetValue(StateProperty)

Attached-DP reads are COM-interop calls. On hot paths — TextChanged per keystroke, Slider.ValueChanged during a drag — that is two interop reads per event when one suffices. The generic ControlledPropEntry trampoline was even worse: three reads (payload + suppress + tag).

Fix

Collapse the reads into a single GetValue(StateProperty) per event, with no behavior change to echo suppression:

  • Reconciler.TryGetReactorState(FrameworkElement, out ReactorState?) — one DP read, returns the state.
  • State-based suppression overloads that hold the exact decrement-and-suppress semantics:
    • ChangeEchoSuppressor.ShouldSuppress(ReactorState)
    • ChangeEchoSuppressor.ShouldSuppressEcho(ReactorState, object?)
    • The existing UIElement overloads now read the DP once and delegate to these, so the external contract (and the public WriteSuppressed primitive / ChangeEchoSuppressor) is untouched.
  • Reconciler.TryGetControlEventPayload<T>(ReactorState) — lets the generic descriptor trampoline reuse the already-read state (3 reads → 1).
  • Every change-handler trampoline now reads state once → checks suppression via state → dispatches via state.Element.

This is an internal hot-path optimization only. The value-diff drain-on-suppress logic in ControlledPropEntry (so a stranded pending-echo flag can't swallow the user's next real interaction) is preserved exactly.

Sites touched

  • Core/V1Protocol/Descriptor/PropEntry.cs — ControlledPropEntry.StaticTrampoline (3 reads → 1)
  • Core/V1Protocol/ReactorBindingT.cs
  • Core/V1Protocol/Descriptor/Descriptors/: GridViewDescriptor, ListViewDescriptor, TemplatedFlipViewDescriptor
  • Core/V1Protocol/Handlers/: GridViewHandler, ListViewHandler
  • Core/V1Protocol/TemplatedListLifecycle.cs — ListView / GridView / FlipView
  • Core/Element.cs — TextBox.TextChanged, NumberBox.ValueChanged, AutoSuggestBox.TextChanged, RichEditBox.TextChanged, Expander.Expanding/Collapsed, ListBox.SelectionChanged, CalendarView.SelectedDatesChanged, ComboBox.SelectionChanged, Pivot.SelectionChanged, SelectorBar.SelectionChanged
  • Core/Element.TabView.cs — TabView.SelectionChanged
  • Helpers added in Core/Reconciler.cs and Core/ChangeEchoSuppressor.cs

Measurement

Added an A/B micro-benchmark C207 to the repo's existing PerfBench.ControlModel harness. It holds one tagged TextBox off-tree and runs the change-handler body N times under three variants, so BEFORE and AFTER are measured in the same process under identical conditions:

Variant What it runs Mean ns/event
Direct callback invoke only (floor, no DP read) 22.5
ReactorToday BEFORE — ShouldSuppress(control) + GetElementTag(control) (2 DP reads) 314.4
Reactor AFTER — TryGetReactorState → shared state (1 DP read) 181.2

Delta: ~133 ns/event saved (~42% faster); exactly one COM-interop DP read eliminated. Subtracting the 22.5 ns callback floor, the DP-read cost drops 291.9 ns → 158.7 ns (~46%), i.e. the per-event DP traffic is essentially halved as designed. Allocation is unchanged (gen0 = 0; reported bytes are harness noise).

Methodology: PerfBench.ControlModel.exe --test C207 --variant All --iterations 2000000 --reps 8 --headless on ARM64, Debug. The absolute win per event is small (as the issue notes); the value is confirming a measurable improvement with no regression and no new allocation across the ~24 wiring sites.

Tests

  • tests/Reactor.Tests full suite: 9439 passed, 0 failed, 64 skipped.
  • All EchoSuppress selftests green, including a new TryGetReactorStateCoalescing fixture that asserts (a) TryGetReactorState resolves the same element as GetElementTag, (b) a user edit still fires the callback, and (c) a programmatic echo is still swallowed exactly once.
  • New ChangeEchoSuppressorStateTests (headless xUnit) pin the exact semantics of the state-based ShouldSuppress/ShouldSuppressEcho overloads (scope is non-consuming, counter decrements once, pending-echo drain). The real-FrameworkElement path can't be exercised headlessly, so it's covered by the selftest fixture instead.

)

After PR #205 each change-event trampoline read the ReactorState attached
DP twice per event — once in ChangeEchoSuppressor.ShouldSuppress(control)
(fe.GetValue(StateProperty)) and again in GetElementTag(control). On hot
paths (TextChanged per keystroke, Slider.ValueChanged during drag) that is
two COM-interop DP reads per event.

Collapse the two reads into one:
- Add internal Reconciler.TryGetReactorState(FrameworkElement, out state):
  a single GetValue(StateProperty) read.
- Add state-based overloads ChangeEchoSuppressor.ShouldSuppress(ReactorState)
  and ShouldSuppressEcho(ReactorState, object?) that hold the exact
  decrement-and-suppress semantics; the existing UIElement overloads now
  delegate after one DP read, keeping the external contract (and
  WriteSuppressed) intact.
- Add Reconciler.TryGetControlEventPayload<T>(ReactorState) so the generic
  ControlledPropEntry trampoline collapses three DP reads (payload +
  suppress + tag) into one.
- Rewrite every change-handler trampoline to read state once, check
  suppression inline via state, then dispatch via state.Element.

Sites: PropEntry.ControlledPropEntry, ReactorBindingT, Grid/ListView
descriptors + handlers, TemplatedListLifecycle (ListView/GridView/FlipView),
TemplatedFlipViewDescriptor, and the Element.cs/Element.TabView.cs
trampolines (TextBox, NumberBox, AutoSuggestBox, RichEditBox, Expander
expand/collapse, ListBox, CalendarView, ComboBox, Pivot, SelectorBar,
TabView).

Measurement (PerfBench.ControlModel bench C207, A/B in one process,
2,000,000 iters x 8 reps, ARM64): BEFORE (2 DP reads) 314.4 ns/event vs
AFTER (1 DP read) 181.2 ns/event — ~133 ns (~42%) saved, one COM-interop
read eliminated; floor (callback only) 22.5 ns. No new allocation.

Tests: full Reactor.Tests suite green (9439 passed); all EchoSuppress
selftests green incl. new TryGetReactorStateCoalescing fixture; added
ChangeEchoSuppressorStateTests pinning the state-overload semantics.

Co-authored-by: Copilot <[email protected]>
The TryGetReactorStateCoalescing fixture's invariant-2a check
(calls.All(s => s == "next")) could not distinguish a swallowed echo from
one that fired with the readback value: the controlled write sets
Text = "next", so the echo value is also "next" and a single regressed echo
of ["next"] still satisfied All(s => s == "next"). It proved "no cross-value
callback", not "no callback", contradicting the fixture's own stated
invariant that the programmatic echo is swallowed exactly once.

Snapshot calls.Count before the controlled write and assert it is unchanged
after the render flush, so a missed-suppression regression fails the
fixture. Rename the check label to Coalesce207_TextBox_NoEchoCall to match
the strict semantics. EchoSuppression selftests still green.

Co-authored-by: Copilot <[email protected]>

Copilot AI 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.

Pull request overview

This PR optimizes Reactor’s high-frequency change-event trampolines by coalescing multiple ReactorAttached.StateProperty reads (COM interop) into a single read per event, while preserving existing echo-suppression semantics by routing suppression through new ReactorState-based overloads.

Changes:

  • Added Reconciler.TryGetReactorState(...) + Reconciler.TryGetControlEventPayload<T>(ReactorState) to enable one DP read shared across suppression + dispatch.
  • Updated change-handler trampolines across core elements/descriptors/handlers to use the single-read state path.
  • Added unit + selftest coverage for the new state-based suppression semantics and a perf bench (C207) to quantify the win.

Reviewed changes

Copilot reviewed 16 out of 16 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/Reactor/Core/Reconciler.cs Adds TryGetReactorState and a ReactorState-based payload accessor to avoid extra DP reads.
src/Reactor/Core/ChangeEchoSuppressor.cs Adds ReactorState overloads for suppression checks; UIElement overloads delegate after one DP read.
src/Reactor/Core/V1Protocol/Descriptor/PropEntry.cs Updates descriptor controlled-prop trampoline to reuse a single state read (payload + suppress + element).
src/Reactor/Core/V1Protocol/ReactorBindingT.cs Uses single-read TryGetReactorState in custom-event trampoline.
src/Reactor/Core/V1Protocol/TemplatedListLifecycle.cs Coalesces DP reads in ListView/GridView/FlipView selection trampolines.
src/Reactor/Core/V1Protocol/Handlers/ListViewHandler.cs Coalesces DP reads in ListView handler selection trampoline.
src/Reactor/Core/V1Protocol/Handlers/GridViewHandler.cs Coalesces DP reads in GridView handler selection trampoline.
src/Reactor/Core/V1Protocol/Descriptor/Descriptors/ListViewDescriptor.cs Coalesces DP reads in ListView descriptor selection trampoline.
src/Reactor/Core/V1Protocol/Descriptor/Descriptors/GridViewDescriptor.cs Coalesces DP reads in GridView descriptor selection trampoline.
src/Reactor/Core/V1Protocol/Descriptor/Descriptors/TemplatedFlipViewDescriptor.cs Coalesces DP reads in FlipView selection trampoline (value-diff path).
src/Reactor/Core/Element.cs Updates multiple built-in control trampolines (TextBox/NumberBox/etc.) to use the single-read state path.
src/Reactor/Core/Element.TabView.cs Updates TabView selection trampoline to use the single-read state path.
tests/Reactor.Tests/ChangeEchoSuppressorStateTests.cs Adds headless unit tests pinning state-overload suppression semantics.
tests/Reactor.AppTests.Host/SelfTest/Fixtures/EchoSuppressionFixtures.cs Adds selftest fixture asserting coalescing resolves the same element + preserves echo behavior end-to-end.
tests/Reactor.AppTests.Host/SelfTest/SelfTestFixtureRegistry.cs Registers the new selftest fixture in both required registry locations.
tests/perf_bench/PerfBench.ControlModel/Benches/AllBenches.cs Adds C207 micro-benchmark to measure the DP-read coalescing delta in-process.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/Reactor/Core/Reconciler.cs Resolved
…e)] (#207)

Addresses Copilot PR review feedback on #626. With the nullable contract

expressed via [NotNullWhen(true)], flow analysis knows state is non-null

after a true return, so the ~23 trampoline call sites drop the redundant

'|| state is null' guard and simplify to 'if (!TryGetReactorState(...)) return;'.

Co-authored-by: Copilot <[email protected]>

Copilot AI 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.

Pull request overview

Copilot reviewed 16 out of 16 changed files in this pull request and generated no new comments.

@azchohfi
Alexandre Zollinger Chohfi (azchohfi) merged commit ea1d807 into main Jun 22, 2026
18 checks passed
@azchohfi
Alexandre Zollinger Chohfi (azchohfi) deleted the azchohfi-coalesce-reactorstate-dp-reads branch June 22, 2026 22:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Coalesce ReactorState DP read in change-event handlers (perf)

2 participants