Using FindFirstFileA() instead of CreateFileA() to get the file size

Since we already call FindFirstFileA() and it returns the size there is
no need to open/close every file to get it's size
This commit is contained in:
2026-03-10 19:54:52 +01:00
parent 1fa306643f
commit aef070192f
5 changed files with 81 additions and 26 deletions

View File

@@ -1,3 +1,4 @@
#include "arena.h"
#include "platform.h"
// ----------------------------- Globals ------------------------------------
@@ -201,16 +202,7 @@ void scan_folder_windows_parallel(const char *base, DirQueue *q) {
platform_get_file_owner(full, fe->owner, sizeof(fe->owner));
LARGE_INTEGER size;
HANDLE hf =
CreateFileA(full, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hf != INVALID_HANDLE_VALUE) {
if (GetFileSizeEx(hf, &size))
fe->size_bytes = (uint64_t)size.QuadPart;
CloseHandle(hf);
}
fe->size_bytes = ((uint64_t)fd.nFileSizeHigh << 32) | fd.nFileSizeLow;
mpmc_push(&g_file_queue, fe);
}
@@ -474,7 +466,7 @@ int main(int argc, char **argv) {
arena_free(&gp_arena, (u8 **)&buf, len);
// Add some extra threads to overlap I/O more aggressively
u8 num_threads = hw_threads * 2;
size_t num_threads = hw_threads * 2;
if (num_threads < 2)
num_threads = 2;
@@ -490,6 +482,10 @@ int main(int argc, char **argv) {
InitializeConditionVariable(&q.cv);
q.active = 0;
for (int i = 0; i < folder_count; ++i) {
dirqueue_push(&q, folders[i]);
}
// starting hash threads
WorkerContext workers[num_threads];
@@ -508,10 +504,6 @@ int main(int argc, char **argv) {
// starting scan threads
HANDLE progress = CreateThread(NULL, 0, progress_thread, NULL, 0, NULL);
for (int i = 0; i < folder_count; ++i) {
dirqueue_push(&q, folders[i]);
}
size_t scan_threads = hw_threads;
if (scan_threads < 2)
scan_threads = 2;
@@ -526,7 +518,9 @@ int main(int argc, char **argv) {
WaitForMultipleObjects((DWORD)scan_threads, scan_tids, TRUE, INFINITE);
mpmc_producers_finished(&g_file_queue, num_threads);
for (size_t i = 0; i < num_threads; i++) {
mpmc_push(&g_file_queue, NULL);
}
atomic_store(&g_scan_done, 1);
@@ -597,7 +591,7 @@ int main(int argc, char **argv) {
double total_mb = (double)total_bytes / (1024.0 * 1024.0);
double avg_mbps = total_mb / total_seconds;
printf("Total: %.2f MB, Average: %.2f MB/s\n", total_mb, avg_mbps);
printf(" Total time : %.2f seconds\n", total_seconds);
printf(" Total time : %.2f seconds\n\n", total_seconds);
return 0;
}