Optimized Peek() function for args and request header cookies

This commit is contained in:
Aliaksandr Valialkin
2015-11-14 20:38:47 +02:00
parent 91f896309a
commit 8a42c802f5
2 changed files with 20 additions and 12 deletions
+15 -6
View File
@@ -137,8 +137,7 @@ func (a *Args) SetBytesKV(key, value []byte) {
//
// Returned value is valid until the next Args call.
func (a *Args) Peek(key string) []byte {
a.bufKV.key = AppendBytesStr(a.bufKV.key[:0], key)
return a.PeekBytes(a.bufKV.key)
return peekArgStr(a.args, key)
}
// PeekBytes returns query arg value for the given key.
@@ -147,7 +146,7 @@ func (a *Args) Peek(key string) []byte {
//
// It is safe modifying key buffer after PeekBytes return.
func (a *Args) PeekBytes(key []byte) []byte {
return peekArg(a.args, key)
return peekArgBytes(a.args, key)
}
// Has returns true if the given key exists in Args.
@@ -282,17 +281,27 @@ func releaseArg(h []argsKV) []argsKV {
func hasArg(h []argsKV, k []byte) bool {
for i, n := 0, len(h); i < n; i++ {
kv := &h[i]
if bytes.Equal(k, kv.key) {
if bytes.Equal(kv.key, k) {
return true
}
}
return false
}
func peekArg(h []argsKV, k []byte) []byte {
func peekArgBytes(h []argsKV, k []byte) []byte {
for i, n := 0, len(h); i < n; i++ {
kv := &h[i]
if bytes.Equal(k, kv.key) {
if bytes.Equal(kv.key, k) {
return kv.value
}
}
return nil
}
func peekArgStr(h []argsKV, k string) []byte {
for i, n := 0, len(h); i < n; i++ {
kv := &h[i]
if EqualBytesStr(kv.key, k) {
return kv.value
}
}
+5 -6
View File
@@ -549,7 +549,7 @@ func (h *ResponseHeader) peek(key []byte) []byte {
}
return nil
default:
return peekArg(h.h, key)
return peekArgBytes(h.h, key)
}
}
@@ -567,26 +567,25 @@ func (h *RequestHeader) peek(key []byte) []byte {
}
return nil
default:
return peekArg(h.h, key)
return peekArgBytes(h.h, key)
}
}
// PeekCookie returns cookie for the given key.
func (h *RequestHeader) PeekCookie(key string) []byte {
h.bufKV.key = AppendBytesStr(h.bufKV.key[:0], key)
return h.PeekCookieBytes(h.bufKV.key)
return peekArgStr(h.cookies, key)
}
// PeekCookieBytes returns cookie for the given key.
func (h *RequestHeader) PeekCookieBytes(key []byte) []byte {
return peekArg(h.cookies, key)
return peekArgBytes(h.cookies, key)
}
// GetCookie fills cookie for the given cookie.Key.
//
// Returns false if cookie with the given cookie.Key is missing.
func (h *ResponseHeader) GetCookie(cookie *Cookie) bool {
v := peekArg(h.cookies, cookie.Key)
v := peekArgBytes(h.cookies, cookie.Key)
if v == nil {
return false
}