From 633ef58032c13a1ebd5ed78798e3156694ef8b63 Mon Sep 17 00:00:00 2001 From: Diego Resendez Date: Sun, 16 Aug 2026 14:26:16 -0500 Subject: [PATCH] perf(observability): do not build event payloads for an empty room MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The correlation work regressed the in-process hot path from 4.52us to 5.43us per request, ~20%. The design predicted 0.25% per step and was wrong by more than an order of magnitude, so recording what the measurement actually said: spreading correlation into payloads ~197ns <- the cost crypto.randomUUID() ~170ns in place try/catch inside the step loop ~2.6ns <- not the cost emit() cannot avoid the first: its argument is already built by the time it is called. Only the caller can decline to build it, so Bus.hasListeners lets it, and runSteps hoists the check out of the loop since listeners are registered at boot. An application subscribing to nothing now pays a branch instead of four payloads and three clock reads for an audience of nobody. That recovers half. 4.96us remains against 4.52us — ~10% in process, ~4.5% against the 10.19us of a real socketed request. Two ways to shave the rest were measured and both rejected in comments: a lazy requestId is defeated by the first spread, and a boot-prefix counter is 5x cheaper but trades the standard identifier shape for ~1.7%. Neither earns its complexity yet. The remaining cost is the feature, not an accident: correlation only exists if something carries it. Signed-off-by: Diego Resendez --- src/bus.ts | 15 +++++++++++++++ src/http/core.ts | 7 +++++++ src/pipeline.ts | 41 +++++++++++++++++++++++++++-------------- 3 files changed, 49 insertions(+), 14 deletions(-) diff --git a/src/bus.ts b/src/bus.ts index 0e6f097..5a3d229 100644 --- a/src/bus.ts +++ b/src/bus.ts @@ -60,6 +60,21 @@ export class Bus { return () => listenerSet.delete(listener); } + /** + * Whether anything is listening to `event` — for skipping the *construction* of a payload nobody + * will read. + * + * Not a micro-optimisation looking for a problem. A correlated payload is built by spreading the + * request's identity into it, and that spread was measured at ~197 ns per request on a two-step + * pipeline — against a ~4.5 µs in-process request, on a path where the overwhelmingly common + * case is an application that subscribes to nothing at all. `emit` cannot help: its argument is + * already built by the time it is called. Only the caller can decline to build it. + */ + hasListeners(event: LifecycleEvent): boolean { + const listenerSet = this.listeners.get(event); + return listenerSet !== undefined && listenerSet.size > 0; + } + /** Dispatch `payload` to every listener of `event`; a throwing listener is isolated and does not affect the others. */ emit(event: LifecycleEvent, payload: EventPayload): void { for (const listener of this.listeners.get(event) ?? []) { diff --git a/src/http/core.ts b/src/http/core.ts index bb3375d..d59999c 100644 --- a/src/http/core.ts +++ b/src/http/core.ts @@ -47,6 +47,13 @@ export function correlateRequest(headers: Record }; return renderError( error,