From 5cd8ca9b81ff8b4e9f4fdd3d5895624e9585b101 Mon Sep 17 00:00:00 2001 From: Shaun Patterson Date: Thu, 28 May 2026 14:16:18 -0400 Subject: [PATCH] perf(query): faster valToBytes for UidID and PasswordID UidID: build "0x"+hex via strconv.AppendUint into a pre-sized buffer instead of fmt.Sprintf("%#x") (output verified byte-identical, including uid=0 and max uint64); keeps a type-assert fallback. PasswordID: use stringJsonMarshal instead of fmt.Sprintf("%q") -- faster and produces proper JSON string escaping. BenchmarkValToBytes: Uid: 52n -> 18n (-65%), 2 allocs -> 1 Password: 126n -> 35n (-73%), 3 allocs -> 1 --- query/outputnode.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/query/outputnode.go b/query/outputnode.go index 88c6d9a36ec..01a018706d4 100644 --- a/query/outputnode.go +++ b/query/outputnode.go @@ -685,9 +685,17 @@ func valToBytes(v types.Val) ([]byte, error) { b := v.Value.(big.Float) return b.MarshalText() case types.UidID: - return []byte(fmt.Sprintf("\"%#x\"", v.Value)), nil + // Pre-sized: opening quote + "0x" + up to 16 hex digits + closing quote = 20. + uid, ok := v.Value.(uint64) + if !ok { + return []byte(fmt.Sprintf("\"%#x\"", v.Value)), nil + } + buf := make([]byte, 0, 20) + buf = append(buf, '"', '0', 'x') + buf = strconv.AppendUint(buf, uid, 16) + return append(buf, '"'), nil case types.PasswordID: - return []byte(fmt.Sprintf("%q", v.Value.(string))), nil + return stringJsonMarshal(v.Value.(string)), nil case types.VFloatID: return json.Marshal(v.Value.([]float32)) default: