Copy into a buffer or establish a mapping
Read transfers bytes from a file descriptor into a caller-provided buffer. Mmap establishes an address-space mapping, after which ordinary memory accesses use the mapped contents. They expose different interfaces and different places for failures to appear. The distinction is not disk versus memory: ordinary file access through either interface can involve the page cache. This comparison starts with a normal local file. Device mappings, direct I/O and network filesystems add contracts that need separate examination. Identify the actual mapping and file type before assuming that a statement about ordinary buffered files also applies to a device or a specialized I/O configuration.
| Aspect | read | mmap |
|---|---|---|
| Interface | Read into caller buffer | Map into virtual address space |
| Failure observation | Inspect result and error | Later access can also fail |
| File changes | Define concurrent-change contract | Define mapping mode and lifetime |
Compare repeated access to the same region
Suppose an application repeatedly inspects part of a 1 MiB file. With read, it can retain and reuse a buffer. With mmap, it establishes a mapping and accesses selected offsets, but first touches can require fault handling and obtaining pages. Both designs can reuse data after initial work, so repetition alone does not prove that mmap is faster. Measure mapping creation, first access, repeated access and teardown as appropriate to the workload. Keep bytes examined and access order aligned. A benchmark that excludes mapping setup on one side but includes repeated buffer allocation on the other is comparing different lifecycles rather than isolating the interface trade-off.
Avoided copying is not zero total cost
Mmap can avoid copying bytes from the page cache into an additional user buffer as an ordinary read would. Mapping management, page tables, TLB activity, faults and obtaining the contents still require work. Repeatedly mapping and unmapping small regions can make management overhead important. Writes to a private mapping also involve copy-on-write semantics and do not simply become persistent changes to the underlying file. When encountering the term zero-copy, identify which particular copy is avoided. It does not mean that every transfer, memory reference, allocation or wait throughout the system has disappeared. Compare the complete data path and lifecycle rather than treating the label as a performance result.
Handle short reads and later access failures
Read can return fewer bytes than requested, so distinguish a short successful result, end-of-file and an error. Successful mmap creation does not guarantee that every later access succeeds. If another actor truncates the backing file, accessing a now-invalid mapped region can produce a signal such as SIGBUS on Linux. Checking only the mapping call’s return value would miss that later failure boundary. Define whether concurrent modification is allowed, who controls file size and how failures are handled. Buffer-based reading also needs a policy for concurrent file changes; it simply presents different observations and error paths. Neither interface automatically turns a mutable file into a stable snapshot.
Choose with visibility and persistence in mind
Shared and private mappings expose different write-visibility rules. Changes visible through a shared mapping are not automatically proof that the required storage durability and ordering have completed. Conversely, private mapped writes are not an interface for modifying the original file. For reading, compare locality, region size, reuse and memory pressure. For writing, additionally specify synchronization, crash consistency and file-size ownership. Both choices still require I/O and memory-management reasoning; the responsibilities appear through different interfaces. Select from the application’s consistency, lifetime and failure requirements, then validate performance under those requirements rather than choosing from a general claim that mapping or explicit reading is universally superior.