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 <noreply@anthropic.com>
This commit is contained in:
xfy 2026-04-30 15:59:42 +08:00
parent 73ef3a938b
commit 5593a729b8

View File

@ -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
}