Issue #278: optimize normalizeHeaderKey a bit

Performance results on amd64:

name                            old time/op  new time/op  delta
NormalizeHeaderKeyCommonCase-4  43.3ns ± 0%  32.6ns ± 1%  -24.80%  (p=0.000 n=9+10)
NormalizeHeaderKeyLowercase-4   42.6ns ± 3%  32.6ns ± 1%  -23.41%  (p=0.000 n=10+10)
NormalizeHeaderKeyUppercase-4   43.5ns ± 1%  32.6ns ± 2%  -25.03%  (p=0.000 n=9+8)

Based on top of https://github.com/valyala/fasthttp/pull/279 .
This commit is contained in:
Aliaksandr Valialkin
2017-07-10 12:54:13 +03:00
parent 498431ce67
commit 952171f61b
2 changed files with 35 additions and 18 deletions
+24 -8
View File
@@ -343,18 +343,34 @@ func hexbyte2int(c byte) int {
const toLower = 'a' - 'A'
func uppercaseByte(p *byte) {
c := *p
if c >= 'a' && c <= 'z' {
*p = c - toLower
var toLowerTable = func() [256]byte {
var a [256]byte
for i := 0; i < 256; i++ {
c := byte(i)
if c >= 'A' && c <= 'Z' {
a[i] = toLower
}
}
return a
}()
var toUpperTable = func() [256]byte {
var a [256]byte
for i := 0; i < 256; i++ {
c := byte(i)
if c >= 'a' && c <= 'z' {
a[i] = 256 - toLower
}
}
return a
}()
func uppercaseByte(p *byte) {
*p += toUpperTable[*p]
}
func lowercaseByte(p *byte) {
c := *p
if c >= 'A' && c <= 'Z' {
*p = c + toLower
}
*p += toLowerTable[*p]
}
func lowercaseBytes(b []byte) {
+11 -10
View File
@@ -2014,19 +2014,20 @@ func normalizeHeaderKey(b []byte, disableNormalizing bool) {
}
n := len(b)
up := true
for i := 0; i < n; i++ {
switch b[i] {
case '-':
up = true
default:
if up {
up = false
if n == 0 {
return
}
uppercaseByte(&b[0])
for i := 1; i < n; i++ {
if b[i] == '-' {
i++
if i < n {
uppercaseByte(&b[i])
} else {
lowercaseByte(&b[i])
}
continue
}
lowercaseByte(&b[i])
}
}