[英]Programmer thought process: determining a maximum number of bytes to read when using ReadFile with the Windows API
I need to call the ReadFile function of the Windows API: 我需要调用Windows API的ReadFile函数:
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]
nNumberOfBytesToRead [输入]
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. 这需要深入了解Windows和您的硬件。 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.
现代操作系统将始终使用页面缓存,并且对文件的任何后续访问都将与访问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. 编辑:我的经验与Yam Marcovic和其他人的建议不符。 Caching files and chunking reads to ideal sizes is exactly the thing OS is there to do.
将文件缓存和分块读取到理想大小正是OS要做的事情。 Do not presume to outsmart it and read just what you need.
不要以为是智商,只能阅读所需内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.