Does Open file descriptors on windows incur a performance cost

89 Views Asked by At

I am building a high performance application that streams data in from ~1000 different sources of on disk data (aka files) on demand on windows.

To minimize latency, I was looking into keeping all file descriptors open for the entire run time of the program. I was thinking of doing something like:

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif

#include <windows.h>

#define MAX_NUM_HANDLES 1024

int main()
{
    //...

    HANDLE openHandles[MAX_NUM_HANDLES];
    unsigned long long fileSizes[MAX_NUM_HANDLES];
    for( int i = 0; i < MAX_NUM_HANDLES; ++i )
    {
        openHandles[i] = CreateFileA(filenames[i], GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED ,NULL);
        if( openHandles[i] != INVALID_HANDLE_VALUE )
        {
            LARGE_INTEGER winFileSize;
            GetFileSizeEx(openHandles[i],&winFileSize);
            fileSizes[i] = (unsigned long long)winFileSize.QuadPart;
        }
        else
        {
            fileSizes[i] = 0;
        }
    }

    bool bRunning = true;
    while(bRunning)
    {
         //doing stuff with the handle
    }
    return 0;
}

but my question is does keeping open handles on windows incur a CPU performance penalty? I understand it consumes RAM space, but does it steal CPU time? Like does windows have background processes that scale in CPU runtime with extra open handles such that it would take processor time from me? Even if I measure it on my windows 11 machine I assume different versions of windows behave differently.

Any help or insights into the inner workings would be greatly appreciated, as I am trying to reduce processor impact as much as possible (I am configuring windows as well to reduce as many kernel processes as possible to give me more cpu time).

0

There are 0 best solutions below