Author batches glimmer and Datastar rendering per tick to cut repeated work
When a server streams HTML to the browser, the most direct approach is to re-render and push on every state change. While building glimmer and glimmer-datastar for his own jolt stack, the author found that this naive approach degrades under load into "number of writes × number of open pages" renders, with most results discarded.
When a server streams HTML to the browser, the most direct approach is to re-render and push on every state change. While building glimmer and glimmer-datastar for his own jolt stack, the author found that this naive approach degrades under load into "number of writes × number of open pages" renders, with most results discarded.
The author's core idea is to decouple writes from rendering: a write only marks the pages that have read that state as dirty, and a tick loop at a fixed interval renders them together, with each dirty page rendered once per tick. Slow clients do not pile up; each stream has only one sliding buffer, and old frames are dropped when a new frame arrives. Writes can also optionally be queued with tx! to run at the start of the next tick, so rendering never sees a half-finished state. Duplicate work across pages is also removed: the component cache is keyed by "function plus arguments", so the same 300-line dashboard block shares one render result across multiple pages. The hard part is deciding when the cache goes stale; the author uses capture to record which reactive cells a component read and their values at the time, and refreshes when a value changes. The cache only accepts entries requested more than once, and evicts them by generation. Once rendering is no longer the bottleneck, the network becomes the main cost: a one-line change still sends a 40 KB full page, gzip largely stops helping beyond its 32 KB window, and a one-line change still costs 4316 bytes. So components with an id are treated as page fragments, the stream compares old and new pages fragment by fragment and sends only the changed parts, and the front end Datastar replaces them by id; it still falls back to the full page when page structure changes or the client drops frames. The author says this cut each tick from 21.7 ms to 7.0 ms. Finally there is derived state: glimmer.table borrows from DBSP, tables store rows by key, views process only changed rows, and reversible aggregates such as count and sum only need to take out the old row and put in the new one, so a write costs one row rather than the whole table. When a page closes or a 401 arrives, the stream cancels all subscriptions so the connection can be reclaimed. The author's Newsroom application is the worst case: multi-threaded source fetching and model streaming output updating several times per second; after switching to a 200 ms tick, each page renders at most five times per second, and only one line of configuration changed.
The author's experience shows that the key to server-side reactive rendering performance is using dependency tracking to decide both "what to re-render" and "what can be skipped", shaving off repeated work layer by layer through ticks, caching, incremental patches and incremental aggregation. When building similar real-time pages, batching and merging writes and pushing by fragment should come before optimising a single render.