Is there any reasons to not use "mmap" to read files?

Steven Schveighoffer schveiguy at gmail.com
Tue Feb 8 03:33:11 UTC 2022


On Sunday, 6 February 2022 at 09:40:48 UTC, rempas wrote:
> This should have probably been posted in the "Learn" section 
> but I thought that it is an advanced topic so maybe people 
> other than me may learn something too. So here we go!
>
> I'm planning to make a change to my program to use "mmap" to 
> the contents of a file rather than "fgetc". This is because I 
> learned that "mmap" can do it faster. The thing is, are there 
> any problems that can occur when using "mmap"? I need to know 
> now because changing this means changing the design of the 
> program and this is not something pleasant to do so I want to 
> be sure that I won't have to change back in the future (where 
> the project will be even bigger).

Will mmap be faster than fgetc? Almost certainly.

Will it be faster than other i/o systems? Possibly not.

for my i/o system [iopipe](https://github.com/schveiguy/iopipe), 
every array is also an iopipe, so switching between mmap and file 
i/o is trivial. See [my talk in 
2017](https://dconf.org/2017/talks/schveighoffer.html) where I 
switched to mmap while on stage to show the difference.

IMO, the best way to determine which is better is to try it and 
measure. Having an i/o system that allows easy switching is 
helpful.

For sure, depending on your other tasks in your program, 
improving the file i/o might be insignificant.

-Steve


More information about the Digitalmars-d mailing list