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:
Andreas Kling 2018-10-25 12:35:49 +02:00
parent 82bbfa8496
commit fdc782c1d1
Notes: sideshowbarker 2024-07-19 18:38:54 +09:00
4 changed files with 36 additions and 3 deletions

View file

@ -49,9 +49,12 @@ public:
bool isEmpty() const { return m_table.isEmpty(); }
unsigned size() const { return m_table.size(); }
unsigned capacity() const { return m_table.capacity(); }
void clear() { m_table.clear(); }
void set(const K&, const V&);
void set(const K&, V&&);
void remove(const K&);
void removeOneRandomly() { m_table.remove(m_table.begin()); }
typedef HashTable<Entry, EntryTraits> HashTableType;
typedef typename HashTableType::Iterator IteratorType;
@ -77,6 +80,12 @@ void HashMap<K, V>::set(const K& key, V&& value)
m_table.set(Entry{key, move(value)});
}
template<typename K, typename V>
void HashMap<K, V>::set(const K& key, const V& value)
{
m_table.set(Entry{key, value});
}
template<typename K, typename V>
void HashMap<K, V>::remove(const K& key)
{

View file

@ -216,7 +216,7 @@ public:
remove(it);
}
void remove(Iterator&);
void remove(Iterator);
private:
Bucket& lookup(const T&, unsigned* bucketIndex = nullptr);
@ -329,7 +329,7 @@ auto HashTable<T, TraitsForT>::find(const T& value) const -> ConstIterator
}
template<typename T, typename TraitsForT>
void HashTable<T, TraitsForT>::remove(Iterator& it)
void HashTable<T, TraitsForT>::remove(Iterator it)
{
ASSERT(!isEmpty());
m_buckets[it.m_bucketIndex].chain.remove(it.m_bucketIterator);

View file

@ -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();
}

View file

@ -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;
};