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

Steven Schveighoffer schveiguy at gmail.com
Sun Feb 13 02:16:01 UTC 2022


On 2/8/22 9:07 PM, user1234 wrote:
> 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).
> 
> `std.file.readText()` is just fine... your really want to do an os with 
> call `fgetc` for every single byte that has to be read ?

Just a clarification here -- `fgetc` does NOT do an OS system call for 
every character. It's a C library function, which uses a `FILE *`. And 
this is not a new development -- my ANSI C book from 1988 talks about 
how `FILE` has a buffer.

While it does not do a system call (unless the buffer is empty and it 
needs to fill the buffer), it's still an opaque call, which might cost a 
decent amount if you are reading by character.

-Steve


More information about the Digitalmars-d mailing list