Repository navigation
perf(reconciler): coalesce ReactorState DP read in change handlers (#207) - #626
Merged
Alexandre Zollinger Chohfi (azchohfi) merged 3 commits intoJun 22, 2026
Conversation
) 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]>
Alexandre Zollinger Chohfi (azchohfi)
requested a review
from Chris Anderson (codemonkeychris)
as a code owner
June 22, 2026 18:34
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]>
Contributor
There was a problem hiding this comment.
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
statepath. - 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.
…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]>
Alexandre Zollinger Chohfi (azchohfi)
deleted the
azchohfi-coalesce-reactorstate-dp-reads
branch
June 22, 2026 22:50
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #207.
Problem
After PR #205, every change-event trampoline read the
ReactorStateattached DP twice per event:ChangeEchoSuppressor.ShouldSuppress(control)→fe.GetValue(ReactorAttached.StateProperty)Reconciler.GetElementTag(control)→ a secondGetValue(StateProperty)Attached-DP reads are COM-interop calls. On hot paths —
TextChangedper keystroke,Slider.ValueChangedduring a drag — that is two interop reads per event when one suffices. The genericControlledPropEntrytrampoline 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.ChangeEchoSuppressor.ShouldSuppress(ReactorState)ChangeEchoSuppressor.ShouldSuppressEcho(ReactorState, object?)UIElementoverloads now read the DP once and delegate to these, so the external contract (and the publicWriteSuppressedprimitive /ChangeEchoSuppressor) is untouched.Reconciler.TryGetControlEventPayload<T>(ReactorState)— lets the generic descriptor trampoline reuse the already-read state (3 reads → 1).state→ dispatches viastate.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.csCore/V1Protocol/Descriptor/Descriptors/:GridViewDescriptor,ListViewDescriptor,TemplatedFlipViewDescriptorCore/V1Protocol/Handlers/:GridViewHandler,ListViewHandlerCore/V1Protocol/TemplatedListLifecycle.cs— ListView / GridView / FlipViewCore/Element.cs— TextBox.TextChanged, NumberBox.ValueChanged, AutoSuggestBox.TextChanged, RichEditBox.TextChanged, Expander.Expanding/Collapsed, ListBox.SelectionChanged, CalendarView.SelectedDatesChanged, ComboBox.SelectionChanged, Pivot.SelectionChanged, SelectorBar.SelectionChangedCore/Element.TabView.cs— TabView.SelectionChangedCore/Reconciler.csandCore/ChangeEchoSuppressor.csMeasurement
Added an A/B micro-benchmark C207 to the repo's existing
PerfBench.ControlModelharness. It holds one taggedTextBoxoff-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:DirectReactorTodayShouldSuppress(control)+GetElementTag(control)(2 DP reads)ReactorTryGetReactorState→ sharedstate(1 DP read)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 --headlesson 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.Testsfull suite: 9439 passed, 0 failed, 64 skipped.EchoSuppressselftests green, including a newTryGetReactorStateCoalescingfixture that asserts (a)TryGetReactorStateresolves the same element asGetElementTag, (b) a user edit still fires the callback, and (c) a programmatic echo is still swallowed exactly once.ChangeEchoSuppressorStateTests(headless xUnit) pin the exact semantics of the state-basedShouldSuppress/ShouldSuppressEchooverloads (scope is non-consuming, counter decrements once, pending-echo drain). The real-FrameworkElementpath can't be exercised headlessly, so it's covered by the selftest fixture instead.