Skip to content

Screen performance: eight changes from the daedalus benchmark, alongside #242 - #243

Open
coretl wants to merge 8 commits into
DiamondLightSource:masterfrom
coretl:perf-eight-changes
Open

coretl wants to merge 8 commits into
DiamondLightSource:masterfrom
coretl:perf-eight-changes

Conversation

@coretl

@coretl coretl commented Nov 9, 2026 •

Copy link
Copy Markdown

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

  • The real daedalus display (cs-web-lib 0.11.2) ran against a fake PVWS server, with a synthetic screen of N = 100 / 1000 / 5000 widgets and 10 to 3000 PV messages/s.
  • Headless Chromium without a GPU (software rendering), so I only trust main-thread busy %, frame rate and latency from those runs. Only "as shipped" against "all eight changes" was run in the browser.
  • The per-change figures below come from a jsdom harness running the same display stack, one change added at a time, cumulative. jsdom overstates DOM costs (its style and layout are JavaScript, and it has no paint), so read those as relative, not absolute.
  • Figures are CPU as a share of one core and probe latency (update to DOM commit) unless stated. Each is the median of 3 runs, on one machine.
  • I have not run Improve performance #242 through the benchmark. The Improve performance #242 column is from reading its diff (head 5d39d84, unchanged since 2026-11-04), so every statement about its effect is from reading the code only.

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

Change #242 status Suggested action
1. Dispatch connectionChanged only when a PV's connection state changes Implemented Drop if #242 merges
2. Short-circuit fileComparator when the screen is unchanged Implemented Drop if #242 merges
3. Subscribe PV-reading hooks to their own PVs only Partial Keep the remainder, after measuring #242's selector factory
4. Keep one merged update per PV in the throttle queue Absent Drop, it measured no effect
5. Use a plain textarea in the readback widget Absent Keep
6. Render the readback widget as plain divs Absent Keep
7. Batch the subscribe actions of one React commit Partial Keep the remainder, startup only
8. Send connectionChanged through the throttle as one batch Partial, and conflicts with a #242 test Keep the remainder, startup only

The changes, in commit order

1. Dispatch connectionChanged only when a PV's connection state changes

Every PVWS message dispatched connectionChanged (in serviceConnection.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 a pvConnState map and only calls onConnectionChangedCallback when the state changed, and the connectionChanged reducer in src/redux/csState.ts returns 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

fileComparator compared two JSON.stringify results on every dispatch. This returns early when before === 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 same if (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, and useConnection, useRules and useScripts read through useSyncExternalStore instead of useSelector(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() in src/redux/csState.ts (hunk @@ -238,23 +266,43 @@) gives each hook its own memoised selector with a no-allocation bail-out, used through useMemo in useConnection.ts, useRules.ts and useScripts.ts. It still uses useSelector on 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 throttleMiddleware there 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 valuesChanged per 100 ms.

5. Use a plain textarea in the readback widget

A multiline MUI TextField always renders TextareaAutosize, which calls getComputedStyle and reads scrollHeight after every render. This passes inputComponent: "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 TextField in readback.tsx with 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 and aria-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

useSubscription dispatched one subscribe action per widget. This queues them and flushes one subscribeBatch action 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. prepareAutoBatched on subscribe, unsubscribe and connectionChanged in src/redux/csState.ts (hunk @@ -108,77 +113,100 @@) batches the subscriber notifications, but the reducers still run once per action. pvwsClient.ts subscribe() (hunk @@ -152,22 +180,53 @@) also sends one websocket subscribe message 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 connectionChanged was its own dispatch. This routes them through the 100 ms throttle and dispatches one connectionsChanged action 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 connectionChanged and auto-batches it but does not route it through the throttle, and its throttleMiddleware.test.ts (hunk @@ -78,10 +78,10 @@) still asserts that connectionChanged passes 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, the useScripts tests (they mock useSelector, so they now mock the new usePvStates hook 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.

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

No deployments
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.

1 participant