Skip to content

Use partysocket in favour of react-use-websocket, remove batching - #402

Merged
Tom-Willemsen merged 6 commits into
mainfrom
ws
Dec 2, 2026
Merged

Tom-Willemsen merged 6 commits into
mainfrom
ws

Conversation

@rerpha

@rerpha rerpha commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

This PR removes the batching behaviour we added to solve performance issues a while ago, particularly with instruments with large numbers of blocks. I got claude to benchmark this with a softioc with 200 PVs+pvws as well as the real instance. Also smoke tested with chrome's throttler to simulate a low-end phone with a slow 4g connection and it kept up with updates.

react-use-websocket uses flushSync() (see issue here) which causes a re-render of the entire component when a message is received - this is a big performance issue as we only really want to update whichever block has updated. The batching helped because it was rendering unchanged components for the time the batch was "filling up" but I don't think it's needed if we're being more cleverer about what we're re-rendering. Batching is obviously still better for raw performance but gives the trade-off of slower updates.

I also looked at using jotai atoms but this only really helped marginally - i don't think it's worth the extra dependency. We could use an empty filter as a workaround (filter: () => false) and without lastMessage which we don't use now as we set everything up in onOpen(). partysocket seems to be more popular according to npm stats regardless.

benchmarks

Setup:

  • Real PVWS on Tomcat 9, with default settings (each PV limited to 1 update/s).
  • Python soft IOC serving N blocks, each updating at 1 Hz, plus run info and dashboard PVs.
  • Headless Chromium at 412×915 with touch, DPR 2.625 and CPU slowed 6×.
  • 5 s warm-up, then a 15 s measurement window. Each figure is the mean of 2 runs.

Variants:

  • Original: the code before batching was added.
  • main: the current code, with a 1 s batching queue.
  • react-use-websocket, per-message updates: the same render code as this PR, but on react-use-websocket instead of partysocket.
  • partysocket + jotai atoms: one atom per PV, evaluated and rejected.
  • partysocket + React state: this PR.
Blocks Variant Median latency p95 latency p95 lag FPS Script time
200 Original (react-use-websocket, no batching) 12.33 s 15.46 s 10.74 s 5.8 75%
200 main (react-use-websocket + 1 s batching) 867 ms 1.64 s 95 ms 49.9 9%
200 react-use-websocket, per-message updates 181 ms 912 ms 55 ms 31.7 18%
200 partysocket + jotai atoms 210 ms 944 ms 44 ms 35.3 14%
200 partysocket + React state (this PR) 224 ms 946 ms 46 ms 35.8 14%
500 Original (react-use-websocket, no batching) 71.67 s 87.64 s 24.78 s 4.6 78%
500 main (react-use-websocket + 1 s batching) 1.01 s 1.86 s 324 ms 27.4 18%
500 react-use-websocket, per-message updates 1.87 s 3.14 s 2.44 s 4.2 36%
500 partysocket + jotai atoms 372 ms 1.10 s 190 ms 8.7 17%
500 partysocket + React state (this PR) 368 ms 1.10 s 190 ms 8.8 17%
1000 Original (react-use-websocket, no batching) 62.85 s 90.38 s 49.94 s 4.4 85%
1000 main (react-use-websocket + 1 s batching) 4.25 s 6.24 s 1.90 s 4.9 34%
1000 react-use-websocket, per-message updates 18.01 s 22.14 s 3.51 s 3.7 34%
1000 partysocket + jotai atoms 1.14 s 1.88 s 974 ms 3.4 24%
1000 partysocket + React state (this PR) 1.01 s 1.73 s 903 ms 3.4 22%

Metrics:

  • Latency: time from the IOC setting a value to the new value appearing on screen.
  • Lag: how late a 50 ms timer fires, a measure of how responsive the page is.
  • Script time: share of the window spent running JavaScript.

Findings:

  • partysocket vs react-use-websocket, with identical render code: about the same at 200 blocks, but with 500+ blocks latency is 5× lower at 500 and 18× lower at 1000, with 10× / 4× less lag.
    react-use-websocket wraps every message in flushSync, forcing a synchronous React render per message.
  • The original problem was that flushSync combined with re-rendering the whole tree per message. It's unusable even at 200 blocks: 10 s+ lag and about 5 fps.
  • Compared with main, this PR has 3–4× lower latency and lower lag at every size. Batching still gets more fps at 200–500 blocks, because the phone is now limited by layout and paint rather than
    script.
  • jotai made no measurable difference compared with plain React state and a memoised Block, so we dropped the extra dependency.

@rerpha
rerpha requested a review from Tom-Willemsen November 23, 2026 12:30
@Tom-Willemsen

Copy link
Copy Markdown
Member

Did you try a benchmark with both batching and partysocket?

On my machine, firefox profiler is saying this branch is using about 2-3x more CPU when looking at HIFI with hidden blocks expanded.

I think it might still be worth using partysocket even if just for the reconnect behaviour though.

@rerpha

rerpha commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor Author

I didn't. batching is always going to be better because it's skipping any rendering of any compoents for however many messages come through in 1 second. I was hoping that this change would be "good enough" to avoid the extra complexity of batching. I tested this on a simulated low end phone and it keeps up with updates on HIFI with hidden blocks, UI isn't laggy or anything, so deemed that to be OK without it. also, this gets rid of the initial page load delay where it says everything's unreachable while the batching is filling up the 0th batch.

Partysocket does look like it's a bit more widely used too!

@Tom-Willemsen
Tom-Willemsen merged commit a98b515 into main Dec 2, 2026
5 checks passed
@Tom-Willemsen
Tom-Willemsen deleted the ws branch December 2, 2026 15:39
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.

2 participants