1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 17:58:12 +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:
Andreas Kling 2018-10-25 12:35:49 +02:00
parent 82bbfa8496
commit fdc782c1d1
4 changed files with 36 additions and 3 deletions

View file

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

View file

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

View file

@ -1,5 +1,11 @@
#include "DiskBackedFileSystem.h" #include "DiskBackedFileSystem.h"
#ifdef SERENITY
#include "i386.h"
#else
typedef int InterruptDisabler;
#endif
//#define DBFS_DEBUG //#define DBFS_DEBUG
DiskBackedFileSystem::DiskBackedFileSystem(RetainPtr<DiskDevice>&& device) DiskBackedFileSystem::DiskBackedFileSystem(RetainPtr<DiskDevice>&& device)
@ -36,11 +42,26 @@ ByteBuffer DiskBackedFileSystem::readBlock(unsigned index) const
#ifdef DBFS_DEBUG #ifdef DBFS_DEBUG
kprintf("DiskBackedFileSystem::readBlock %u\n", index); kprintf("DiskBackedFileSystem::readBlock %u\n", index);
#endif #endif
{
InterruptDisabler disabler;
auto it = m_blockCache.find(index);
if (it != m_blockCache.end()) {
return (*it).value;
}
}
auto buffer = ByteBuffer::createUninitialized(blockSize()); auto buffer = ByteBuffer::createUninitialized(blockSize());
DiskOffset baseOffset = static_cast<DiskOffset>(index) * static_cast<DiskOffset>(blockSize()); DiskOffset baseOffset = static_cast<DiskOffset>(index) * static_cast<DiskOffset>(blockSize());
auto* bufferPointer = buffer.pointer(); auto* bufferPointer = buffer.pointer();
device().read(baseOffset, blockSize(), bufferPointer); device().read(baseOffset, blockSize(), bufferPointer);
ASSERT(buffer.size() == blockSize()); ASSERT(buffer.size() == blockSize());
{
InterruptDisabler disabler;
if (m_blockCache.size() >= 32)
m_blockCache.removeOneRandomly();
m_blockCache.set(index, buffer);
}
return buffer; return buffer;
} }
@ -74,5 +95,6 @@ void DiskBackedFileSystem::setBlockSize(unsigned blockSize)
void DiskBackedFileSystem::invalidateCaches() void DiskBackedFileSystem::invalidateCaches()
{ {
// FIXME: Implement block cache. InterruptDisabler disabler;
m_blockCache.clear();
} }

View file

@ -2,6 +2,7 @@
#include "FileSystem.h" #include "FileSystem.h"
#include <AK/ByteBuffer.h> #include <AK/ByteBuffer.h>
#include <AK/HashMap.h>
class DiskBackedFileSystem : public FileSystem { class DiskBackedFileSystem : public FileSystem {
public: public:
@ -27,4 +28,5 @@ protected:
private: private:
unsigned m_blockSize { 0 }; unsigned m_blockSize { 0 };
RetainPtr<DiskDevice> m_device; RetainPtr<DiskDevice> m_device;
mutable HashMap<unsigned, ByteBuffer> m_blockCache;
}; };