From 1ee83b005be490ff693be99b4e6359efec642fcd Mon Sep 17 00:00:00 2001 From: Will Hawkins Date: Sat, 18 Dec 2021 00:55:04 -0500 Subject: Performance (Take 2): A different attempt at improving perf Try intercepting a reader. --- lbc/lbc.go | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) (limited to 'lbc') diff --git a/lbc/lbc.go b/lbc/lbc.go index 38210a3..d0014dd 100644 --- a/lbc/lbc.go +++ b/lbc/lbc.go @@ -37,18 +37,18 @@ func (lbd *LoadBearingConnectionDownload) Client() *http.Client { return lbd.client } -type syntheticCountingWriter struct { - n *uint64 - ctx context.Context +type countingReader struct { + n *uint64 + ctx context.Context + readable io.Reader } -func (s *syntheticCountingWriter) Write(p []byte) (n int, err error) { - if s.ctx.Err() != nil { +func (cr *countingReader) Read(p []byte) (n int, err error) { + if cr.ctx.Err() != nil { return 0, io.EOF } - err = nil - n = len(p) - atomic.AddUint64(s.n, uint64(n)) + n, err = cr.readable.Read(p) + *cr.n += uint64(n) return } @@ -90,10 +90,8 @@ func (lbd *LoadBearingConnectionDownload) doDownload(ctx context.Context) { lbd.valid = false return } - s := &syntheticCountingWriter{n: &lbd.downloaded, ctx: ctx} - // Setup a single buffer! - buffer := make([]byte, chunkSize) - _, err = io.CopyBuffer(s, get.Body, buffer) + cr := &countingReader{n: &lbd.downloaded, ctx: ctx, readable: get.Body} + _, _ = io.ReadAll(cr) lbd.valid = false get.Body.Close() if lbd.debug { -- cgit v1.2.3