mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 10:37:45 +00:00
Add a very naive block cache to the DiskBackedFileSystem.
This would be a lot better as an LRU. Right now it's a 32-slot hash table with random eviction.
This commit is contained in:
parent
82bbfa8496
commit
fdc782c1d1
4 changed files with 36 additions and 3 deletions
|
@ -1,5 +1,11 @@
|
|||
#include "DiskBackedFileSystem.h"
|
||||
|
||||
#ifdef SERENITY
|
||||
#include "i386.h"
|
||||
#else
|
||||
typedef int InterruptDisabler;
|
||||
#endif
|
||||
|
||||
//#define DBFS_DEBUG
|
||||
|
||||
DiskBackedFileSystem::DiskBackedFileSystem(RetainPtr<DiskDevice>&& device)
|
||||
|
@ -36,11 +42,26 @@ ByteBuffer DiskBackedFileSystem::readBlock(unsigned index) const
|
|||
#ifdef DBFS_DEBUG
|
||||
kprintf("DiskBackedFileSystem::readBlock %u\n", index);
|
||||
#endif
|
||||
{
|
||||
InterruptDisabler disabler;
|
||||
auto it = m_blockCache.find(index);
|
||||
if (it != m_blockCache.end()) {
|
||||
return (*it).value;
|
||||
}
|
||||
}
|
||||
|
||||
auto buffer = ByteBuffer::createUninitialized(blockSize());
|
||||
DiskOffset baseOffset = static_cast<DiskOffset>(index) * static_cast<DiskOffset>(blockSize());
|
||||
auto* bufferPointer = buffer.pointer();
|
||||
device().read(baseOffset, blockSize(), bufferPointer);
|
||||
ASSERT(buffer.size() == blockSize());
|
||||
|
||||
{
|
||||
InterruptDisabler disabler;
|
||||
if (m_blockCache.size() >= 32)
|
||||
m_blockCache.removeOneRandomly();
|
||||
m_blockCache.set(index, buffer);
|
||||
}
|
||||
return buffer;
|
||||
}
|
||||
|
||||
|
@ -74,5 +95,6 @@ void DiskBackedFileSystem::setBlockSize(unsigned blockSize)
|
|||
|
||||
void DiskBackedFileSystem::invalidateCaches()
|
||||
{
|
||||
// FIXME: Implement block cache.
|
||||
InterruptDisabler disabler;
|
||||
m_blockCache.clear();
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
#include "FileSystem.h"
|
||||
#include <AK/ByteBuffer.h>
|
||||
#include <AK/HashMap.h>
|
||||
|
||||
class DiskBackedFileSystem : public FileSystem {
|
||||
public:
|
||||
|
@ -27,4 +28,5 @@ protected:
|
|||
private:
|
||||
unsigned m_blockSize { 0 };
|
||||
RetainPtr<DiskDevice> m_device;
|
||||
mutable HashMap<unsigned, ByteBuffer> m_blockCache;
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue