Does the Linux filesystem cache files efficiently? Does the Linux filesystem cache files efficiently? linux linux

Does the Linux filesystem cache files efficiently?


Yes, if you do not modify the file each time you open it.

Linux will hold the file's information in copy-on-write pages in memory, and "loading" the file into memory should be very fast (page table swap at worst).

Edit: Though, as cdhowie points out, there is no 'linux filesystem'. However, I believe the relevant code is in linux's memory management, and is therefore independent of the filesystem in question. If you're curious, you can read in the linux source about handling vm_area_struct objects in linux/mm/mmap.c, mainly.


As people have mentioned, mmap is a good solution here.

But, one 250k file is very small. You might want to read it in and put it in some sort of memory structure that matches what you want to send back to the user on startup. Ie, if it is a text file an array of lines might be a good choice, etc.


Yes, definitely. It will keep accessed files in memory indefinitely, unless something else needs the memory.

You can control this behaviour (to some extent) with the fadvise system call. See its "man" page for more details.

A read/write system call will still normally need to copy the data, so if you see a real bottleneck doing this, consider using mmap() which can avoid the copy, by mapping the cache pages directly into the process.