perf(observability): do not build event payloads for an empty room
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 <diego.resendez@zero-oneit.com>
This commit is contained in:
3 files changed
+49
-14
No files matched your search
@@ -47,6 +47,13 @@ export function correlateRequest(headers: Record<string, string | string[] | und
|
||||
const incoming = first(headers['x-request-id'])?.trim();
|
||||
const traceId = first(headers.traceparent)?.trim();
|
||||
|
||||
// Eager, and `randomUUID` rather than something cheaper, both deliberately. A lazy getter is
|
||||
// defeated the moment anything spreads this object — which the adapters and the payload builders
|
||||
// both do — so it would buy nothing without threading a nested correlation object through the
|
||||
// whole request path. And a boot-prefix counter measured 5x cheaper (14.7ns vs 77.1ns) but
|
||||
// trades the standard identifier shape for ~1.7% of a real request. Neither is worth its
|
||||
// complexity until a profile says otherwise; the expensive part was building payloads for
|
||||
// nobody, and `Bus.hasListeners` is what fixed that.
|
||||
return { requestId: incoming || crypto.randomUUID(), traceId: traceId || undefined };
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user