From 7af6fa8a3a6796b70532c31b0c746e89d516d7d0 Mon Sep 17 00:00:00 2001 From: Nikhil Tanwar <2002nikhiltanwar@gmail.com> Date: Sat, 27 Apr 2024 16:27:18 +0530 Subject: [PATCH] Cache stat values after first use. When reading ZIMs with a lot of files (for example: mediawiki_en_all), getting their filesize took a lot of time. There were 2 choices: 1. Read filesize while creating the tree - this gave fast subsequent responses (on commands such as ls) but the fuse initialization took a good amount of time. 2. Find filesize when requested by a command (and save it for future requests) - This provides fast initialization but the first read takes time (though, not as much as getting all filesizes - only the requested ones). They are later saved for subsequent requests. I went with option 2. --- src/zimfuse/main.cpp | 6 ++++++ src/zimfuse/tree.h | 3 +++ 2 files changed, 9 insertions(+) diff --git a/src/zimfuse/main.cpp b/src/zimfuse/main.cpp index d61146a7..bad05da0 100644 --- a/src/zimfuse/main.cpp +++ b/src/zimfuse/main.cpp @@ -18,6 +18,11 @@ static const Node* getNode(const std::string& nName) void setStat(struct stat* st, const Node* node) { + Tree* const tree = static_cast(fuse_get_context()->private_data); + if (tree->statCache.count(node->originalPath)) { + *st = tree->statCache[node->originalPath]; + return; + } if (node->isDir) { st->st_mode = S_IFDIR | 0555; st->st_nlink = 1; @@ -30,6 +35,7 @@ void setStat(struct stat* st, const Node* node) .getItem(true) .getSize(); } + tree->statCache[node->originalPath] = *st; } static int zimGetAttr(const char* path, struct stat* st, fuse_file_info* fi) diff --git a/src/zimfuse/tree.h b/src/zimfuse/tree.h index 2c775db9..e45553bf 100644 --- a/src/zimfuse/tree.h +++ b/src/zimfuse/tree.h @@ -3,11 +3,14 @@ #include #include "node.h" +#include #include class Tree { public: + using statStruct = struct stat; + std::unordered_map statCache; Tree(const std::string &path); std::pair attachNode(const std::string& path, Node* parent); std::pair attachFile(const std::string& path, Node* parent, int collisionCount);