PDA

View Full Version : Should I memory-map this file?



Angus
08-06-2009, 08:10 AM
I have a simple database file with a fixed number of records, each with a fixed length. I don't need to insert or delete anything, I just need to read records. Also, every record is merely dereferenced with an array-like index. All this to say that I don't need to bother with complicated indexing or any 3rd party libraries like SQLite.

Furthermore, every instance of the app will read just 1 record then exit. Many instances could be running at the same time.

I see the following options.


Don't memory-map anything. Just seek to the target record and read it.
Memory-map just the record I want.
Memory-map the whole file.

As I understand it, the biggest reason for memory-mapping files is to dispense with the overhead of system calls. If that's the case then there seems to be little point in going with 2 or 3, since they'll have the same number of system calls as 1 (1 seeks and reads, while 2 and 3 map and unmap). However, since multiple instances will be accessing this file, I'm wondering if there might be an advantage with memory-mapping there.

Codeplug
08-06-2009, 08:16 AM
>> However, since multiple instances will be accessing this file, I'm wondering if there might be an advantage with memory-mapping there.
Probably. Map the whole thing as read-only and let the OS take care of the rest.

gg

Kennedy
08-06-2009, 08:17 AM
If you ONLY read this file and you don't write it (or even don't write it much), consider creating a tmpfs mount (old name RAMDISC) and throw that puppy into memory. At least then you aren't doing disc reads/writes all the time.

Even if you are updating this file, you could still do this and then have a daemon that writes it back to disc every so often (like after a change is made).

Andy

MK27
08-06-2009, 08:31 AM
Here's a kind of unothodox option: you can write a kernel module to create a memory device (really, thats what tmpfs, etc are). The device has a file handle, eg /dev/mymem, but resides in kernel memory (of which there is more than you might think) and gets serviced by it.

See here:
http://cboard.cprogramming.com/linux-programming/115461-kernel-memory-char-device.html

A complication would be that this is not permanent storage, but anything involving memory will have to be copied to and from disk at some point, that's kind of a natural law, methinks.

Not sure if I agree with your reasons for wanting to do this tho.