Skip to content

Commit

Permalink
proxy: use buffer pool to copy response
Browse files Browse the repository at this point in the history
```
name                        old time/op    new time/op    delta
CopyStream/size_100-8         2.37µs ± 1%    0.07µs ± 3%  -97.09%  (p=0.000 n=9+10)
CopyStream/size_1000-8        2.38µs ± 2%    0.07µs ± 6%  -97.04%  (p=0.000 n=10+10)
CopyStream/size_10000-8       2.38µs ± 1%    0.07µs ± 7%  -97.09%  (p=0.000 n=10+10)
CopyStream/size_100000-8      2.45µs ± 1%    0.07µs ± 1%  -97.20%  (p=0.000 n=10+10)
CopyStream/size_1000000-8     2.54µs ± 2%    0.07µs ± 2%  -97.29%  (p=0.000 n=9+10)
CopyStream/size_10000000-8    2.60µs ± 6%    0.07µs ± 2%  -97.35%  (p=0.000 n=10+10)

name                        old alloc/op   new alloc/op   delta
CopyStream/size_100-8         8.21kB ± 0%    0.02kB ± 0%  -99.81%  (p=0.000 n=10+10)
CopyStream/size_1000-8        8.21kB ± 0%    0.02kB ± 0%  -99.81%  (p=0.000 n=10+10)
CopyStream/size_10000-8       8.21kB ± 0%    0.02kB ± 0%  -99.81%  (p=0.000 n=10+10)
CopyStream/size_100000-8      8.21kB ± 0%    0.02kB ± 0%  -99.81%  (p=0.000 n=10+10)
CopyStream/size_1000000-8     8.21kB ± 0%    0.02kB ± 0%  -99.81%  (p=0.000 n=10+10)
CopyStream/size_10000000-8    8.21kB ± 0%    0.02kB ± 0%  -99.81%  (p=0.000 n=10+10)

name                        old allocs/op  new allocs/op  delta
CopyStream/size_100-8           2.00 ± 0%      1.00 ± 0%  -50.00%  (p=0.000 n=10+10)
CopyStream/size_1000-8          2.00 ± 0%      1.00 ± 0%  -50.00%  (p=0.000 n=10+10)
CopyStream/size_10000-8         2.00 ± 0%      1.00 ± 0%  -50.00%  (p=0.000 n=10+10)
CopyStream/size_100000-8        2.00 ± 0%      1.00 ± 0%  -50.00%  (p=0.000 n=10+10)
CopyStream/size_1000000-8       2.00 ± 0%      1.00 ± 0%  -50.00%  (p=0.000 n=10+10)
CopyStream/size_10000000-8      2.00 ± 0%      1.00 ± 0%  -50.00%  (p=0.000 n=10+10)
```

Signed-off-by: Alexander Yastrebov <alexander.yastrebov@zalando.de>
  • Loading branch information
AlexanderYastrebov committed Feb 10, 2023
1 parent f74b863 commit 0fdda80
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"runtime"
"strconv"
"strings"
"sync"
"time"
"unicode/utf8"

Expand Down Expand Up @@ -425,10 +426,18 @@ func (f *flusher) Write(p []byte) (n int, err error) {
return
}

var bufPool = sync.Pool{
New: func() any {
b := make([]byte, proxyBufferSize)
return &b
},
}

func copyStream(to flushWriter, from io.Reader) (int64, error) {
b := make([]byte, proxyBufferSize)
b := bufPool.Get().(*[]byte)
defer bufPool.Put(b)

return io.CopyBuffer(&flusher{to}, from, b)
return io.CopyBuffer(&flusher{to}, from, *b)
}

func schemeFromRequest(r *http.Request) string {
Expand Down

0 comments on commit 0fdda80

Please sign in to comment.