Our desktop cockpit is an Electron application for watching and steering a fleet of Claude Code agent sessions that run across several subscription accounts at once. It shows session output in xterm terminals and keeps accounting of their scrollback. This note covers one change to that accounting and the measurement taken for it.
What changed in the scrollback accounting?
Before the change, scrollback accounting used a per-write recount, so each write into the terminal triggered a fresh count. After the change, the accounting is O(1). This note concerns only that accounting change. It makes no statement about rendering, the terminal library, or any other part of the application.
Why is a per-write recount expensive?
In general, a recount scales with the total amount of state being counted, not with the size of the write that triggered it. When small writes arrive often against a large buffer, each write pays for the whole buffer just to account for a few new characters. A constant-time update instead adjusts a running value from the incoming delta, which removes the dependence on buffer size. That is the general shape of this class of problem. It is offered as background, not as a diagnosis of the internals of our implementation.
How much did O(1) accounting save?
| Measurement | Value |
|---|---|
| Measured cost before (per-write recount) | 9405ms |
| Measured cost after (O(1) accounting) | 5.4ms |
| Speedup | 1741x |
The rounded figure of roughly 1700x comes from the 1741x ratio. A gap that large between two implementations of the same bookkeeping is a reminder that an accounting routine can carry real cost. Code like this is easy to treat as negligible because it is not the feature itself.
What does this measurement not tell you?
The recorded result consists of a before figure, an after figure, and their ratio. It does not record:
- The workload or volume of output used.
- The scrollback size.
- The number of concurrent sessions.
- The hardware.
- Whether the figures are per operation or totals across a run.
- Whether the timing covered only the accounting routine or the full write path.
- How the before figure appeared to the person using the application.
Because of these gaps, the ratio should not be read as the accounting's share of total terminal work. It also should not be read as a multiplier you can expect on other workloads. It shows how the two accounting approaches compared under the conditions that were measured.
Why should multi-session tools pay attention to per-write costs?
A tool built to show many terminals side by side multiplies whatever each terminal does per write. A routine that looks harmless when one terminal runs it can deserve a second look once dozens of terminals run it at the same time. This is a design consideration for that class of tool. It is not a measured finding about how our cockpit behaves under load.
How should you audit your own terminal and log views for this?
- Inspect every per-write path. Look at anything that runs per write, per chunk or per event. Ask whether its cost depends on total accumulated state or only on the incoming data.
- Time bookkeeping on its own. If counters, totals or limits are recomputed inside a write handler, measure them separately. Overhead inside a handler is easy to miss when you profile only the handler as a whole.
- Maintain counts incrementally. Where a value can be updated from the delta, update it from the delta. Save full recounts for rare events.
- Record what you measured. Write down the workload, buffer size, concurrency, and what the timer actually wrapped. That record is what lets a reader interpret a before/after ratio correctly.
- Re-measure under the same setup. A before/after ratio only means something when both sides ran under identical conditions.
The practical lesson is to treat accounting code as a first-class candidate for profiling rather than an afterthought. A recount that runs on every write is worth questioning wherever it appears.