Question

  1. CreateFileMapping and MapViewOfFile, and then we do I/O with a function like memcpy.
  2. Just use Read/WriteFile.

Is the first one faster than second? I don't understand that.

Why is it faster?
If we use a section object, then we can get more cache benefits from VMM or Cache Manager?

Was it helpful?

Solution

File memory mapping is faster when page out occurs, as the file itself is used as paging storage.

If the memory in the memory mapped file is not changed there is no need to flush the the page to the paging file as the data is in the file already and windows can reread the page from disk. .EXE and .DLL files are loaded using this mechanism and thus are their own page storage.

If the memory in the memory mapped file is written, then page out is the same as if the paging file had been used. Faster possibly as the same place on disk is used (subject to NTFS optimisations).

The plain APIs consume page file backed memory to hold the contents of the file while in memory.

On slightly different perspective both APIs are optimised as memory mapped files are may actually be used the Read/Write File APIs under the hood so you get a micro-optimisation as you're using a lower abstraction

Both mechanisms will employ the VMM/Cache manager.

OTHER TIPS

Use of ReadFile/WriteFile involves several extra memory block copying operations, so it will be slower, than use of MMFs. Another question is how much slower it will be - this is what you need to measure yourself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top