mmap file performance

Patrick Schluter Patrick.Schluter at bbox.fr
Mon Apr 15 08:05:25 UTC 2024


On Thursday, 11 April 2024 at 00:24:44 UTC, Andy Valencia wrote:
> I wrote a "count newlines" based on mapped files.  It used 
> about twice the CPU of the version which just read 1 meg at a 
> time.  I thought something was amiss (needless slice 
> indirection or something), so I wrote the code in C.  It had 
> the same CPU usage as the D version.  So...mapped files, not so 
> much.  Not D's fault.  And writing it in C made me realize how 
> much easier it is to code in D!
>
> [...]

The setup of a memory mapped file is relatively costly. For 
smaller files it is a net loss and read/write beats it hands 
down. Furthermore, sequential access is not the best way to 
exploit the advantages of mmap. Full random access is the strong 
suite of mmap as it replaces kernel syscalls (lseek,read, write 
or pread, pwrite) by user land processing.
You could try MAP_POPULATE option in the mmap as it enables 
read-ahead on the file which may help on sequential code.


More information about the Digitalmars-d-learn mailing list