Skip to content

Commit 7af6fa8

Browse files
committed
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.
1 parent 93ab559 commit 7af6fa8

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

src/zimfuse/main.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ static const Node* getNode(const std::string& nName)
1818

1919
void setStat(struct stat* st, const Node* node)
2020
{
21+
Tree* const tree = static_cast<Tree*>(fuse_get_context()->private_data);
22+
if (tree->statCache.count(node->originalPath)) {
23+
*st = tree->statCache[node->originalPath];
24+
return;
25+
}
2126
if (node->isDir) {
2227
st->st_mode = S_IFDIR | 0555;
2328
st->st_nlink = 1;
@@ -30,6 +35,7 @@ void setStat(struct stat* st, const Node* node)
3035
.getItem(true)
3136
.getSize();
3237
}
38+
tree->statCache[node->originalPath] = *st;
3339
}
3440

3541
static int zimGetAttr(const char* path, struct stat* st, fuse_file_info* fi)

src/zimfuse/tree.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33

44
#include <zim/archive.h>
55
#include "node.h"
6+
#include <sys/stat.h>
67
#include <unordered_map>
78

89
class Tree
910
{
1011
public:
12+
using statStruct = struct stat;
13+
std::unordered_map<std::string, statStruct> statCache;
1114
Tree(const std::string &path);
1215
std::pair<Node*, bool> attachNode(const std::string& path, Node* parent);
1316
std::pair<Node*, bool> attachFile(const std::string& path, Node* parent, int collisionCount);

0 commit comments

Comments
 (0)