perf(api): avoid rune decoding in route label checks

Route labels classify ASCII digits and hexadecimal UUID bytes. Scan those
bytes directly while preserving numeric precedence, Unicode names and
invalid UTF-8 handling. This reduces the shared normaliser overhead exposed
by paired landing benchmarks without changing the benchmark gate.

Refs #1928

Contract-Neutral: ASCII route-label optimization preserves label values, identifier precedence, Unicode and invalid UTF-8 behavior, and agent lifecycle authority.
This commit is contained in:
rcourtman
2026-09-06 08:31:26 +01:00
parent ba69933da3
commit a2f0ef8817
5 changed files with 63 additions and 4 deletions
@@ -1290,3 +1290,41 @@ remains enforced. Further prompt or orchestration rules are not justified merely
because this response ignored already explicit evidence limitations. Real-model
diagnosis, approved/rejected action outcomes and wider customer readiness remain
open qualification requirements.
### Landing benchmark follow-through
Run `34017211910` on `173d74a8e422` completed with the frontend, full API
race shard, both remaining backend shards and build/smoke checks passing. Its
only failed job was the unchanged benchmark gate, with four NormalizeSegment
time regressions against exact base `3347f561ec7bc7ae30903e64998b0f90b5fb5217`.
A ten-pair, 500ms-sample worker reproduction confirmed three segment regressions,
while full middleware time and allocations remained unchanged. Both source
files and normalized compiled instruction streams matched between base and
candidate. The binary addresses differed. Layout sensitivity is an inference
from these observations, not a proven functional defect.
The canonical route-label classifiers now inspect ASCII bytes directly instead
of decoding Unicode runes that cannot satisfy the numeric/hexadecimal checks.
Existing label precedence and non-ASCII behavior are preserved, with regression
cases for long numeric IDs, Unicode digits/names and malformed UTF-8. The full
HTTP-metrics test file passes under the race detector in 1.057s on Go 1.26.8.
Ten new alternating baseline/candidate pairs pass the existing greater-than-10%,
p-less-than-0.05 gate with 500ms samples. Segment numeric, UUID, long-token,
short-name and medium-name time changes are -32.19%, -33.59%, -38.83%, -37.06%
and -37.11%. Bytes and allocations are unchanged. Adjacent route and full
middleware benchmarks have no significant regressions. These are local
microbenchmark results, not a claim about customer-perceived application speed.
Final source hashes:
- `internal/api/http_metrics.go`: `916c27ad5ff07e00170dd94df0b75eb509dd329e34288647f5358a0759353d37`
- `internal/api/http_metrics_test.go`: `4d425559d84052a50de56286101a6b37422027629d076c845920d8d02f9aff41`
Worker comparison: `/opt/pulse-release-worker/patrol-normalize-final-bench/`.
Private copies, the failed initial comparison and exact CI failure output remain
at workspace-relative `tmp/patrol-current-assistant-check/`. The preceding
diagnostic-record commit `ba69933da352` passed its two-file staged hook, all
163 tests in 127.527s with unchanged hashes. The current runtime change still
requires its final staged hook and exact-head remote CI. Overall diagnostic and
action-outcome qualification remains open.
File diff suppressed because one or more lines are too long
@@ -3104,3 +3104,15 @@ The signal survives transport loss and resets on organisation URL changes.
On admission-request failure retain the existing facet without extra retries,
polling or full-estate reads. A later successful refresh replaces that facet.
### ASCII route-label classification
HTTP metric route normalization classifies numeric IDs and UUID-like labels
using ASCII byte checks. Unicode decoding is unnecessary for this alphabet.
Preserve numeric-before-token precedence and ordinary Unicode or invalid UTF-8
names while optimizing this shared path. Qualification compares exact base and
candidate on the same worker with alternating samples, and retains full-route
and middleware controls. Identical source or instruction sequences alone do not
prove identical timing. The recorded final ten-pair check passes the unchanged
time/bytes/allocation gate with no adjacent request-path regression.
+5 -3
View File
@@ -140,8 +140,9 @@ func isNumeric(s string) bool {
if s == "" {
return false
}
for _, r := range s {
if r < '0' || r > '9' {
// These labels accept ASCII digits only, so decoding UTF-8 is unnecessary.
for i := 0; i < len(s); i++ {
if s[i] < '0' || s[i] > '9' {
return false
}
}
@@ -152,7 +153,8 @@ func looksLikeUUID(s string) bool {
if len(s) != 36 {
return false
}
for i, r := range s {
for i := 0; i < len(s); i++ {
r := s[i]
switch {
case r == '-':
if i != 8 && i != 13 && i != 18 && i != 23 {
+7
View File
@@ -83,6 +83,8 @@ func TestIsNumeric(t *testing.T) {
{"hexadecimal prefix", "0x10", false},
{"special characters", "12@34", false},
{"unicode digits", "123", false}, // fullwidth digits
{"arabic digits", "١٢٣", false},
{"invalid utf8 after digits", "123\xff", false},
}
for _, tt := range tests {
@@ -127,6 +129,8 @@ func TestLooksLikeUUID(t *testing.T) {
{"space in uuid", "550e8400 e29b-41d4-a716-446655440000", false},
{"underscore", "550e8400_e29b-41d4-a716-446655440000", false},
{"special char", "550e8400-e29b-41d4-a716-44665544000!", false},
{"unicode within 36 bytes", "é0e8400-e29b-41d4-a716-446655440000", false},
{"invalid utf8 within 36 bytes", "\xff50e8400-e29b-41d4-a716-446655440000", false},
// Edge cases
{"all zeros no dashes wrong length", "00000000000000000000000000000000xxxx", false},
@@ -153,6 +157,7 @@ func TestNormalizeSegment(t *testing.T) {
{"numeric id", "123", ":id"},
{"single digit", "5", ":id"},
{"large number", "9999999999", ":id"},
{"numeric precedence over token", "123456789012345678901234567890123456", ":id"},
// UUID segments -> :uuid
{"uuid", "550e8400-e29b-41d4-a716-446655440000", ":uuid"},
@@ -173,6 +178,8 @@ func TestNormalizeSegment(t *testing.T) {
{"empty string", "", ""},
{"single letter", "a", "a"},
{"mixed alphanumeric short", "user123", "user123"},
{"unicode name", "café", "café"},
{"invalid utf8 name", "node\xff", "node\xff"},
}
for _, tt := range tests {