From 50e190ff54c9e3b94f6e1f48be5b66b5e113e561 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Thu, 9 Feb 2017 11:46:53 +0000 Subject: [PATCH] cat: don't allocate buffers if not needed to reduce memory usage --- fs/operations.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/fs/operations.go b/fs/operations.go index d60a9234e..f7011c6b2 100644 --- a/fs/operations.go +++ b/fs/operations.go @@ -1262,10 +1262,13 @@ func Cat(f Fs, w io.Writer, offset, count int64) error { defer func() { Stats.DoneTransferring(o.Remote(), err == nil) }() + size := o.Size() thisOffset := offset if thisOffset < 0 { - thisOffset += o.Size() + thisOffset += size } + // size remaining is now reduced by thisOffset + size -= thisOffset var options []OpenOption if thisOffset > 0 { options = append(options, &SeekOption{Offset: thisOffset}) @@ -1278,8 +1281,12 @@ func Cat(f Fs, w io.Writer, offset, count int64) error { } if count >= 0 { in = &readCloser{Reader: &io.LimitedReader{R: in, N: count}, Closer: in} + // reduce remaining size to count + if size > count { + size = count + } } - in = NewAccountWithBuffer(in, o) // account and buffer the transfer + in = NewAccountSizeNameWithBuffer(in, size, o.Remote()) // account and buffer the transfer defer func() { err = in.Close() if err != nil {