trueKylotan said:a light breeze said:So, what do you think would be a good use-case for memory-mapped files that cannot be accomplished with traditional file i/o? I can think of only one, and it's such a niche case that it might as well not exist: passing the contents of a file to an API call, where the file is too big too fit into physical memory + swap space.
I'd say it's the other way around - what's a good use case for having to open a file, check its length, choose how much memory to allocate, read bit by bit, etc - vs. simply mapping the file and letting the OS manage mapping the relevant parts into memory? It seems like this is a perfect situation where the OS can usually do a better job than you in most cases, leaving the file I/O API for very specific optimisations.
That line of reasoning works may work well on most operating systems, but on Windows you will suffer a significant performance penalty - hence this thread. Just because the OS can do a better job doesn't mean it will.
That leaves convenience as a reason for choosing memory-mapped files despite the performance penalty, but for small files the convenience is in the form of not having to write a single five-line reusable function that you just need to write once. And for large (multi-gigabyte) files, the performance penalty becomes too big to ignore.