I need to call the ReadFile function of the Windows API:
BOOL WINAPI ReadFile(
_In_ HANDLE hFile,
_Out_ LPVOID lpBuffer,
_In_ DWORD nNumberOfBytesToRead,
_Out_opt_ LPDWORD lpNumberOfBytesRead,
_Inout_opt_ LPOVERLAPPED lpOverlapped
);
The argument I'm interested in is the 3rd one:
nNumberOfBytesToRead [in]
The maximum number of bytes to be read.
I'm not interested so much in the "magic number" to put there but the process a seasoned programmer takes to determine the number to put there, preferably in numbered steps.
Also keep in mind I am writing my program in assembler so I'm more interested in the thought process from that perspective.
This requires plenty of insight into both Windows and your hardware. But, in general, here are some possible directions:
In general, you'd probably want to play around until you get something that works well enough.
That paremeter is there only to protect you from buffer overflow, so you of course must enter size of the buffer you allocated for this purpose. Other than that you should only read as many bytes as you are interested in this exact time. Modern OS will always use pagecache and any following access to the file will be as fast as accessing RAM. You can also force the OS to cache the file beforehand if you need it whole.
Edit: My experience is against what Yam Marcovic and others recommend. Caching files and chunking reads to ideal sizes is exactly the thing OS is there to do. Do not presume to outsmart it and read just what you need.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With