From 5593a729b8442bfbe68e5eda5924c8e215dd663d Mon Sep 17 00:00:00 2001 From: xfy Date: Thu, 30 Apr 2026 15:59:42 +0800 Subject: [PATCH] perf(compression): use bytes operations to avoid string allocation - Replace strings.ToLower(string(...)) with bytes.ToLower - Reduces memory allocation in hot path for Accept-Encoding checks Co-Authored-By: Claude Opus 4.7 --- internal/middleware/compression/gzip_static.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/internal/middleware/compression/gzip_static.go b/internal/middleware/compression/gzip_static.go index e19697f..d12e94b 100644 --- a/internal/middleware/compression/gzip_static.go +++ b/internal/middleware/compression/gzip_static.go @@ -25,6 +25,7 @@ package compression import ( + "bytes" "os" "path/filepath" "strings" @@ -216,13 +217,13 @@ func supportsEncoding(acceptEncoding []byte, ext string) bool { if len(acceptEncoding) == 0 { return false } - enc := strings.ToLower(string(acceptEncoding)) + // 使用 bytes 操作避免字符串分配 switch ext { case ".br": - return strings.Contains(enc, "br") + return bytes.Contains(bytes.ToLower(acceptEncoding), []byte("br")) case ".gz": - return strings.Contains(enc, "gzip") + return bytes.Contains(bytes.ToLower(acceptEncoding), []byte("gzip")) default: return false }