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 6674c2f
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6674c2f

Please sign in to comment.