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.
This commit is contained in:
parent
f7997ab5c4
commit
83d4e5e860
@ -6,7 +6,6 @@
|
|||||||
package proxy
|
package proxy
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"hash/fnv"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/valyala/fasthttp"
|
"github.com/valyala/fasthttp"
|
||||||
@ -17,25 +16,41 @@ import (
|
|||||||
// buildCacheKeyHash 使用 FNV-64a 计算缓存键的 uint64 哈希值。
|
// buildCacheKeyHash 使用 FNV-64a 计算缓存键的 uint64 哈希值。
|
||||||
// 使用零分配方式构建哈希,避免 []byte(origKey) 转换。
|
// 使用零分配方式构建哈希,避免 []byte(origKey) 转换。
|
||||||
func (p *Proxy) buildCacheKeyHash(ctx *fasthttp.RequestCtx) (uint64, string) {
|
func (p *Proxy) buildCacheKeyHash(ctx *fasthttp.RequestCtx) (uint64, string) {
|
||||||
h := fnv.New64a()
|
method := ctx.Request.Header.Method()
|
||||||
h.Write(ctx.Request.Header.Method())
|
uri := ctx.Request.URI().RequestURI()
|
||||||
h.Write([]byte(":"))
|
|
||||||
h.Write(ctx.Request.URI().RequestURI())
|
|
||||||
hash := h.Sum64()
|
|
||||||
|
|
||||||
// 仅在需要 origKey 时构建字符串
|
var h uint64 = 14695981039346656037
|
||||||
origKey := b2s(ctx.Request.Header.Method()) + ":" + b2s(ctx.Request.URI().RequestURI())
|
for i := 0; i < len(method); i++ {
|
||||||
return hash, origKey
|
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 {
|
func (p *Proxy) buildCacheKeyHashValue(ctx *fasthttp.RequestCtx) uint64 {
|
||||||
h := fnv.New64a()
|
method := ctx.Request.Header.Method()
|
||||||
h.Write(ctx.Request.Header.Method())
|
uri := ctx.Request.URI().RequestURI()
|
||||||
h.Write([]byte(":"))
|
|
||||||
h.Write(ctx.Request.URI().RequestURI())
|
var h uint64 = 14695981039346656037
|
||||||
return h.Sum64()
|
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 响应上下文。
|
// writeCachedResponse 将缓存的响应写入 FastHTTP 响应上下文。
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user