From 6674c2ff26477ac62ff9adf286cb2550a4219d9a Mon Sep 17 00:00:00 2001 From: Sergey Vilgelm Date: Mon, 25 Sep 2023 10:44:09 -0700 Subject: [PATCH] using acquireBuffer reusing a buffer from the pool decreases the allocs and memory usage ```shell % go test -benchmem -bench=. -run=^Benchmark goos: darwin goarch: amd64 pkg: github.com/go-resty/resty/v2 cpu: Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz Benchmark_parseRequestURL_PathParams-16 753834 1367 ns/op 256 B/op 5 allocs/op Benchmark_parseRequestURL_QueryParams-16 1000000 1167 ns/op 352 B/op 9 allocs/op PASS ok github.com/go-resty/resty/v2 2.373s ``` --- middleware.go | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/middleware.go b/middleware.go index 445f7360..94f1972c 100644 --- a/middleware.go +++ b/middleware.go @@ -54,11 +54,9 @@ func parseRequestURL(c *Client, r *Request) error { } if len(params) > 0 { - var ( - prev int - buf bytes.Buffer - ) - buf.Grow(len(r.URL)) + var prev int + buf := acquireBuffer() + defer releaseBuffer(buf) // search for the next or first opened curly bracket for curr := strings.Index(r.URL, "{"); curr > prev; curr = prev + strings.Index(r.URL[prev:], "{") { // write everything form the previous position up to the current