From 83d4e5e860cc72797314a3e7c67bafe0322d546f Mon Sep 17 00:00:00 2001 From: xfy Date: Thu, 4 Jun 2026 10:45:55 +0800 Subject: [PATCH] perf(proxy): inline FNV-1a in buildCacheKeyHash/buildCacheKeyHashValue Replaces fnv.New64a() with direct inline hash computation over fasthttp's []byte slices, eliminating 1 allocation per cache key computation and 1 []byte(":") allocation. --- internal/proxy/cache_handler.go | 47 ++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/internal/proxy/cache_handler.go b/internal/proxy/cache_handler.go index f646686..c1e1a36 100644 --- a/internal/proxy/cache_handler.go +++ b/internal/proxy/cache_handler.go @@ -6,7 +6,6 @@ package proxy import ( - "hash/fnv" "time" "github.com/valyala/fasthttp" @@ -17,25 +16,41 @@ import ( // buildCacheKeyHash 使用 FNV-64a 计算缓存键的 uint64 哈希值。 // 使用零分配方式构建哈希,避免 []byte(origKey) 转换。 func (p *Proxy) buildCacheKeyHash(ctx *fasthttp.RequestCtx) (uint64, string) { - h := fnv.New64a() - h.Write(ctx.Request.Header.Method()) - h.Write([]byte(":")) - h.Write(ctx.Request.URI().RequestURI()) - hash := h.Sum64() + method := ctx.Request.Header.Method() + uri := ctx.Request.URI().RequestURI() - // 仅在需要 origKey 时构建字符串 - origKey := b2s(ctx.Request.Header.Method()) + ":" + b2s(ctx.Request.URI().RequestURI()) - return hash, origKey + var h uint64 = 14695981039346656037 + for i := 0; i < len(method); i++ { + h ^= uint64(method[i]) + h *= 1099511628211 + } + h ^= uint64(':') + h *= 1099511628211 + for i := 0; i < len(uri); i++ { + h ^= uint64(uri[i]) + h *= 1099511628211 + } + + origKey := b2s(method) + ":" + b2s(uri) + return h, origKey } -// buildCacheKeyHashValue 直接计算缓存键的哈希值,零字符串分配。 -// 用于只需要哈希值而不需要原始键的场景。 func (p *Proxy) buildCacheKeyHashValue(ctx *fasthttp.RequestCtx) uint64 { - h := fnv.New64a() - h.Write(ctx.Request.Header.Method()) - h.Write([]byte(":")) - h.Write(ctx.Request.URI().RequestURI()) - return h.Sum64() + method := ctx.Request.Header.Method() + uri := ctx.Request.URI().RequestURI() + + var h uint64 = 14695981039346656037 + for i := 0; i < len(method); i++ { + h ^= uint64(method[i]) + h *= 1099511628211 + } + h ^= uint64(':') + h *= 1099511628211 + for i := 0; i < len(uri); i++ { + h ^= uint64(uri[i]) + h *= 1099511628211 + } + return h } // writeCachedResponse 将缓存的响应写入 FastHTTP 响应上下文。