From e749bc58f4e54833fc860c682040f465794d377d Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Thu, 28 Jul 2022 17:35:14 +0100 Subject: [PATCH] vfs: reduce memory use by embedding sync.Cond --- vfs/read.go | 6 +++--- vfs/vfscache/cache.go | 4 ++-- vfs/vfscache/item.go | 4 ++-- vfs/write.go | 6 +++--- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/vfs/read.go b/vfs/read.go index 88bcabfce..4a5658ead 100644 --- a/vfs/read.go +++ b/vfs/read.go @@ -20,7 +20,7 @@ type ReadFileHandle struct { baseHandle done func(ctx context.Context, err error) mu sync.Mutex - cond *sync.Cond // cond lock for out of sequence reads + cond sync.Cond // cond lock for out of sequence reads closed bool // set if handle has been closed r *accounting.Account readCalled bool // set if read has been called @@ -63,7 +63,7 @@ func newReadFileHandle(f *File) (*ReadFileHandle, error) { size: nonNegative(o.Size()), sizeUnknown: o.Size() < 0, } - fh.cond = sync.NewCond(&fh.mu) + fh.cond = sync.Cond{L: &fh.mu} return fh, nil } @@ -267,7 +267,7 @@ func (fh *ReadFileHandle) readAt(p []byte, off int64) (n int, err error) { maxBuf = len(p) } if gap := off - fh.offset; gap > 0 && gap < int64(8*maxBuf) { - waitSequential("read", fh.remote, fh.cond, fh.file.VFS().Opt.ReadWait, &fh.offset, off) + waitSequential("read", fh.remote, &fh.cond, fh.file.VFS().Opt.ReadWait, &fh.offset, off) } doSeek := off != fh.offset if doSeek && fh.noSeek { diff --git a/vfs/vfscache/cache.go b/vfs/vfscache/cache.go index 9c7feb646..8e5209a79 100644 --- a/vfs/vfscache/cache.go +++ b/vfs/vfscache/cache.go @@ -52,7 +52,7 @@ type Cache struct { avFn AddVirtualFn // if set, can be called to add dir entries mu sync.Mutex // protects the following variables - cond *sync.Cond // cond lock for synchronous cache cleaning + cond sync.Cond // cond lock for synchronous cache cleaning item map[string]*Item // files/directories in the cache errItems map[string]error // items in error state used int64 // total size of files in the cache @@ -139,7 +139,7 @@ func New(ctx context.Context, fremote fs.Fs, opt *vfscommon.Options, avFn AddVir // Create a channel for cleaner to be kicked upon out of space con c.kick = make(chan struct{}, 1) - c.cond = sync.NewCond(&c.mu) + c.cond = sync.Cond{L: &c.mu} go c.cleaner(ctx) diff --git a/vfs/vfscache/item.go b/vfs/vfscache/item.go index 2c8145870..883bc9bde 100644 --- a/vfs/vfscache/item.go +++ b/vfs/vfscache/item.go @@ -57,7 +57,7 @@ type Item struct { // read only c *Cache // cache this is part of mu sync.Mutex // protect the variables - cond *sync.Cond // synchronize with cache cleaner + cond sync.Cond // synchronize with cache cleaner name string // name in the VFS opens int // number of times file is open downloaders *downloaders.Downloaders // a record of the downloaders in action - may be nil @@ -138,7 +138,7 @@ func newItem(c *Cache, name string) (item *Item) { ATime: now, }, } - item.cond = sync.NewCond(&item.mu) + item.cond = sync.Cond{L: &item.mu} // check the cache file exists osPath := c.toOSPath(name) fi, statErr := os.Stat(osPath) diff --git a/vfs/write.go b/vfs/write.go index 24595e041..25fa4fbf6 100644 --- a/vfs/write.go +++ b/vfs/write.go @@ -15,7 +15,7 @@ import ( type WriteFileHandle struct { baseHandle mu sync.Mutex - cond *sync.Cond // cond lock for out of sequence writes + cond sync.Cond // cond lock for out of sequence writes closed bool // set if handle has been closed remote string pipeWriter *io.PipeWriter @@ -43,7 +43,7 @@ func newWriteFileHandle(d *Dir, f *File, remote string, flags int) (*WriteFileHa result: make(chan error, 1), file: f, } - fh.cond = sync.NewCond(&fh.mu) + fh.cond = sync.Cond{L: &fh.mu} fh.file.addWriter(fh) return fh, nil } @@ -130,7 +130,7 @@ func (fh *WriteFileHandle) writeAt(p []byte, off int64) (n int, err error) { return 0, ECLOSED } if fh.offset != off { - waitSequential("write", fh.remote, fh.cond, fh.file.VFS().Opt.WriteWait, &fh.offset, off) + waitSequential("write", fh.remote, &fh.cond, fh.file.VFS().Opt.WriteWait, &fh.offset, off) } if fh.offset != off { fs.Errorf(fh.remote, "WriteFileHandle.Write: can't seek in file without --vfs-cache-mode >= writes")