3 ms·
That's right. Indeed, if the optimization is implemented properly it should have no observable behavioral difference except for performance (better in some cas
by comex 4y ago
That's right. Indeed, if the optimization is implemented properly it should have no observable behavioral difference except for performance (better in some cases, worse in others) which is not really something POSIX concerns itself with. So POSIX doesn't forbid it.
I can think of one edge case not mentioned by others, though.
One of the main benefits of mmap is that the data can be paged out while not used. But when you try to page the data back in, the attempt may fail, or it may return different data than was originally there. This is particularly likely with network filesystems or external storage. With mmap, these situations result in, respectively, the process receiving SIGBUS, or the contents of memory changing out from under it. The former is usually a crash (unless the program installs a signal handler), and the latter can be worse than a crash, if either program logic or compiler optimizations make assumptions about memory staying consistent when not written to. A program using mmap is opting in to that risk. A program using malloc and read is not.
That said, it may be reasonable to trust that the system’s main disk(s) (however you define that) won’t misbehave this way, and limit the optimization to files mapped from them. After all, if swap is enabled, the same potential risks exist for any memory that’s swapped out. Even without swap, if the root partition starts failing, the system is not going to stay up for long.
Or, the optimization could be enabled for other disks, but with the caveat that the pages would be swapped to the swap file/partition rather than paged out normally.
- xiphias2 4y agoI see, the main disks should be probably defined when mounting (allow memory mapping), but it needs a lot of kernel work to know whether it all is worth it or not. Still, the current problem with mmap is that it's buggy on some operating systems according to https://www.sqlite.org/mmap.html https://www.sqlite.org/mmap.html, so I guess it will cause more frustrations until AGI takes over the world :)
- manwe150 4y agoSounds like an argument for liburing more. That also is supposed to let the kernel share buffers and reduce syscalls, but does come with a mechanism for handling errors without terminating the process