Skip to content

perf: compress static assets and stop idle redraws - #36

Merged
Alorse merged 9 commits into
mainfrom
chore/perf-idle-and-compression
Sep 29, 2026
Merged

Alorse merged 9 commits into
mainfrom
chore/perf-idle-and-compression

Conversation

@Alorse

@Alorse Alorse commented Sep 29, 2026

Copy link
Copy Markdown
Owner

What changed

Faster first load (#25). The server now sends text files (the page, styles and code, 1 KB or larger) compressed with brotli, or gzip for browsers without brotli. Each file is compressed once per version and kept in memory. Responses carry Vary: Accept-Encoding, and each encoding gets its own ETag ("<hash>-br", "<hash>-gzip", "<hash>"), so a cache never gives a browser the wrong encoding. A 304 revalidation still works, including when a CDN has weakened the ETag. Fonts and PNGs are sent as they are. Cache-Control, ?v=N and CACHE_NAME work as before.

Less battery drain while idle (#22). Three animation loops ran all the time, about 180 redraws a second with nothing happening:

  • the orb,
  • the hidden mic line,
  • a third loop that only fed the orb its sound level.

Now:

  • The orb reads the sound level itself, so the third loop is gone.
  • When the orb is only breathing, it draws 20 frames a second. Movement per frame is well under a pixel, so it looks the same.
  • With reduced motion it draws 10 frames a second.
  • It stops completely while another screen is shown.
  • The mic line is drawn only while it is on screen.

Frames drawn at the slower pace still follow real time, so the animation runs at the same speed. Cache version bumped v11 → v12.

Flaky test. '#13 an upstream that never completes setup…' checked that the upstream was closed at the exact moment the client saw the error. The server's close reaches the fake upstream a moment later, so the test sometimes failed. It now waits for the close with the shared waitFor.

Also:

  • perf-api measured the shell with fetch, which decodes compressed bodies, so it could never have shown compression. The measurement now counts raw bytes; node scripts/perf-server.mjs --shell runs it offline.
  • New repro-browser scenario sw-offline.
  • idle-cpu now also measures reduced motion and another screen shown.
  • echo-window now records whether the mic line is drawn each second, and whether anything draws when you switch screens mid-conversation.

Closes #22
Closes #25

Evidence

Flaky test, run 30 times alone on /usr/bin/node 20:

failures
before 3/30
after 0/30

Bytes on the wire for the app shell (perf-server.mjs --shell, 18 assets):

total compressed responses
before 237.9 KB 0
after 158.2 KB 12

The 12 text files go from about 112 KB to about 30 KB, e.g. app.js 31,114 → 8,228 bytes and styles.css 25,570 → 5,122 bytes. Fonts and icons are unchanged.

Service worker (repro-browser sw-offline, headless Chrome):

  • the files arrive as br;
  • the parley-v12 cache holds 22 entries, and the cached app.js is identical to the file;
  • after an offline reload the app still opens under the worker, with no errors.

Idle cost (repro-browser idle-cpu, two runs each; these numbers are noisy):

rAF callbacks/s main-thread task ms/s
Talk screen, before 166–181 169–285
Talk screen, after 19 60–67
another screen, before 178–181 66–70
another screen, after 0 1–3
reduced motion, before 181 203–217
reduced motion, after 9–10 28–38

During a conversation (echo-window):

  • The mic line is drawn only while listening (440–558 painted pixels) and never while hidden.
  • With another screen shown mid-conversation, nothing draws. Before the last fix the mic line still drew 60.5 times a second there; now 0.
  • The half-duplex timing is unchanged: 0 mic frames were sent while the tutor was playing.
  • double-tap still sees one mic pipeline at 30–32 frames/s.

Test suites: npm run typecheck, npm test and /usr/bin/node --import tsx --test test/*.test.js all pass. Test count went from 177 to 192: 8 compression tests (unit tests plus the real server) and 7 orb pacing tests. The only todo left is the deliberate ACCESS_TOKENS one.

Not done

  • The server still reads and hashes each static file on every request, as it did before. With about 20 files and a few users it isn't worth a cache yet.
  • Two other harness tests use a fixed delay before checking that the upstream closed (the timing assumption behind the flaky test). Neither has been seen to flake, so I left them alone.

🤖 Generated with Claude Code

Alorse and others added 9 commits September 29, 2026 11:44
The test sampled the fake upstream's closedAt the instant the client saw
the error, but the server's close reaches the upstream a moment later, so
it failed about 1 run in 10 on Node 20 (3/30 before, 0/30 after).

Co-authored-by: Claude Code <noreply@anthropic.com>
fetch() decodes compressed bodies, so perf-api reported the decoded size
even for a compressing server. Move the measurement to a shared module
that reads raw bytes over node:http, and add an offline
'perf-server.mjs --shell' that runs it against the harness server.

Co-authored-by: Claude Code <noreply@anthropic.com>
The page, styles and code (1 KB or more) are compressed once per content
hash and served with Vary: Accept-Encoding. Each encoding gets its own
ETag so a cache never answers one encoding with another, and
If-None-Match also accepts a CDN-weakened tag. Fonts and PNGs are sent
as they are. Cache-Control and the ?v=/CACHE_NAME scheme are unchanged.

App shell on the wire (perf-server.mjs --shell): 237.9 KB -> 157.6 KB;
the 12 text files go from 112 KB to 30 KB.

Closes #25

Co-authored-by: Claude Code <noreply@anthropic.com>
Checks in headless Chrome that the shell arrives compressed, that the
service worker's cached copies decode to the real files, and that the
app still opens offline.

Co-authored-by: Claude Code <noreply@anthropic.com>
idle-cpu now also measures the app with reduced motion and with another
screen shown, and echo-window samples how much of the mic line is drawn
each second (null while it is hidden).

Co-authored-by: Claude Code <noreply@anthropic.com>
Three animation loops ran all the time, about 180 frames a second while
idle: the orb, the hidden mic line, and a loop that only fed the orb its
level. Now the orb reads its level itself, draws about 20 frames a second
when it only breathes (well under a pixel of movement per frame) and 10
with reduced motion, and stops while another screen is shown. The mic
line is drawn only while it is visible. Paced frames keep real time, so
the animation looks the same.

Idle Talk screen (repro-browser idle-cpu, 2 runs each):
  rAF callbacks/s 166-181 -> 19; task ms/s 169-285 -> 60-67
  other screen 178-181 -> 0 rAF/s; reduced motion 181 -> 10 rAF/s

Cache version bumped to v12.

Closes #22

Co-authored-by: Claude Code <noreply@anthropic.com>
Switching tabs mid-conversation left the mic line drawing at 60 frames a
second off screen (repro-browser echo-window: 60.5 -> 0 rAF/s). The
screen now counts toward whether it is shown. Also: both canvases read
the live reduced-motion query instead of caching or re-querying it, the
waveform gets its data source once, the orb starts with the Talk screen,
and the frame-time cap is derived from the slowest pace.

Co-authored-by: Claude Code <noreply@anthropic.com>
…m sw.js

getRaw moves to test/harness/http.mjs (with a method, so the HEAD test
uses it too), and the shell-weight probe takes the code modules from the
service worker's APP_SHELL instead of a hand-kept copy.

Co-authored-by: Claude Code <noreply@anthropic.com>
Co-authored-by: Claude Code <noreply@anthropic.com>
@Alorse
Alorse merged commit 9c2248e into main Sep 29, 2026
2 checks passed
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.

Compress the app's files for a faster first load Save battery: stop animating when nothing is happening

1 participant