Hacker News new | ask | show | jobs
by habibur 239 days ago
Is mmap still faster than fread? That might have been true in the 90s but I was wondering about current improvements.

If you have enough free memory, the file will be cached in memory anyway instead of residing on disk. Therefore both will be reading from memory, albeit through different API.

Looking for recent benchmark or view from OS developers.

3 comments

In our experience building a high performance database server: absolutely. If your line of thinking is “if you have enough free memory”, then these types of optimizations aren’t for you. one of the main benefits is eliminating an extra copy.

additionally, mmap is heavily optimized for random access, so if that’s what you’re doing, then you’ll have a much better time with it than fread.

(I hope a plug is not frowned upon here: if you like this kind of stuff, we’re a fully remote company and hiring C++ devs: https://apply.workable.com/quasar/j/436B0BEE43/ )

Even if the file is cached, fread has to do a memcpy. mmap doesn't.
fread is (usually) buffered io, so it actually does two additional mem copies (kernel to FILE buffer then to user buffer)
Not in Go
oh, right, this is Go (https://pkg.go.dev/github.com/odeke-em/go-utils/fread#sectio...). Do the strings it return share memory with the internal buffer?
read, or fread? fread is the buffered version that does an extra copy for no reason that would benefit this use case.