diff options
Diffstat (limited to 'lib/diskio')
-rw-r--r-- | lib/diskio/file_blockbuf.go | 18 |
1 files changed, 10 insertions, 8 deletions
diff --git a/lib/diskio/file_blockbuf.go b/lib/diskio/file_blockbuf.go index 77b823c..15ae13b 100644 --- a/lib/diskio/file_blockbuf.go +++ b/lib/diskio/file_blockbuf.go @@ -1,4 +1,4 @@ -// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com> +// Copyright (C) 2022-2023 Luke Shumaker <lukeshu@lukeshu.com> // // SPDX-License-Identifier: GPL-2.0-or-later @@ -19,16 +19,18 @@ type bufferedFile[A ~int64] struct { inner File[A] mu sync.RWMutex blockSize A - blockCache *containers.LRUCache[A, bufferedBlock] + blockCache containers.ARCache[A, bufferedBlock] } var _ File[assertAddr] = (*bufferedFile[assertAddr])(nil) func NewBufferedFile[A ~int64](file File[A], blockSize A, cacheSize int) *bufferedFile[A] { return &bufferedFile[A]{ - inner: file, - blockSize: blockSize, - blockCache: containers.NewLRUCache[A, bufferedBlock](cacheSize), + inner: file, + blockSize: blockSize, + blockCache: containers.ARCache[A, bufferedBlock]{ + MaxLen: cacheSize, + }, } } @@ -53,13 +55,13 @@ func (bf *bufferedFile[A]) maybeShortReadAt(dat []byte, off A) (n int, err error defer bf.mu.RUnlock() offsetWithinBlock := off % bf.blockSize blockOffset := off - offsetWithinBlock - cachedBlock, ok := bf.blockCache.Get(blockOffset) + cachedBlock, ok := bf.blockCache.Load(blockOffset) if !ok { cachedBlock.Dat = make([]byte, bf.blockSize) n, err := bf.inner.ReadAt(cachedBlock.Dat, blockOffset) cachedBlock.Dat = cachedBlock.Dat[:n] cachedBlock.Err = err - bf.blockCache.Add(blockOffset, cachedBlock) + bf.blockCache.Store(blockOffset, cachedBlock) } n = copy(dat, cachedBlock.Dat[offsetWithinBlock:]) if n < len(dat) { @@ -77,7 +79,7 @@ func (bf *bufferedFile[A]) WriteAt(dat []byte, off A) (n int, err error) { // Cache invalidation for blockOffset := off - (off % bf.blockSize); blockOffset < off+A(n); blockOffset += bf.blockSize { - bf.blockCache.Remove(blockOffset) + bf.blockCache.Delete(blockOffset) } return |