Skip to content

Commit

Permalink
Removed accessors converting []byte to string, i.e. allocating memory…
Browse files Browse the repository at this point in the history
…. This should reduce memory usage for apps using fasthttp, since now they shoud either use []byte or do string() conversion on their own :)
  • Loading branch information
valyala committed Nov 14, 2015
1 parent 2e78d83 commit 88e41d9
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 201 deletions.
16 changes: 0 additions & 16 deletions args.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,22 +133,6 @@ func (a *Args) SetBytesKV(key, value []byte) {
a.args = setArg(a.args, key, value)
}

// Get returns query arg value for the given key.
//
// Each Get call allocates memory for returned string,
// so consider using Peek() instead.
func (a *Args) Get(key string) string {
return string(a.Peek(key))
}

// GetBytes returns query arg value for the given key.
//
// Each GetBytes call allocates memory for returned string,
// so consider using PeekBytes() instead.
func (a *Args) GetBytes(key []byte) string {
return string(a.PeekBytes(key))
}

// Peek returns query arg value for the given key.
//
// Returned value is valid until the next Args call.
Expand Down
44 changes: 22 additions & 22 deletions args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ func testArgsString(t *testing.T, a *Args, s string) {
func TestArgsSetGetDel(t *testing.T) {
var a Args

if a.Get("foo") != "" {
t.Fatalf("Unexpected value: %q", a.Get("foo"))
if len(a.Peek("foo")) > 0 {
t.Fatalf("Unexpected value: %q", a.Peek("foo"))
}
if a.Get("") != "" {
t.Fatalf("Unexpected value: %q", a.Get(""))
if len(a.Peek("")) > 0 {
t.Fatalf("Unexpected value: %q", a.Peek(""))
}
a.Del("xxx")

Expand All @@ -112,51 +112,51 @@ func TestArgsSetGetDel(t *testing.T) {
k := fmt.Sprintf("foo%d", i)
v := fmt.Sprintf("bar_%d", i)
a.Set(k, v)
if a.Get(k) != v {
t.Fatalf("Unexpected value: %q. Expected %q", a.Get(k), v)
if string(a.Peek(k)) != v {
t.Fatalf("Unexpected value: %q. Expected %q", a.Peek(k), v)
}
}
}
for i := 0; i < 10; i++ {
k := fmt.Sprintf("foo%d", i)
v := fmt.Sprintf("bar_%d", i)
if a.Get(k) != v {
t.Fatalf("Unexpected value: %q. Expected %q", a.Get(k), v)
if string(a.Peek(k)) != v {
t.Fatalf("Unexpected value: %q. Expected %q", a.Peek(k), v)
}
a.Del(k)
if a.Get(k) != "" {
t.Fatalf("Unexpected value: %q. Expected %q", a.Get(k), "")
if string(a.Peek(k)) != "" {
t.Fatalf("Unexpected value: %q. Expected %q", a.Peek(k), "")
}
}

a.Parse("aaa=xxx&bb=aa")
if a.Get("foo0") != "" {
t.Fatalf("Unepxected value %q", a.Get("foo0"))
if string(a.Peek("foo0")) != "" {
t.Fatalf("Unepxected value %q", a.Peek("foo0"))
}
if a.Get("aaa") != "xxx" {
t.Fatalf("Unexpected value %q. Expected %q", a.Get("aaa"), "xxx")
if string(a.Peek("aaa")) != "xxx" {
t.Fatalf("Unexpected value %q. Expected %q", a.Peek("aaa"), "xxx")
}
if a.Get("bb") != "aa" {
t.Fatalf("Unexpected value %q. Expected %q", a.Get("bb"), "aa")
if string(a.Peek("bb")) != "aa" {
t.Fatalf("Unexpected value %q. Expected %q", a.Peek("bb"), "aa")
}

for i := 0; i < 10; i++ {
k := fmt.Sprintf("xx%d", i)
v := fmt.Sprintf("yy%d", i)
a.Set(k, v)
if a.Get(k) != v {
t.Fatalf("Unexpected value: %q. Expected %q", a.Get(k), v)
if string(a.Peek(k)) != v {
t.Fatalf("Unexpected value: %q. Expected %q", a.Peek(k), v)
}
}
for i := 5; i < 10; i++ {
k := fmt.Sprintf("xx%d", i)
v := fmt.Sprintf("yy%d", i)
if a.Get(k) != v {
t.Fatalf("Unexpected value: %q. Expected %q", a.Get(k), v)
if string(a.Peek(k)) != v {
t.Fatalf("Unexpected value: %q. Expected %q", a.Peek(k), v)
}
a.Del(k)
if a.Get(k) != "" {
t.Fatalf("Unexpected value: %q. Expected %q", a.Get(k), "")
if string(a.Peek(k)) != "" {
t.Fatalf("Unexpected value: %q. Expected %q", a.Peek(k), "")
}
}
}
Expand Down
97 changes: 8 additions & 89 deletions header.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,7 @@ type RequestHeader struct {
}

// ContentType returns Content-Type header value.
//
// This function allocates memory on each call, so consider
// using ContentTypeBytes instead.
func (h *ResponseHeader) ContentType() string {
return string(h.ContentTypeBytes())
}

// ContentTypeBytes returns Content-Type header value.
func (h *ResponseHeader) ContentTypeBytes() []byte {
func (h *ResponseHeader) ContentType() []byte {
return h.contentType
}

Expand All @@ -89,15 +81,7 @@ func (h *ResponseHeader) SetContentTypeBytes(contentType []byte) {
}

// ContentType returns Content-Type header value.
//
// This function allocates memory on each call, so consider
// using ContentTypeBytes instead.
func (h *RequestHeader) ContentType() string {
return string(h.ContentTypeBytes())
}

// ContentTypeBytes returns Content-Type header value.
func (h *RequestHeader) ContentTypeBytes() []byte {
func (h *RequestHeader) ContentType() []byte {
return h.contentType
}

Expand All @@ -114,14 +98,7 @@ func (h *RequestHeader) SetContentTypeBytes(contentType []byte) {
}

// Host returns Host header value.
//
// This function allocates memory on each call, so consider using HostBytes.
func (h *RequestHeader) Host() string {
return string(h.HostBytes())
}

// HostBytes returns Host header value.
func (h *RequestHeader) HostBytes() []byte {
func (h *RequestHeader) Host() []byte {
return h.host
}

Expand All @@ -138,15 +115,7 @@ func (h *RequestHeader) SetHostBytes(host []byte) {
}

// UserAgent returns User-Agent header value.
//
// This function allocates memory on each call, so consider
// using UserAgentBytes.
func (h *RequestHeader) UserAgent() string {
return string(h.UserAgentBytes())
}

// UserAgentBytes returns User-Agent header value.
func (h *RequestHeader) UserAgentBytes() []byte {
func (h *RequestHeader) UserAgent() []byte {
return h.userAgent
}

Expand All @@ -163,14 +132,7 @@ func (h *RequestHeader) SetUserAgentBytes(userAgent []byte) {
}

// Method returns HTTP request method.
//
// Method allocates memory on each call, so consider using MethodBytes.
func (h *RequestHeader) Method() string {
return string(h.MethodBytes())
}

// MethodBytes returns HTTP request method.
func (h *RequestHeader) MethodBytes() []byte {
func (h *RequestHeader) Method() []byte {
if len(h.method) == 0 {
return strGet
}
Expand Down Expand Up @@ -212,17 +174,17 @@ func (h *RequestHeader) SetRequestURIBytes(requestURI []byte) {

// IsGet returns true if request method is GET.
func (h *RequestHeader) IsGet() bool {
return bytes.Equal(h.MethodBytes(), strGet)
return bytes.Equal(h.Method(), strGet)
}

// IsPost returns true if request methos is POST.
func (h *RequestHeader) IsPost() bool {
return bytes.Equal(h.MethodBytes(), strPost)
return bytes.Equal(h.Method(), strPost)
}

// IsHead returns true if request method is HEAD.
func (h *RequestHeader) IsHead() bool {
return bytes.Equal(h.MethodBytes(), strHead)
return bytes.Equal(h.Method(), strHead)
}

// Len returns the number of headers set, not counting Content-Length,
Expand Down Expand Up @@ -620,49 +582,6 @@ func (h *RequestHeader) PeekCookieBytes(key []byte) []byte {
return peekArg(h.cookies, key)
}

// Get returns header value for the given key.
//
// Get allocates memory on each call, so prefer using Peek instead.
func (h *ResponseHeader) Get(key string) string {
return string(h.Peek(key))
}

// GetBytes returns header value for the given key.
//
// GetBytes allocates memory on each call, so prefer using PeekBytes instead.
func (h *ResponseHeader) GetBytes(key []byte) string {
return string(h.PeekBytes(key))
}

// Get returns header value for the given key.
//
// Get allocates memory on each call, so prefer using Peek instead.
func (h *RequestHeader) Get(key string) string {
return string(h.Peek(key))
}

// GetBytes returns header value for the given key.
//
// GetBytes allocates memory on each call, so prefer using PeekBytes instead.
func (h *RequestHeader) GetBytes(key []byte) string {
return string(h.PeekBytes(key))
}

// GetCookie returns cookie for the given key.
//
// GetCookie allocates memory on each call, so prefere using PeekCookie instead.
func (h *RequestHeader) GetCookie(key string) string {
return string(h.PeekCookie(key))
}

// GetCookieBytes returns cookie for the given key.
//
// GetCookieBytes allocates memory on each call, so prefer using PeekCookieBytes
// instead.
func (h *RequestHeader) GetCookieBytes(key []byte) string {
return string(h.PeekCookieBytes(key))
}

// GetCookie fills cookie for the given cookie.Key.
//
// Returns false if cookie with the given cookie.Key is missing.
Expand Down
Loading

0 comments on commit 88e41d9

Please sign in to comment.