Serve the app precompressed (br/gzip) with immutable caching; bigger list pages #422

Closed
opened 2026-10-15 07:54:52 +00:00 by kayg · 2 comments
Owner

Found by the #327 web-perf work: the built app is served uncompressed. It's 815 KB raw against 229 KB with brotli, so every cold start on a phone downloads about 3.5x more than it needs to.

Fix (server, static file serving):

  • At build time, precompress every web asset to .br (brotli q11) and .gz (gzip -9). This happens in the build (vite plugin or a post-build step) and in the Containerfile, never per request.
  • The server serves the precompressed file that matches Accept-Encoding, with Content-Encoding and Vary: Accept-Encoding. It falls back to the raw file.
  • Hashed assets get Cache-Control: public, max-age=31536000, immutable. index.html and the service worker get no-cache. Check what the PWA service worker caches, and that updates still arrive (#309, pwa-update.spec).
  • The JSON API answers big responses with on-the-fly compression (tower-http CompressionLayer, br/gzip), with a size threshold. Measure the CPU cost on the perf VM. Skip websocket frames for now, or use permessage-deflate if it's cheap; measure first.
  • Also from #327: the notes list's 500-per-page cap makes a 100k list take 200 round trips. Raise the cap for the background fill (e.g. 2000–5000 per page, streamed) while keeping the first page small. Check the #44 budgets (home.first_page) so they don't regress.

Gates: bytes transferred on a cold start (Chromium, 4x CPU and Fast 3G) before and after; CPU per request; pwa, pwa-update and served specs.

Found by the #327 web-perf work: **the built app is served uncompressed.** It's 815 KB raw against 229 KB with brotli, so every cold start on a phone downloads about 3.5x more than it needs to. Fix (server, static file serving): - At build time, precompress every web asset to `.br` (brotli q11) and `.gz` (gzip -9). This happens in the build (vite plugin or a post-build step) and in the Containerfile, never per request. - The server serves the precompressed file that matches `Accept-Encoding`, with `Content-Encoding` and `Vary: Accept-Encoding`. It falls back to the raw file. - Hashed assets get `Cache-Control: public, max-age=31536000, immutable`. `index.html` and the service worker get `no-cache`. Check what the PWA service worker caches, and that updates still arrive (#309, pwa-update.spec). - The JSON API answers big responses with on-the-fly compression (`tower-http` CompressionLayer, br/gzip), with a size threshold. Measure the CPU cost on the perf VM. Skip websocket frames for now, or use permessage-deflate if it's cheap; measure first. - Also from #327: the notes list's 500-per-page cap makes a 100k list take 200 round trips. Raise the cap for the background fill (e.g. 2000–5000 per page, streamed) while keeping the first page small. Check the #44 budgets (`home.first_page`) so they don't regress. Gates: bytes transferred on a cold start (Chromium, 4x CPU and Fast 3G) before and after; CPU per request; pwa, pwa-update and served specs.
Author
Owner

Numbers (PR for #422)

Cold start, bytes on the wire

Chromium, cache off, Fast 3G (1.6 Mbps down, 750 Kbps up, 562 ms RTT), CPU 4x, phone viewport, 10k-note Home served by the real release server (--web). 3 cold starts each, identical bytes. Host load was about 35, so the times are inflated; the bytes are exact.

before after
total transferred 5,237,980 B 2,190,447 B (-58%)
app files (page, JS, CSS, fonts, backdrop) 1,835,264 B 1,116,403 B
JSON API 3,402,716 B 1,074,044 B
requests 40 25
first note painted 41.0 s 18.2 s

"Before" = the plain files, Accept-Encoding: identity (no API compression), the note list in pages of 500. "After" = .br files, br API answers, a first page of 500 then pages of 2000. The main script goes 848,137 B -> 238,149 B (brotli), the CSS 130,080 -> 21,664 B. Fonts and the backdrop photo are already compressed (451 KB mountains.jpg is now the biggest single download).

CPU per API response (perf VM, 100k Home, notes.list, 40 requests per cell)

Server CPU per response (utime+stime of the server process), and bytes. Chosen level: brotli/gzip 4.

page plain gzip br
50 notes 0.75 ms, 18.9 KB 1.25 ms, 5.8 KB 2.0 ms, 5.8 KB
500 notes 3.75 ms, 183 KB 8.75 ms, 48.6 KB 10.5 ms, 47.9 KB
2000 notes 19.0 ms, 729 KB 33.0 ms, 193 KB 36.5 ms, 188 KB

Other levels (2000 notes, br): fastest 28.0 ms / 225 KB; level 6 89 ms / 174 KB. Level 4 is the knee: 16% smaller than the fastest for about 8 ms more, and level 6 costs 2.4x for another 7%. Answers under 1 KB are never compressed. The live socket is untouched (no permessage-deflate: the frames are small and mostly Yjs binary; not worth the CPU).

#44 budgets (perf VM, 100k, calternal-perf, dev vs this branch)

home.first_page is a page of 50 and is not changed by this PR: it does not compress (the perf client sends no Accept-Encoding) and the cap only matters for bigger asks.

  • Full harness, two rounds each (the VM was shared: the second mine round was 30-60% slower on every metric, including cold.index 19 s -> 32 s): dev 13.7 / 15.0 ms p50, mine 34.6 / 33.2 ms (p95 240 / 64.8 ms, one over in the first round).
  • So I re-ran it interleaved, same warm Home, 6 server starts per binary, first notes.list{limit:50} after start: dev 17, 17, 20, 27, 28, 226 ms; this branch 17, 20, 21, 21, 30, 124 ms (and 17, 18, 21, 26, 43, 68 ms for a build of only the calternal package). Same distribution. Second request: 2.2-5.1 ms vs 1.7-7.3 ms.
  • New metric home.page_big (five pages of 2000, the app's background fill): 33-43 ms p50 per page at 100k (dev can only do 500 per page: 9-10 ms). 200 round trips of 500 become 50 of 2000.

Tests

  • cargo fmt --check, cargo clippy --workspace --all-targets -- -D warnings: clean.
  • cargo test --workspace --no-fail-fast: 407 passed, 0 failed. (engine::a_huge_job_never_delays_anyone failed once in the first run at host load 40 on a 880 ms timing assert, and passed on the re-run; unrelated.)
  • Playwright against the release binary: pwa, pwa-update, served in chromium-1440 and webkit-1440: 10 passed, 10 skipped by design.

PWA updates

The service worker lists the plain files and fetches them with the browser's own Accept-Encoding, so it gets the .br and decodes it. index.html and sw.js stay no-cache. One trap: the server sends sw.js.br if present, so a hand-edited sw.js next to a stale .br hides a new version. Builds write all three at once; the faked deploy in pwa-update.spec now does too.

PR: #439

## Numbers (PR for #422) ### Cold start, bytes on the wire Chromium, cache off, Fast 3G (1.6 Mbps down, 750 Kbps up, 562 ms RTT), CPU 4x, phone viewport, 10k-note Home served by the real release server (`--web`). 3 cold starts each, identical bytes. Host load was about 35, so the times are inflated; the bytes are exact. | | before | after | |---|---:|---:| | total transferred | **5,237,980 B** | **2,190,447 B (-58%)** | | app files (page, JS, CSS, fonts, backdrop) | 1,835,264 B | 1,116,403 B | | JSON API | 3,402,716 B | 1,074,044 B | | requests | 40 | 25 | | first note painted | 41.0 s | 18.2 s | "Before" = the plain files, `Accept-Encoding: identity` (no API compression), the note list in pages of 500. "After" = `.br` files, br API answers, a first page of 500 then pages of 2000. The main script goes 848,137 B -> 238,149 B (brotli), the CSS 130,080 -> 21,664 B. Fonts and the backdrop photo are already compressed (451 KB `mountains.jpg` is now the biggest single download). ### CPU per API response (perf VM, 100k Home, `notes.list`, 40 requests per cell) Server CPU per response (utime+stime of the server process), and bytes. Chosen level: brotli/gzip 4. | page | plain | gzip | br | |---|---:|---:|---:| | 50 notes | 0.75 ms, 18.9 KB | 1.25 ms, 5.8 KB | 2.0 ms, 5.8 KB | | 500 notes | 3.75 ms, 183 KB | 8.75 ms, 48.6 KB | 10.5 ms, 47.9 KB | | 2000 notes | 19.0 ms, 729 KB | 33.0 ms, 193 KB | 36.5 ms, 188 KB | Other levels (2000 notes, br): fastest 28.0 ms / 225 KB; level 6 89 ms / 174 KB. Level 4 is the knee: 16% smaller than the fastest for about 8 ms more, and level 6 costs 2.4x for another 7%. Answers under 1 KB are never compressed. The live socket is untouched (no permessage-deflate: the frames are small and mostly Yjs binary; not worth the CPU). ### #44 budgets (perf VM, 100k, `calternal-perf`, dev vs this branch) `home.first_page` is a page of 50 and is not changed by this PR: it does not compress (the perf client sends no Accept-Encoding) and the cap only matters for bigger asks. - Full harness, two rounds each (the VM was shared: the second `mine` round was 30-60% slower on every metric, including `cold.index` 19 s -> 32 s): dev 13.7 / 15.0 ms p50, mine 34.6 / 33.2 ms (p95 240 / 64.8 ms, one `over` in the first round). - So I re-ran it interleaved, same warm Home, 6 server starts per binary, first `notes.list{limit:50}` after start: dev 17, 17, 20, 27, 28, 226 ms; this branch 17, 20, 21, 21, 30, 124 ms (and 17, 18, 21, 26, 43, 68 ms for a build of only the `calternal` package). Same distribution. Second request: 2.2-5.1 ms vs 1.7-7.3 ms. - New metric `home.page_big` (five pages of 2000, the app's background fill): 33-43 ms p50 per page at 100k (dev can only do 500 per page: 9-10 ms). 200 round trips of 500 become 50 of 2000. ### Tests - `cargo fmt --check`, `cargo clippy --workspace --all-targets -- -D warnings`: clean. - `cargo test --workspace --no-fail-fast`: 407 passed, 0 failed. (`engine::a_huge_job_never_delays_anyone` failed once in the first run at host load 40 on a 880 ms timing assert, and passed on the re-run; unrelated.) - Playwright against the release binary: `pwa`, `pwa-update`, `served` in chromium-1440 and webkit-1440: 10 passed, 10 skipped by design. ### PWA updates The service worker lists the plain files and fetches them with the browser's own Accept-Encoding, so it gets the `.br` and decodes it. `index.html` and `sw.js` stay `no-cache`. One trap: the server sends `sw.js.br` if present, so a hand-edited `sw.js` next to a stale `.br` hides a new version. Builds write all three at once; the faked deploy in `pwa-update.spec` now does too. PR: https://git.kayg.org/kayg/calternal/pulls/439
Author
Owner

Closing during the 26 Oct handover cleanup: The precompressed, immutable serving landed in dev (PR #439).

Closing during the 26 Oct handover cleanup: The precompressed, immutable serving landed in dev (PR #439).
kayg closed this issue 2026-10-26 08:46:24 +00:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
kayg/calternal#422
No description provided.