4 ms·
Note, be very careful with "mmap is the fastest way to do IO". That's not true 90% of the time. mmap is the fastest way to do IO when the OS already has file co
by tarasglek 13y ago
Note, be very careful with "mmap is the fastest way to do IO". That's not true 90% of the time.
mmap is the fastest way to do IO when the OS already has file contents in memory and it's more than a few pages so setup of mmap() call is cheaper than memcopying data from kernel space.
However mmap is terrible for cold IO. The problem is that mmap is IO-by-sideeffect-of-pagefaults. One has to use madvise() very carefully to make sure that the OS reads useful parts of a file during a pagefault. mmap style of IO is also susceptible to access-pattern bugs, eg even the Linux toolchain gets it deadly wrong and ends up doing IO backwards: https://blog.mozilla.org/tglek/2010/05/27/startup-backward-constructors/ https://blog.mozilla.org/tglek/2010/05/27/startup-backward-c....
mmap is even worse on Windows. Windows has no madvise(), OSX equivalent is pathetic. OSX and Windows read mmaped files in bizzarely small chunks, etc.
mmap has the worst error-handling semantics of all IO methods. Linux gives you a super-hard-to-wrap SIGBUS, Windows forces use of slightly-less-horrific SEH http://msdn.microsoft.com/en-us/library/windows/desktop/aa366801%28v=vs.85%29.aspx http://msdn.microsoft.com/en-us/library/windows/desktop/aa36...