Repository navigation
Long sessions open almost instantly #784
JoaoLuz-pixel
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Here's a follow-up to the suite of features I've been adding to my fork
Opening an existing session from the sidebar took 2–3 seconds once the session got long. This change makes it open in about 0.3 seconds on a native build of the app (one built for the computer's own processor), with no change to what the learner ends up seeing, and keeps Find (Ctrl+F) covering the whole conversation.
Branch
inprogress/JoaoLuz-pixel(on my fork), on top ofc8caabc. 5 files, about 220 lines including tests.Results
Measured on the packaged Windows build (Electron, production server), native to the test machine's processor, clicking a session in the sidebar of headless Chrome, using copies of real sessions:
"Before/After" is the time from the click until the transcript is on screen.
Native vs emulated
On an ARM64 Windows PC, the official Windows build is x64, so Windows runs it through its x64 emulator. The emulator translates the app's code while it runs, including the code that the JavaScript engine generates on the fly, which is most of the work of rendering a session. To measure the cost, I ran the same test on the same PC with the same data. One app was the installed x64 build (emulated). The other was a native ARM64 build of the same code. Both used the app's own window (not headless Chrome), with copies of real sessions; the long session had grown to 303 messages by then.
Under emulation everything took about twice as long, and the first session after launch took nearly four times as long. The recent-first rendering still works the same way in both: the long session opened with about 8% of its text and filled in the rest afterwards. If you're on an ARM64 PC, a native ARM64 build gives you the advertised times.
Why it was slow
A CPU profile of the 2.0 s case showed the server was not the problem: once warm, it returns even the largest session in about 36 ms. The time was all in the renderer, which rendered the entire history on every open:
getBoundingClientRectin a RadixCollapsiblelayout effect and in the chat's growthResizeObserver)The cost grows with the size of the history, and the learner only sees the last screenful when a session opens.
Separately, the first session opened after launching the app paid a one-time ~1.3 s on the server to load the session page's code.
What changed
1. Recent-first rendering with a background fill
components/progressive-history.ts(new) providesuseProgressiveHistory(messages), used bycomponents/chat.tsx.requestIdleCallback(500 ms timeout), insidestartTransition, so typing, clicking and answering always take priority. A batch takes about 40–50 ms.renderFromcounts from the start of the history, so messages that arrive during the session are always rendered. In the map,mistays the index in the whole history, sophases[mi]and the other index-based logic are unchanged.Why a background fill, and not load-on-scroll or virtualisation. Find (Ctrl+F) walks the DOM, so it only searches what's rendered. Filling ahead of time means the work is already done by the time the learner opens Find, instead of a full render at that moment. Every message ends up in the page; nothing is ever unloaded.
2. Find and message links complete the history at once
components/session-find.tsx:SessionFindtakes an optionalonOpen, called as Find opens. The chat passes the hook'scompleteNow, so Find always covers the whole conversation. Find's existingMutationObserverrefreshes its matches as messages are added.components/chat.tsx: if the URL carries a message hash (#message-…, e.g. from a knowledge-graph link), the whole history is rendered at mount. That waySessionMessageNavigationfinds the anchor even when it is an old message.3. Launch warm-up
electron/main.cjs: once the server is up, the shell sends one fire-and-forget request,GET /chat/lh-warmupwithRSC: 1. No session has that id, so it is a plain 404: no record is read or written. The request loads the session page's server code before the learner's first click. Pages are not behindproxy.ts(only/apiis), so no capability header is needed.Considered and dropped:
content-visibility: autoon transcript rowsIt was in the original plan, to skip layout for off-screen rows. In testing it left the teacher's last message blank above an open question dock, and it made the scrollbar size depend on estimates. Once recent-first rendering was in, it only shortened the background fill by ~0.2 s and did not change opening time. Dropped.
What it affects
Known limits, only during the 2–3 s fill after opening a long session:
Files:
components/progressive-history.ts(new),components/chat.tsx,components/session-find.tsx,electron/main.cjs,tests/progressive-history.test.tsx(new).How it was tested
Automated (
tests/progressive-history.test.tsx, 6 tests)completeNowrenders the whole history at once. A shorter list clamps correctly.mikeeps whole-history indexes; Find and message links call the completion; the shell sends the warm-up request.Full suite: 5,421 passed, 0 failed. Typecheck and lint are clean; the only lint warning is pre-existing, in an untouched file.
On the packaged Windows build (headless Chrome, puppeteer-core)
Run against
dist/win-unpackedwith a copy of real sessions, no credentials, and separate data folders:The only console errors were unrelated to this change:
404 /favicon.ico, and503 /api/stt/tokenbecause the test server had no provider configured.Not tested
All reactions