77#include  <unistd.h>      
88#include  <dirent.h>      
99#include  "../headers/request.h" 
10+ #define  BATCH_SIZE  64
1011
12+ static  int  pending_in_batch  =  0 ;
1113
12- void  submit_open_request (const  char  * path , struct  io_uring  * ring , int  * inflight_ops ) {
14+ void  flush_batch (struct  io_uring  * ring ){
15+     if  (pending_in_batch > 0 )
16+     {
17+         int  ret  =  io_uring_submit (ring );
18+         if  (ret < 0 )
19+         {
20+            fprintf (stderr , "Error in io_uring_submit: %s\n" , strerror (- ret ));
21+         }
22+         pending_in_batch  =  0 ;
23+     }
24+     
25+ }
26+ 
27+ void  submit_open_request (const  char  * path , struct  io_uring  * ring , int  * inflight_ops ,int  force_flush ) {
1328    Request  * req  =  malloc (sizeof (Request ));
1429    if  (!req ) {
1530        perror ("malloc request" );
@@ -29,6 +44,14 @@ void submit_open_request(const char *path, struct io_uring *ring, int *inflight_
2944    io_uring_sqe_set_data (sqe , req );
3045
3146    (* inflight_ops )++ ;
47+     pending_in_batch ++ ;
48+ 
49+     if  (pending_in_batch >=BATCH_SIZE  ||  force_flush )
50+     {
51+         flush_batch (ring );
52+         //pending_in_batch=0; 
53+     }
54+     
3255}
3356
3457
@@ -67,7 +90,7 @@ void handle_completion(struct io_uring_cqe *cqe, const char *search_term, struct
6790        // Using d_type is much faster than calling stat() for every entry. 
6891        if  (entry -> d_type  ==  DT_DIR ) {
6992            // RECURSIVE STEP: If it's a directory, submit a new async 'openat' request. 
70-             submit_open_request (full_path , ring , inflight_ops );
93+             submit_open_request (full_path , ring , inflight_ops , 0 );
7194        } else  if  (entry -> d_type  ==  DT_REG ) {
7295            // It's a regular file. Check if its name matches the search term. 
7396            if  (strstr (entry -> d_name , search_term ) !=  NULL ) {
@@ -76,6 +99,11 @@ void handle_completion(struct io_uring_cqe *cqe, const char *search_term, struct
7699        }
77100        // Note: We are ignoring DT_UNKNOWN, symlinks, etc. for simplicity. 
78101    }
102+     if  (pending_in_batch > 0 )
103+     {
104+         flush_batch (ring );
105+     }
106+     
79107
80108    // Clean up resources for the directory we just finished scanning. 
81109    closedir (dir_stream ); // This also closes the underlying dir_fd. 
0 commit comments