Skip to content

Commit

Permalink
using acquireBuffer
Browse files Browse the repository at this point in the history
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
```
  • Loading branch information
SVilgelm committed Sep 25, 2023
1 parent 67a05db commit 2b10e62
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,9 @@ func parseRequestURL(c *Client, r *Request) error {
}

if len(params) > 0 {
var (
prev int
buf bytes.Buffer
)
var prev int
buf := acquireBuffer()
defer releaseBuffer(buf)
buf.Grow(len(r.URL))
// search for the next or first opened curly bracket
for curr := strings.Index(r.URL, "{"); curr > prev; curr = prev + strings.Index(r.URL[prev:], "{") {
Expand Down

0 comments on commit 2b10e62

Please sign in to comment.