From 09ce4ac1ca1b1c99f1d3f4516c14b65f1a5ecbf2 Mon Sep 17 00:00:00 2001 From: Ankur Shrivastava Date: Sat, 4 Apr 2026 22:21:38 +0800 Subject: [PATCH] perf: fast-path in validateTraceID for already-valid trace IDs Add a pre-scan loop that checks if all bytes are printable ASCII before allocating a strings.Builder. Most trace IDs (UUIDs, hex) are already clean, so this avoids one allocation per request. --- notifier/notifier.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/notifier/notifier.go b/notifier/notifier.go index 11a1146..6ddc0c4 100644 --- a/notifier/notifier.go +++ b/notifier/notifier.go @@ -634,6 +634,19 @@ func validateTraceID(id string) string { if len(id) > 128 { id = id[:128] } + // Fast path: most trace IDs (UUIDs, hex) are already clean. + // Scan first to avoid strings.Builder allocation. + clean := true + for i := 0; i < len(id); i++ { + if id[i] < 0x20 || id[i] > 0x7E { + clean = false + break + } + } + if clean { + return id + } + // Slow path: sanitize dirty input. var b strings.Builder b.Grow(len(id)) for i := 0; i < len(id); i++ {