Richard Kettlewell <invalid@invalid.invalid> writes:
Steve Keller <keller@no.invalid> writes:
AFAIU, reading files using mmap(2) has some performance benefits
compared to read(2). If a number of proecesses read the same file and
each process mmap()s the file into its address space to read it, then
only one copy of the file is in memory. OTOH, if the processes malloc
some memory and use read() to fill it with file data, the memory is
not shared, because (1) it will be aligned differently in these
processes and (2) each process writes to the memory causing a private
copy to be created.
So I think one should prefer mmap() to access files,
Profile first; historically at least mmap was not reliably faster than >>read/write. Fiddling with pages tables can be quite expensive.
Yeah, though over time, memory closer to the CPU (cache, memory, page
tables) has become much faster and CPU became faster more quickly.
Storage, however, was lacking.
AFAIU, reading files using mmap(2) has some performance benefits
compared to read(2).
If a number of proecesses read the same file and
each process mmap()s the file into its address space to read it, then
only one copy of the file is in memory. OTOH, if the processes malloc
some memory and use read() to fill it with file data, the memory is
not shared, because (1) it will be aligned differently in these
processes and (2) each process writes to the memory causing a private
copy to be created.
So I think one should prefer mmap() to access files, but how can
errors be handled portably, then? On file I/O errors I get an error
return code from read() (e.g. EIO), but with mmap() I typically get a SIGSEGV. How should I handle this?
Steve Keller <keller@no.invalid> writes:
AFAIU, reading files using mmap(2) has some performance benefits
compared to read(2). If a number of proecesses read the same file and
each process mmap()s the file into its address space to read it, then
only one copy of the file is in memory. OTOH, if the processes malloc
some memory and use read() to fill it with file data, the memory is
not shared, because (1) it will be aligned differently in these
processes and (2) each process writes to the memory causing a private
copy to be created.
So I think one should prefer mmap() to access files,
Profile first; historically at least mmap was not reliably faster than read/write. Fiddling with pages tables can be quite expensive.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 307 |
Nodes: | 16 (2 / 14) |
Uptime: | 96:50:57 |
Calls: | 6,850 |
Calls today: | 1 |
Files: | 12,352 |
Messages: | 5,414,948 |