News for hackers. Stuff that matters.
Re:How memory maps (mmap) deliver faster file access in Go by gustavpaul on Thursday October 23, 2025 @08:06PM (#45689129)
The MmapReader is not copying the requested byte range into the buf argument, so if ever the underlying file descriptor is closed (or the file truncated out of band) any subsequent slice access will throw SIGBUS, which is really unpleasant.

It also means the latency due to pagefaults is shifted from inside mmapReader.ReadRecord() (where it would be expected) to wherever in the application the bytes are first accessed, leading to spooky unpreditactable latency spikes in what are otherwise pure functions. That inevitably leads to wild arguments about how bad GC stalls are :-)

An apples to apples comparison should be copying the bytes from the mmap buffer and returning the resulting slice.

Re:How memory maps (mmap) deliver faster file access in Go by dapperdrake on Thursday October 23, 2025 @09:05PM (#45689536)
It’s not accessible until it is in user space. (Virtual memory addresses mapped to physical RAM holding the data.)

Good point.

Re:How memory maps (mmap) deliver faster file access in Go by loeg on Thursday October 23, 2025 @10:14PM (#45690000)
> so if ever the underlying file descriptor is closed

Nit: Mmap mapping lifetimes are not attached to the underlying fd. The file truncation and latency concerns are valid, though.

Re:How memory maps (mmap) deliver faster file access in Go by dahfizz on Friday October 24, 2025 @08:34AM (#45693981)
Being able to avoid an extra copy is actually a huge performance gain when you can safely do it. You shouldn't discount how useful mmap is just because its not useful in every scenario.

You shouldn't replace every single file access with mmap. But when it makes sense, mmap is a big performance win.