[VictoriaFallS] Cache the results of reading inodes.

This reduces the number of reads when starting up the OS by ~30% (32-23
for a basic use case).

In the future we should cache things using a BTree in the VFS but this
is sufficient for now.
This commit is contained in:
Drew Galbraith 2024-01-11 18:29:51 -08:00
parent e7cc98a20c
commit 7b8528ea99
2 changed files with 14 additions and 5 deletions

View File

@ -56,7 +56,7 @@ glcr::ErrorOr<glcr::Vector<DirEntry>> Ext2Driver::ReadDirectory(
dbgln("Reading non directory.");
return glcr::INVALID_ARGUMENT;
}
ASSIGN_OR_RETURN(mmth::OwnedMemoryRegion dir, ReadInode(inode));
ASSIGN_OR_RETURN(mmth::OwnedMemoryRegion dir, ReadInode(inode_number, inode));
glcr::Vector<DirEntry> directory;
@ -90,10 +90,14 @@ glcr::ErrorOr<mmth::OwnedMemoryRegion> Ext2Driver::ReadFile(
dbgln("Reading non file.");
return glcr::INVALID_ARGUMENT;
}
return ReadInode(inode);
return ReadInode(inode_number, inode);
}
glcr::ErrorOr<mmth::OwnedMemoryRegion> Ext2Driver::ReadInode(Inode* inode) {
glcr::ErrorOr<mmth::OwnedMemoryRegion> Ext2Driver::ReadInode(uint64_t inode_num,
Inode* inode) {
if (inode_cache_.Contains(inode_num)) {
return inode_cache_.at(inode_num).Duplicate();
}
// This calculation is cursed.
uint64_t real_block_cnt =
(inode->blocks - 1) / (ext2_reader_->BlockSize() / 512) + 1;
@ -150,5 +154,7 @@ glcr::ErrorOr<mmth::OwnedMemoryRegion> Ext2Driver::ReadInode(Inode* inode) {
}
}
return ext2_reader_->ReadBlocks(blocks_to_read);
ASSIGN_OR_RETURN(auto inode_mem, ext2_reader_->ReadBlocks(blocks_to_read));
RET_ERR(inode_cache_.Insert(glcr::Move(inode_num), inode_mem.Duplicate()));
return inode_mem;
}

View File

@ -1,5 +1,6 @@
#pragma once
#include <glacier/container/hash_map.h>
#include <glacier/memory/move.h>
#include <glacier/memory/unique_ptr.h>
#include <yellowstone/yellowstone.yunq.h>
@ -24,10 +25,12 @@ class Ext2Driver {
private:
glcr::SharedPtr<Ext2BlockReader> ext2_reader_;
glcr::UniquePtr<InodeTable> inode_table_;
glcr::HashMap<uint64_t, mmth::OwnedMemoryRegion> inode_cache_;
Ext2Driver(const glcr::SharedPtr<Ext2BlockReader>& reader,
glcr::UniquePtr<InodeTable> inode_table)
: ext2_reader_(reader), inode_table_(glcr::Move(inode_table)) {}
glcr::ErrorOr<mmth::OwnedMemoryRegion> ReadInode(Inode* inode);
glcr::ErrorOr<mmth::OwnedMemoryRegion> ReadInode(uint64_t inode_num,
Inode* inode);
};