memory-mapped files

Sean Kelly sean at invisibleduck.org
Wed Feb 18 11:08:18 PST 2009


== Quote from Andrei Alexandrescu (SeeWebsiteForEmail at erdani.org)'s article
> Brad Roberts wrote:
> > bearophile wrote:
> >  >> What the heck is going on? When does memory mapping actually help?<
> >> You are scanning the file linearly, and the memory window you use is
> >> probably very small. In such situation a memory mapping is probably
> >> not the best thing. A memory mapping is useful when you for example
> >> operate with random access on a wider sliding window on the file.
> >
> > You can drop the 'sliding' part.  mmap tends to help when doing random
> > access (or sequential but non-contiguous maybe) over a file.  Pure
> > streaming is handled pretty well by both patterns.  One nicity with mmap
> > is that you can hint to the os how you'll be using it via madvise.  You
> > can't do that with [f]read.
> This all would make perfect sense if the performance was about the same
> in the two cases. But in fact memory mapping introduced a large
> *pessimization*. Why? I am supposedly copying less data and doing less
> work. This is very odd.

If I had to guess, I'd say that the OS assumes every file will be read in a
linear manner from front to back, and optimizes accordingly.  There's no
way of knowing how a memory-mapped file will be accessed however,
so no such optimization occurs.


Sean



More information about the Digitalmars-d mailing list