2

I want to use memory mapping files for data that I read only once. Currently I am using:

FILE * f = fopen("data.dat", "rb");

fseek(f, 0L, SEEK_END);
size_t fileSize = ftell(f);
fseek(f, 0L, SEEK_SET);

char * buffer = new char[fileSize];
fread(buffer, sizeof(char), fileSize, f);
fclose(f);

//operate with buffer
for (size_t i = 0; i < fileSize; i++){ 
  //...
}

I wanted to use mmap and operate directly on mapped buffer. However, I have found in Apple doc about mmap that there is:

You should not use file mapping in the following situations:

  • You want to read a file sequentially from start to finish only once.

Why? If I use fread there is more "logic", plus I am creating new array, that is also not for free.

Community
  • 1
  • 1
Martin Perry
  • 9,232
  • 8
  • 46
  • 114
  • It is not C++. Update the tag. – 273K Dec 20 '17 at 18:07
  • 1
    `mmap`, like everything, has measurable overhead. But if you're storing the *results* of `read` in a buffer, that means you're probably not using it "only once". Note also the `madvise` options which are occasionally (but not usually) useful. – o11c Dec 20 '17 at 18:07
  • memory mapped files are great if you are going to have random access across the file. For example large chunk based files read faster if they are memory mapped. for simply reading it as a continuous stream, memory mapping it takes a lot of time and resources, and you're not taking any real advantage of it. Ofcourse, if you really have HUGE files, huge as in terrabytes, it's a different story. – StarShine Dec 20 '17 at 18:08
  • 1
    @S.M. Seems like legal c++ to me. And it's definitively not c, since `new` is used. – François Andrieux Dec 20 '17 at 18:10
  • 1
    Unless you're re-reading the data, in general `mmap()` is slow. If you're reading the data more than once, the extra overhead of `mmap()` can be worth it. Read this from one Linus Torvalds: http://lkml.iu.edu/hypermail/linux/kernel/0802.0/1496.html One advantage of `mmap()` is code can be very simple. – Andrew Henle Dec 20 '17 at 18:14

0 Answers0