use IndexByte replace Split to improve performance

This commit is contained in:
yonbiaoxiao 2020-09-17 19:44:29 +08:00
parent 3100b7cb05
commit 8a66b72613
2 changed files with 11 additions and 1 deletions

View File

@ -103,7 +103,11 @@ func parseAccept(acceptHeader string) []string {
parts := strings.Split(acceptHeader, ",")
out := make([]string, 0, len(parts))
for _, part := range parts {
if part = strings.TrimSpace(strings.Split(part, ";")[0]); part != "" {
i := strings.IndexByte(part, ';')
if i > 0 {
part = part[:i]
}
if part = strings.TrimSpace(part); part != "" {
out = append(out, part)
}
}

View File

@ -18,6 +18,12 @@ func init() {
SetMode(TestMode)
}
func BenchmarkParseAccept(b *testing.B) {
for i := 0; i < b.N; i++ {
parseAccept("text/html , application/xhtml+xml,application/xml;q=0.9, */* ;q=0.8")
}
}
type testStruct struct {
T *testing.T
}