From 0a72100f003fb7440aeff3c61ee036f59f32d04d Mon Sep 17 00:00:00 2001 From: tyltr <tylitianrui@126.com> Date: Thu, 2 Sep 2021 18:01:55 +0800 Subject: [PATCH 1/5] feat: improve bytesEqual --- header.go | 18 +++++++++--------- header_timing_test.go | 9 +++++++++ 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/header.go b/header.go index 677219ae93..c836e52756 100644 --- a/header.go +++ b/header.go @@ -503,47 +503,47 @@ func (h *RequestHeader) SetRequestURIBytes(requestURI []byte) { // IsGet returns true if request method is GET. func (h *RequestHeader) IsGet() bool { - return bytes.Equal(h.Method(), strGet) + return string(h.Method()) == MethodGet } // IsPost returns true if request method is POST. func (h *RequestHeader) IsPost() bool { - return bytes.Equal(h.Method(), strPost) + return string(h.Method()) == MethodPost } // IsPut returns true if request method is PUT. func (h *RequestHeader) IsPut() bool { - return bytes.Equal(h.Method(), strPut) + return string(h.Method()) == MethodPut } // IsHead returns true if request method is HEAD. func (h *RequestHeader) IsHead() bool { - return bytes.Equal(h.Method(), strHead) + return string(h.Method()) == MethodHead } // IsDelete returns true if request method is DELETE. func (h *RequestHeader) IsDelete() bool { - return bytes.Equal(h.Method(), strDelete) + return string(h.Method()) == MethodDelete } // IsConnect returns true if request method is CONNECT. func (h *RequestHeader) IsConnect() bool { - return bytes.Equal(h.Method(), strConnect) + return string(h.Method()) == MethodConnect } // IsOptions returns true if request method is OPTIONS. func (h *RequestHeader) IsOptions() bool { - return bytes.Equal(h.Method(), strOptions) + return string(h.Method()) == MethodOptions } // IsTrace returns true if request method is TRACE. func (h *RequestHeader) IsTrace() bool { - return bytes.Equal(h.Method(), strTrace) + return string(h.Method()) == MethodTrace } // IsPatch returns true if request method is PATCH. func (h *RequestHeader) IsPatch() bool { - return bytes.Equal(h.Method(), strPatch) + return string(h.Method()) == MethodPatch } // IsHTTP11 returns true if the request is HTTP/1.1. diff --git a/header_timing_test.go b/header_timing_test.go index 8c2f102aff..ae8dba9055 100644 --- a/header_timing_test.go +++ b/header_timing_test.go @@ -178,3 +178,12 @@ func BenchmarkRemoveNewLines(b *testing.B) { }) } } + +func BenchmarkRequestHeaderIsPost(b *testing.B) { + req := &RequestHeader{method: strPost} + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + req.IsPost() + } + }) +} From 9725df819c982fc997f899a1189e4489a0b9746c Mon Sep 17 00:00:00 2001 From: tyltr <tylitianrui@126.com> Date: Thu, 2 Sep 2021 18:31:48 +0800 Subject: [PATCH 2/5] benchmark --- header_timing_test.go | 61 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/header_timing_test.go b/header_timing_test.go index ae8dba9055..bd85be5ff4 100644 --- a/header_timing_test.go +++ b/header_timing_test.go @@ -187,3 +187,64 @@ func BenchmarkRequestHeaderIsPost(b *testing.B) { } }) } +func BenchmarkRequestHeaderIsHead(b *testing.B) { + req := &RequestHeader{method: strHead} + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + req.IsHead() + } + }) +} +func BenchmarkRequestHeaderIsPut(b *testing.B) { + req := &RequestHeader{method: strPut} + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + req.IsPut() + } + }) +} + +func BenchmarkRequestHeaderIsDelete(b *testing.B) { + req := &RequestHeader{method: strDelete} + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + req.IsDelete() + } + }) +} + +func BenchmarkRequestHeaderIsConnect(b *testing.B) { + req := &RequestHeader{method: strConnect} + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + req.IsConnect() + } + }) +} + +func BenchmarkRequestHeaderIsOptions(b *testing.B) { + req := &RequestHeader{method: strOptions} + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + req.IsOptions() + } + }) +} + +func BenchmarkRequestHeaderIsTrace(b *testing.B) { + req := &RequestHeader{method: strTrace} + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + req.IsTrace() + } + }) +} + +func BenchmarkRequestHeaderIsPatch(b *testing.B) { + req := &RequestHeader{method: strPatch} + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + req.IsPatch() + } + }) +} From a0a84ba3f1a8d38d72ad35d3836d245158c7c878 Mon Sep 17 00:00:00 2001 From: tyltr <tylitianrui@126.com> Date: Thu, 2 Sep 2021 18:39:33 +0800 Subject: [PATCH 3/5] nolint:unused --- strings.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/strings.go b/strings.go index 2a3d553c26..6870a65949 100644 --- a/strings.go +++ b/strings.go @@ -24,15 +24,15 @@ var ( strResponseContinue = []byte("HTTP/1.1 100 Continue\r\n\r\n") - strGet = []byte(MethodGet) - strHead = []byte(MethodHead) - strPost = []byte(MethodPost) - strPut = []byte(MethodPut) - strDelete = []byte(MethodDelete) - strConnect = []byte(MethodConnect) - strOptions = []byte(MethodOptions) - strTrace = []byte(MethodTrace) - strPatch = []byte(MethodPatch) + strGet = []byte(MethodGet) //nolint:unused + strHead = []byte(MethodHead) //nolint:unused + strPost = []byte(MethodPost) //nolint:unused + strPut = []byte(MethodPut) //nolint:unused + strDelete = []byte(MethodDelete) //nolint:unused + strConnect = []byte(MethodConnect) //nolint:unused + strOptions = []byte(MethodOptions) //nolint:unused + strTrace = []byte(MethodTrace) //nolint:unused + strPatch = []byte(MethodPatch) //nolint:unused strExpect = []byte(HeaderExpect) strConnection = []byte(HeaderConnection) From 1ecb699b0a521ca1dc3cc5351e52d3368449a8ef Mon Sep 17 00:00:00 2001 From: tyltr <tylitianrui@126.com> Date: Thu, 2 Sep 2021 20:15:07 +0800 Subject: [PATCH 4/5] remove unused code --- client.go | 2 +- header.go | 2 +- header_timing_test.go | 67 ++----------------------------------------- strings.go | 10 ------- 4 files changed, 5 insertions(+), 76 deletions(-) diff --git a/client.go b/client.go index 832513fe25..9121107e85 100644 --- a/client.go +++ b/client.go @@ -950,7 +950,7 @@ var clientURLResponseChPool sync.Pool func clientPostURL(dst []byte, url string, postArgs *Args, c clientDoer) (statusCode int, body []byte, err error) { req := AcquireRequest() - req.Header.SetMethodBytes(strPost) + req.Header.SetMethodBytes([]byte(MethodPost)) req.Header.SetContentTypeBytes(strPostArgsContentType) if postArgs != nil { if _, err := postArgs.WriteTo(req.BodyWriter()); err != nil { diff --git a/header.go b/header.go index c836e52756..0eb4b7ba42 100644 --- a/header.go +++ b/header.go @@ -443,7 +443,7 @@ func (h *RequestHeader) SetRefererBytes(referer []byte) { // Method returns HTTP request method. func (h *RequestHeader) Method() []byte { if len(h.method) == 0 { - return strGet + return []byte(MethodGet) } return h.method } diff --git a/header_timing_test.go b/header_timing_test.go index bd85be5ff4..745b5c303e 100644 --- a/header_timing_test.go +++ b/header_timing_test.go @@ -179,72 +179,11 @@ func BenchmarkRemoveNewLines(b *testing.B) { } } -func BenchmarkRequestHeaderIsPost(b *testing.B) { - req := &RequestHeader{method: strPost} +func BenchmarkRequestHeaderIsGet(b *testing.B) { + req := &RequestHeader{method: []byte(MethodGet)} b.RunParallel(func(pb *testing.PB) { for pb.Next() { - req.IsPost() - } - }) -} -func BenchmarkRequestHeaderIsHead(b *testing.B) { - req := &RequestHeader{method: strHead} - b.RunParallel(func(pb *testing.PB) { - for pb.Next() { - req.IsHead() - } - }) -} -func BenchmarkRequestHeaderIsPut(b *testing.B) { - req := &RequestHeader{method: strPut} - b.RunParallel(func(pb *testing.PB) { - for pb.Next() { - req.IsPut() - } - }) -} - -func BenchmarkRequestHeaderIsDelete(b *testing.B) { - req := &RequestHeader{method: strDelete} - b.RunParallel(func(pb *testing.PB) { - for pb.Next() { - req.IsDelete() - } - }) -} - -func BenchmarkRequestHeaderIsConnect(b *testing.B) { - req := &RequestHeader{method: strConnect} - b.RunParallel(func(pb *testing.PB) { - for pb.Next() { - req.IsConnect() - } - }) -} - -func BenchmarkRequestHeaderIsOptions(b *testing.B) { - req := &RequestHeader{method: strOptions} - b.RunParallel(func(pb *testing.PB) { - for pb.Next() { - req.IsOptions() - } - }) -} - -func BenchmarkRequestHeaderIsTrace(b *testing.B) { - req := &RequestHeader{method: strTrace} - b.RunParallel(func(pb *testing.PB) { - for pb.Next() { - req.IsTrace() - } - }) -} - -func BenchmarkRequestHeaderIsPatch(b *testing.B) { - req := &RequestHeader{method: strPatch} - b.RunParallel(func(pb *testing.PB) { - for pb.Next() { - req.IsPatch() + req.IsGet() } }) } diff --git a/strings.go b/strings.go index 6870a65949..42f73c5c58 100644 --- a/strings.go +++ b/strings.go @@ -24,16 +24,6 @@ var ( strResponseContinue = []byte("HTTP/1.1 100 Continue\r\n\r\n") - strGet = []byte(MethodGet) //nolint:unused - strHead = []byte(MethodHead) //nolint:unused - strPost = []byte(MethodPost) //nolint:unused - strPut = []byte(MethodPut) //nolint:unused - strDelete = []byte(MethodDelete) //nolint:unused - strConnect = []byte(MethodConnect) //nolint:unused - strOptions = []byte(MethodOptions) //nolint:unused - strTrace = []byte(MethodTrace) //nolint:unused - strPatch = []byte(MethodPatch) //nolint:unused - strExpect = []byte(HeaderExpect) strConnection = []byte(HeaderConnection) strContentLength = []byte(HeaderContentLength) From 9bc165696072a620986532a80cfa33ff7f28809c Mon Sep 17 00:00:00 2001 From: tyltr <tylitianrui@126.com> Date: Mon, 6 Sep 2021 17:29:55 +0800 Subject: [PATCH 5/5] Update client.go Co-authored-by: Erik Dubbelboer <erik@dubbelboer.com> --- client.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client.go b/client.go index 9121107e85..47bbd58ed4 100644 --- a/client.go +++ b/client.go @@ -950,7 +950,7 @@ var clientURLResponseChPool sync.Pool func clientPostURL(dst []byte, url string, postArgs *Args, c clientDoer) (statusCode int, body []byte, err error) { req := AcquireRequest() - req.Header.SetMethodBytes([]byte(MethodPost)) + req.Header.SetMethod(MethodPost) req.Header.SetContentTypeBytes(strPostArgsContentType) if postArgs != nil { if _, err := postArgs.WriteTo(req.BodyWriter()); err != nil {