Skip to content

Commit d969bb7

Browse files
committed
Optimize the file iteration
Each `simplefs_extent` structure contains a counter that records the total number of files within that extent. When the counter matches the expected file number, it indicates there are no more files after this index, allowing the iterator to skip directly to the next extension block. This reduces unnecessary scanning and improves traversal efficiency.
1 parent d7e0c9b commit d969bb7

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

dir.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,10 @@ static int simplefs_iterate(struct file *dir, struct dir_context *ctx)
6262
for (; remained_nr_files && ei < SIMPLEFS_MAX_EXTENTS; ei++) {
6363
if (eblock->extents[ei].ee_start == 0)
6464
continue;
65-
65+
int ei_nr = eblock->extents[ei].nr_files;
6666
/* Iterate over blocks in one extent */
67-
for (bi = 0; bi < eblock->extents[ei].ee_len && remained_nr_files;
67+
for (bi = 0;
68+
bi < eblock->extents[ei].ee_len && remained_nr_files && ei_nr;
6869
bi++) {
6970
bh2 = sb_bread(sb, eblock->extents[ei].ee_start + bi);
7071
if (!bh2) {
@@ -86,6 +87,7 @@ static int simplefs_iterate(struct file *dir, struct dir_context *ctx)
8687
offset--;
8788
} else {
8889
remained_nr_files--;
90+
ei_nr--;
8991
if (!dir_emit(ctx, dblock->files[fi].filename,
9092
SIMPLEFS_FILENAME_LEN,
9193
dblock->files[fi].inode, DT_UNKNOWN)) {

0 commit comments

Comments
 (0)