there are a bunch of blogs saying mmap is faster than traditional file io. One main reason is mmap reduce one copy between user and kernel space. This is where I quite don't understand.
Suppose I have a byte array char* bytes in user space. I want to write bytes to a file called some-file.txt.
- traditional file write: copy bytes to kernel, then kernel write the bytes to file
- mmap write: copy bytes to the memory location where mmap returns to you, then kernel flush the memory back to disk at some point in the future using paging mechanism.
Both are two copies.
The only advantage I'm sure is that mmap write reduce system calls since each write just manipulate memory byte array, but every file write involves one system call which has many context switches.
I've search many blogs but none explains much about this.