Skip to content

Commit 898c7e4

Browse files
authored
Merge pull request #133 from KonstantinBYvitebsk/speedup-getting-list-of-files-by-hash-tables
Enhance fs_handles_db with hash-table for faster file lookup
2 parents a25525a + 17c7fec commit 898c7e4

4 files changed

Lines changed: 277 additions & 143 deletions

File tree

inc/fs_handles_db.h

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -37,43 +37,62 @@ typedef int64_t mtp_offset;
3737

3838
struct fs_entry
3939
{
40-
uint32_t handle;
41-
uint32_t parent;
42-
uint32_t storage_id;
43-
char * name;
44-
uint32_t flags;
45-
mtp_size size;
46-
uint32_t date;
47-
48-
int file_descriptor;
49-
int watch_descriptor;
50-
51-
fs_entry * next;
40+
uint32_t handle;
41+
uint32_t parent;
42+
uint32_t storage_id;
43+
char * name;
44+
uint32_t flags;
45+
mtp_size size;
46+
uint32_t date;
47+
48+
int file_descriptor;
49+
int watch_descriptor;
50+
51+
fs_entry * next;
5252
};
5353

5454
#define ENTRY_IS_DIR 0x00000001
5555
#define ENTRY_IS_DELETED 0x00000002
5656

5757
#define _DEF_FS_HANDLES_ 1
58+
#define HASH_TABLE_SIZE 1024 // Configurable table size
5859

59-
typedef struct fs_handles_db_
60-
{
61-
fs_entry * entry_list;
62-
uint32_t next_handle;
60+
typedef struct hash_node {
61+
fs_entry **entries;
62+
uint32_t size;
63+
uint32_t capacity;
64+
} hash_node;
65+
66+
#define POOL_BLOCK_SIZE 1024
67+
68+
typedef struct fs_entry_pool_block {
69+
fs_entry entries[POOL_BLOCK_SIZE];
70+
struct fs_entry_pool_block *next;
71+
} fs_entry_pool_block;
72+
73+
typedef struct fs_handles_db_ {
74+
fs_entry * entry_list;
75+
hash_node hash_table_by_name[HASH_TABLE_SIZE]; // Hash table by file name for performance improvement
76+
hash_node hash_table_by_handle[HASH_TABLE_SIZE]; // Hash table by file handle for performance improvement
77+
78+
uint32_t next_handle;
79+
80+
fs_entry *search_entry;
81+
uint32_t handle_search;
82+
uint32_t storage_search;
6383

64-
fs_entry * search_entry;
65-
uint32_t handle_search;
66-
uint32_t storage_search;
84+
void *mtp_ctx;
6785

68-
void * mtp_ctx;
69-
}fs_handles_db;
86+
fs_entry_pool_block *pool_head; // Memory pool for fs_entry allocation to improve memory handling performance
87+
uint32_t pool_free_count;
88+
} fs_handles_db;
7089

7190

7291
typedef struct filefoundinfo_
7392
{
74-
int isdirectory;
75-
char filename[FS_HANDLE_MAX_FILENAME_SIZE + 1];
76-
mtp_size size;
93+
int isdirectory;
94+
char filename[FS_HANDLE_MAX_FILENAME_SIZE + 1];
95+
mtp_size size;
7796
}filefoundinfo;
7897

7998

@@ -100,4 +119,4 @@ int fs_remove_tree( char *folder );
100119

101120
int fs_entry_stat(char *path, filefoundinfo* fileinfo);
102121

103-
#endif
122+
#endif

inc/hash_utils.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef HASH_UTILS_H
2+
#define HASH_UTILS_H
3+
4+
#include "fs_handles_db.h"
5+
6+
int init_hash_node(hash_node *node);
7+
int expand_hash_node(hash_node *node);
8+
uint32_t hash_function_name(const char *name);
9+
uint32_t hash_function_handle(uint32_t handle);
10+
int allocate_pool_block(fs_handles_db *db);
11+
void insert_entry_generic(hash_node *node, fs_entry* entry);
12+
void insert_entry(fs_handles_db *db, fs_entry *entry);
13+
fs_entry *find_entry(fs_handles_db *db, const char *name, uint32_t parent, uint32_t storage_id);
14+
15+
#endif // HASH_UTILS_H

0 commit comments

Comments
 (0)