Skip to content

Commit

Permalink
Cache stat values after first use.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
juuz0 committed Apr 29, 2024
1 parent 93ab559 commit 7af6fa8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/zimfuse/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Tree*>(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;
Expand All @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions src/zimfuse/tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@

#include <zim/archive.h>
#include "node.h"
#include <sys/stat.h>
#include <unordered_map>

class Tree
{
public:
using statStruct = struct stat;
std::unordered_map<std::string, statStruct> statCache;
Tree(const std::string &path);
std::pair<Node*, bool> attachNode(const std::string& path, Node* parent);
std::pair<Node*, bool> attachFile(const std::string& path, Node* parent, int collisionCount);
Expand Down

0 comments on commit 7af6fa8

Please sign in to comment.