1

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.

Yang Song
  • 11
  • 4
  • Have you read: https://stackoverflow.com/a/9818473/4386427 – Support Ukraine Jan 04 '23 at 10:05
  • also, have your read: https://stackoverflow.com/q/35891525/12688015 – Sekomer Jan 04 '23 at 10:05
  • first link relates to read, not write. second link relates to the best procedure using mmap. Both not answer my question. – Yang Song Jan 04 '23 at 15:12
  • The number of copy is dependent of the target system. AFAIK, there is no general answer. As for the comparison, even if the number of copy is the same, this does not mean the performance are the same. Indeed, for example, AFAIK, on x86-64 Linux systems, one method use an AVX copy while the other use a slow scalar copy. The speed of the copy will likely change on new x86-64 processor since there are new instruction to make copies faster: the processor is now responsible for doing the copy efficiently instead of the software code. There are many other parameters to consider. – Jérôme Richard Jan 06 '23 at 03:23

0 Answers0