Repository navigation
Conversation
serviceConnection dispatched connectionChanged for every PVWS message, so each value update also ran every mounted selector. It now remembers the last connected/readonly pair per PV and dispatches only when it differs. Measured (jsdom, real display stack, 1000 widgets at 100 msg/s): from saturated and falling behind (probe latency 10.7 s) to 57% of a core with a 92 ms probe. Assisted-by: Claude Opus 5
fileComparator compared two JSON.stringify results on every dispatch. Return early when before and after are the same reference. Measured (jsdom, 1000 widgets): little change in steady state once connectionChanged is deduplicated, but startup falls from 22-27 s to 15-16 s. Assisted-by: Claude Opus 5
Add a small keyed notifier (pvSubscriptions) that calls only the listeners of the PVs in each PV action, and use it through useSyncExternalStore in useConnection, useRules and useScripts instead of useSelector(selectPvStates), which ran for every mounted widget on every dispatch. Measured (jsdom, 1000 widgets at 100 msg/s): 51% to 37% of a core, and startup 15 s to 10 s. When the page is saturated (100 widgets at 1000 msg/s) CPU rises from 82% to 109% but probe latency falls from 420 ms to 274 ms, because updates that used to be dropped are now rendered. Assisted-by: Claude Opus 5
The throttle queued every update between flushes. Keep the latest value per PV, merged with the earlier metadata, so a PV updated many times in 100 ms is reduced once. Measured (jsdom, 1000 widgets, all the rate on 10 PVs): no measurable effect (52-54% of a core without, 52% with). Candidate for dropping. Assisted-by: Claude Opus 5
A multiline MUI TextField always renders TextareaAutosize, which calls getComputedStyle and reads scrollHeight after every render. Pass inputComponent: "textarea" so a text update no longer measures itself. Measured (jsdom, which exaggerates this cost): 1000 widgets at 1000 msg/s from saturated (108%, probe 4.2 s) to 41% with a 100 ms probe; 100 widgets at 100 msg/s from 32% to 6%; startup at 1000 widgets from 10 s to 3 s. The cost in Chromium was not measured separately. Assisted-by: Claude Opus 5
Replace the MUI TextField in readback with two divs using inline styles, so a text update no longer re-serialises about six emotion styled components. Measured (jsdom): 1000 widgets at 1000 msg/s from 41% to 25% of a core, and at 3000 msg/s from saturated to 59%. Assisted-by: Claude Opus 5
useSubscription queued each widget's subscribe as its own action. Queue them and flush as one subscribeBatch action on a microtask, so the reducers and subscribers run once per commit. Measured (jsdom, startup only): 5000 widgets from 72 s to 38 s, steady state unchanged. Assisted-by: Claude Opus 5
At startup every PV's first connectionChanged was its own dispatch. Route them through the 100 ms throttle and dispatch one connectionsChanged action per flush. Measured (jsdom, startup only): 5000 widgets from 38 s to 7 s, 1000 widgets from 2.5 s to 0.9 s with the previous commit, steady state unchanged. Assisted-by: Claude Opus 5
This branch has not been deployed
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.
This is a draft PR with eight small changes to cs-web-lib that came out of claude benchmarking daedalus. It is shared so you can extract what you'd like from this into #242.
How it was measured
Headline from the browser runs: as shipped, daedalus saturates the main thread at 1000 widgets and 100 msg/s (99.8% busy, 20 fps). With all eight changes it is at 13% busy there, and at 1000 widgets and 1000 msg/s it is at 36% busy and 60 fps. At 5000 widgets it still saturates at 1000 msg/s, and a trace puts that mostly in Chromium painting a large DOM, which none of these changes touch.
Summary
The changes, in commit order
1. Dispatch connectionChanged only when a PV's connection state changes
Every PVWS message dispatched
connectionChanged(inserviceConnection.ts), so every value update also ran every mounted selector. This remembers the last connected/readonly pair per PV and dispatches only when it differs.Measured: 1000 widgets at 100 msg/s goes from saturated and falling behind (probe 10.7 s) to 57% of a core with a 92 ms probe. This is the change that removes the overload.
#242 status: implemented.
src/connection/pvws/pvwsClient.ts,handleMessage(hunk@@ -136,8 +153,19 @@) keeps apvConnStatemap and only callsonConnectionChangedCallbackwhen the state changed, and theconnectionChangedreducer insrc/redux/csState.tsreturns early when nothing changed. Same effect at a different layer.Suggested action: drop if #242 merges.
2. Short-circuit fileComparator when the screen is unchanged
fileComparatorcompared twoJSON.stringifyresults on every dispatch. This returns early whenbefore === after.Measured: little in steady state once change 1 is in, but 1000-widget startup goes from 22-27 s to 15-16 s.
#242 status: implemented.
src/redux/slices/fileCacheSlice.ts,fileComparator(hunk@@ -565,21 +554,11 @@) has the sameif (before === after) return true;.Suggested action: drop if #242 merges.
3. Subscribe PV-reading hooks to their own PVs only
A small keyed notifier (
src/redux/pvSubscriptions.ts) calls only the listeners of the PVs in each PV action, anduseConnection,useRulesanduseScriptsread throughuseSyncExternalStoreinstead ofuseSelector(selectPvStates), which runs for every mounted widget on every dispatch.Measured: 1000 widgets at 100 msg/s goes from 51% to 37% of a core, and 1000-widget startup from 15 s to 10 s. On a saturated page (100 widgets at 1000 msg/s) CPU rises from 82% to 109% while probe latency falls from 420 ms to 274 ms, because updates that used to be dropped are now rendered.
#242 status: partial, by a different route.
makeSelectPvStates()insrc/redux/csState.ts(hunk@@ -238,23 +266,43 @@) gives each hook its own memoised selector with a no-allocation bail-out, used throughuseMemoinuseConnection.ts,useRules.tsanduseScripts.ts. It still usesuseSelectoron react-redux 7, so every mounted selector still runs on every dispatch, but each is now cheap. I have not measured that. What this change adds is that a dispatch only wakes the listeners of the changed PVs.Suggested action: benchmark #242's version first. If the remaining per-dispatch cost matters at large N, keep this on top; otherwise drop it. It is also the largest of the eight (115 added lines), so the cheaper option is worth testing before taking it.
4. Keep one merged update per PV in the throttle queue
The throttle queued every update between flushes. This keeps the latest value per PV, merged with earlier metadata.
Measured: no effect. With the whole rate on 10 PVs (the case it was meant for), 1000 widgets used 52-54% of a core without it and 52% with it, inside the spread.
#242 status: absent (the only change to
throttleMiddlewarethere is a test edit).Suggested action: drop. It costs code and buys nothing I could measure, and I would not prioritise further batching of value updates, since the throttle already sends one
valuesChangedper 100 ms.5. Use a plain textarea in the readback widget
A multiline MUI
TextFieldalways rendersTextareaAutosize, which callsgetComputedStyleand readsscrollHeightafter every render. This passesinputComponent: "textarea"so a text update no longer measures itself.Measured (jsdom, which exaggerates this one most): 1000 widgets at 1000 msg/s from saturated (108%, probe 4.2 s) to 41% with a 100 ms probe; 100 widgets at 100 msg/s from 32% to 6%; 1000-widget startup from 10 s to 3 s. The cost in Chromium was not measured separately. The figures are cumulative with changes 1 to 4.
#242 status: absent. Nothing under the readback widget changes.
Suggested action: keep. This was the largest single win in the jsdom runs. One thing to check: the textarea is sized by CSS, so text with a vertical alignment other than top may sit at the top (from reading the code, not tried visually).
6. Render the readback widget as plain divs
Replaces the MUI
TextFieldinreadback.tsxwith two divs using inline styles, so a text update no longer re-serialises about six emotion styled components. Existing tests that looked for a textbox or a disabled textarea now check the text andaria-disabled.Measured (jsdom): 1000 widgets at 1000 msg/s from 41% to 25% of a core, and at 3000 msg/s from saturated to 59%.
#242 status: absent.
Suggested action: keep. It drops MUI's outline and focus styling, which I have only checked by reading the code. If you want those back it would be a CSS addition rather than a revert.
7. Batch the subscribe actions of one React commit
useSubscriptiondispatched one subscribe action per widget. This queues them and flushes onesubscribeBatchaction on a microtask, so the reducers and subscribers run once per commit.Measured (jsdom, startup only): 5000-widget startup from 72 s to 38 s. Steady state is unchanged.
#242 status: partial, same idea in a different form.
prepareAutoBatchedonsubscribe,unsubscribeandconnectionChangedinsrc/redux/csState.ts(hunk@@ -108,77 +113,100 @@) batches the subscriber notifications, but the reducers still run once per action.pvwsClient.tssubscribe()(hunk@@ -152,22 +180,53 @@) also sends one websocketsubscribemessage per microtask, which this change does not. I did not measure the startup effect of #242's form.Suggested action: keep the remainder (one reducer pass per commit) only if startup at thousands of widgets matters. Measure #242 first.
8. Send connectionChanged through the throttle as one batch
At startup every PV's first
connectionChangedwas its own dispatch. This routes them through the 100 ms throttle and dispatches oneconnectionsChangedaction per flush.Measured (jsdom, startup only): 5000-widget startup from 38 s to 7 s, and 1000 widgets from 2.5 s to 0.9 s with change 7. Steady state is unchanged.
#242 status: partial, with a conflict. #242 dedupes
connectionChangedand auto-batches it but does not route it through the throttle, and itsthrottleMiddleware.test.ts(hunk@@ -78,10 +78,10 @@) still asserts thatconnectionChangedpasses straight through, which this change reverses. The two cannot both land without reconciling that test.Suggested action: keep the remainder, but decide it together with change 7 after measuring #242's startup.
Things I changed to get lint and tests green
The eight benchmark patches were not lint-clean and broke a few tests, so each commit carries the small fix it needed: prettier formatting, unused imports in the three hooks, a non-null assertion in
useSubscription, theuseScriptstests (they mockuseSelector, so they now mock the newusePvStateshook too), the readback tests and snapshot, and the throttle test for change 8. Nothing else was changed from the benchmarked patches. The browser figures were measured on the patches before those fixes. Lint, type check and unit tests pass at every commit.AI Disclaimer
The code and this description were generated with AI and reviewed/tweaked by the author.