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

Patrick Schluter Patrick.Schluter at bbox.fr
Sun Feb 6 12:52:45 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).

mmap has quite the overhead to set up the page table for a file. 
This means for small files, open/read/write calls (and stdio 
which build on it) are faster.

The other issue with mmap is if you use string functions on the 
mapped part, you have to make sure that there are 0 bytes in the 
file or else you risk these functions to overshoot to unmapped 
pages and crashing the application.



More information about the Digitalmars-d mailing list