Tuesday, September 21, 2010

FILE_FLAG_NO_BUFFERING and File READ/WRITE Performance

Tip -  FILE_FLAG_NO_BUFFERING FLAG can improve performance for FILE READ/WRITE operations.

Details - CreateFile API supports many flags which can be used to optimize performance for Read/Write operations. Such flags when correctly used will provide good performance compared to Normal Read/Write operations. For example , When opening or creating a file with the CreateFile function, the FILE_FLAG_NO_BUFFERING flag can be specified to disable system caching of data being read from or written to the file. It is seen that for large sized files this provides very good performance because intermediate write to cache is avoided.

An application must meet certain requirements when working with files opened with FILE_FLAG_NO_BUFFERING:
 - File access must begin at byte offsets within the file that are integer multiples of the volume's sector size.

 - File access must be for numbers of bytes that are integer multiples of the volume's sector size. For example, if the sector size is 512 bytes, an application can request reads and writes of 512, 1024, or 2048 bytes, but not of 335, 981, or 7171 bytes.

 - Buffer addresses for read and write operations must be aligned on addresses in memory that are integer multiples of the volume's sector size. One way to     align buffers on integer multiples of the volume sector size is to use VirtualAlloc to allocate the buffers. It allocates memory that is aligned on addresses that are integer multiples of the operating system's memory page size. Because both memory page and volume sector sizes are powers of 2, this memory is also  aligned on addresses that are integer multiples of a volume's sector size. An application can determine a volume's sector size by calling the GetDiskFreeSpace function.

As explained above ,Files opened with FILE_FLAG_NO_BUFFERING need to be written in chunks that are multiples of the sector size. Suppose If we have to write a huge file, but the data size  is not a multiple of the sector size , How can we write the last handful of bytes? 

Answer:- ntdll call NtSetInformationFile allows you to explicitly set the file size.  So you can write a final full sector, and then truncate using that. ( Courtesy www.experts-exchange.com )

Reference -
1. http://msdn.microsoft.com/en-us/library/cc644950(VS.85).aspx
2. http://msdn.microsoft.com/en-us/library/aa363858(VS.85).aspx
3. http://www.experts-exchange.com/Programming/System/Windows__Programming/Q_20548617.html

Posted by - Vijesh Vijay

No comments:

Post a Comment