views:

1144

answers:

4

What are the drawbacks (if any) of using memory mapped file to read (regular sized files) over doing the same using CreateFile ReadFile combination?

A: 

You'll need more complex code for establishing the file mapping than for just opening and reading. File mapping is intended for random access to a section of file. If you don't need that, just don't bother with file mapping.

Also if ever need to port your code onto another platform you'll do it much easier and faster if you don't use file mapping.

sharptooth
+3  A: 

Windows actually uses file mapping for ReadFile/WriteFile due to how Cache Manager is designed. So they are not much different and you should use whichever fits your purpose best.

File mapping has these drawbacks:

  • You can't grow a file
  • You can't map more than 2GB (or 3GB with /3GB) on 32-bit systems so you'll need extra handling

and these advantages:

  • It doesn't need additional memory allocation to hold file contents
  • It acts as a big buffer so some operations might be easier to code on file mapping. It's sometimes cool to "strstr()" on a 150MB text file :)
ssg
You're correct that the NT cache manager implements caching using memory mapped files, however it hides the error propogation semantics from the application. In addition, the cache manager will do predictive read-ahead and other optimizations that aren't automatically there for user mode apps.
Larry Osterman
+3  A: 

With ReadFile/WriteFile you have deterministic error handling semantics. When you use memory mapped files, errors are returned by throwing an exception.

In addition, if the memory mapped file has to hit the disk (or even worse, the network) your memory read may take several seconds (or even minutes) to complete. Depending on your application, this can cause unexpected stalls.

If you use ReadFile/WriteFile you can use asynchronous variants of the API to allow you to control this behavior.

You also have more deterministic performance if you use ReadFile, especially if your I/O pattern is predictable - memory mapped I/O is often random while as ReadFile is almost always serial (since ReadFile reads at the current file position and advances the current file position).

Larry Osterman
+1  A: 

A big advantage of file mapping is that it doesn't influence system cache. If your application does excessive I/O by means of ReadFile, your system cache will grow, consuming more and more physical memory. If your OS is 32 bit and you have much more than 1GB memory, than you're lucky, since on 32 bit Windows the size of system cache is limited by 1GB. Otherwise system cache will consume all available physical memory and the memory manager will soon start purging pages of other processes to disk, intensifying disk operations instead of actually lessen them. The effect is especially noticeable on 64 bit Windows, where the cache size is limited only by available physical memory. File mapping on the other hand doesn't lead to overgrowing of system cache and at the same time doesn't degrade the performance.

noxmetus