vfs: reduce memory use by embedding sync.Cond

This commit is contained in:
Nick Craig-Wood 2022-07-28 17:35:14 +01:00
parent 821e084f28
commit e749bc58f4
4 changed files with 10 additions and 10 deletions

View File

@ -20,7 +20,7 @@ type ReadFileHandle struct {
baseHandle baseHandle
done func(ctx context.Context, err error) done func(ctx context.Context, err error)
mu sync.Mutex 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 closed bool // set if handle has been closed
r *accounting.Account r *accounting.Account
readCalled bool // set if read has been called readCalled bool // set if read has been called
@ -63,7 +63,7 @@ func newReadFileHandle(f *File) (*ReadFileHandle, error) {
size: nonNegative(o.Size()), size: nonNegative(o.Size()),
sizeUnknown: o.Size() < 0, sizeUnknown: o.Size() < 0,
} }
fh.cond = sync.NewCond(&fh.mu) fh.cond = sync.Cond{L: &fh.mu}
return fh, nil return fh, nil
} }
@ -267,7 +267,7 @@ func (fh *ReadFileHandle) readAt(p []byte, off int64) (n int, err error) {
maxBuf = len(p) maxBuf = len(p)
} }
if gap := off - fh.offset; gap > 0 && gap < int64(8*maxBuf) { 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 doSeek := off != fh.offset
if doSeek && fh.noSeek { if doSeek && fh.noSeek {

View File

@ -52,7 +52,7 @@ type Cache struct {
avFn AddVirtualFn // if set, can be called to add dir entries avFn AddVirtualFn // if set, can be called to add dir entries
mu sync.Mutex // protects the following variables 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 item map[string]*Item // files/directories in the cache
errItems map[string]error // items in error state errItems map[string]error // items in error state
used int64 // total size of files in the cache 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 // Create a channel for cleaner to be kicked upon out of space con
c.kick = make(chan struct{}, 1) c.kick = make(chan struct{}, 1)
c.cond = sync.NewCond(&c.mu) c.cond = sync.Cond{L: &c.mu}
go c.cleaner(ctx) go c.cleaner(ctx)

View File

@ -57,7 +57,7 @@ type Item struct {
// read only // read only
c *Cache // cache this is part of c *Cache // cache this is part of
mu sync.Mutex // protect the variables 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 name string // name in the VFS
opens int // number of times file is open opens int // number of times file is open
downloaders *downloaders.Downloaders // a record of the downloaders in action - may be nil 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, ATime: now,
}, },
} }
item.cond = sync.NewCond(&item.mu) item.cond = sync.Cond{L: &item.mu}
// check the cache file exists // check the cache file exists
osPath := c.toOSPath(name) osPath := c.toOSPath(name)
fi, statErr := os.Stat(osPath) fi, statErr := os.Stat(osPath)

View File

@ -15,7 +15,7 @@ import (
type WriteFileHandle struct { type WriteFileHandle struct {
baseHandle baseHandle
mu sync.Mutex 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 closed bool // set if handle has been closed
remote string remote string
pipeWriter *io.PipeWriter pipeWriter *io.PipeWriter
@ -43,7 +43,7 @@ func newWriteFileHandle(d *Dir, f *File, remote string, flags int) (*WriteFileHa
result: make(chan error, 1), result: make(chan error, 1),
file: f, file: f,
} }
fh.cond = sync.NewCond(&fh.mu) fh.cond = sync.Cond{L: &fh.mu}
fh.file.addWriter(fh) fh.file.addWriter(fh)
return fh, nil return fh, nil
} }
@ -130,7 +130,7 @@ func (fh *WriteFileHandle) writeAt(p []byte, off int64) (n int, err error) {
return 0, ECLOSED return 0, ECLOSED
} }
if fh.offset != off { 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 { if fh.offset != off {
fs.Errorf(fh.remote, "WriteFileHandle.Write: can't seek in file without --vfs-cache-mode >= writes") fs.Errorf(fh.remote, "WriteFileHandle.Write: can't seek in file without --vfs-cache-mode >= writes")