Live data from Hacker News

The mmap pattern

john.freml.in

11–20 of 48 posts

Re: The mmap pattern

#11
post #8

TFA says "By mmap'ing a region and then accessing into it, the overhead of creating multitudes of small interlinked items can be reduced hugely." You do not need to call mmap directly, because malloc() will do it if your allocation exceeds MMAP_THRESHOLD (usually on the order of a megabyte). So you get this optimization "for free"--the only important part is that you do not call malloc() for each tiny object.

I think TFA talks about mapping files into memory...

Re: The mmap pattern

#15
post #9

This is basically used for this little gem: http://symas.com/mdb/ LMDB is at the heart of the ubiquitous LDAP ( OpenLDAP ) and is very well optimized ( look at his benchmarks ). Now they are optimized for reading, which is important. I would imagine mmap-ing with large amount of write will result in unpredictable performance....

You can try and manage the predictability using msync(), which you'll need anyway if you're doing any kind of transactional write.

For more general use, a simple way to keep the amount of 'pending writes' down to reasonable levels is to have a thread walking your mmap()'d regions and enforcing an msync() on a regular basis. This also gives you guarantees such as "each part of mmap'd file is no more than N seconds out of date".

Of course, you'll potentially increase disk load overall, since a page dirtied three times, once every second, might only get written out once by the kernel (after 5s) but if you're syncing every 1s it'll get written three times.

But you should be able to take control.

Re: The mmap pattern

#16
If you can live with boost, then boost::interprocess is a good way of managing a mmapped segment - shared with other processes or shared with yourself over time.

This give string,set,map,list,deque,vector types that work in the shared space.

http://www.boost.org/doc/libs/1_55_0/doc/html/interprocess/a...

Re: The mmap pattern

#17
"explicitly choosing a file and mapping that into the memory space of the program means it can persist across crashes"

It's not that your program crashes, it's that your memory becomes corrupt and that causes the crash.

Restarting program is essentially redoing all the memory. If you persist your memory your program will crash on run.

Re: The mmap pattern

#18

"explicitly choosing a file and mapping that into the memory space of the program means it can persist across crashes" It's not that your program crashes, it's that your memory becomes corrupt and that causes the crash. Restarting program is essentially redoing all the memory. If you persist your memory your program will crash on run.

Different types of crashes and different memory. You can crash because you program did not handle a network error well, etc. Also, you do not need to use mmap'ed memory for everything. You can still use the stack and the heap for temporary structures, but store permanent data in the mmap'ed segments. Think about persistent data vs running queries in a database server.

Re: The mmap pattern

#19
> One very key architectural decision for a system is the degree of reliability that it should possess. This is an explicit trade-off between the rapidity of development (in particular the level of indoctrination needed before new contributors are able to augment the feature set) and the operational stability. By preserving state explicitly to memory backed files, several classes of unexpected events causing the program to crash can be recovered from with minimal disruption. The benefit here is that the system can be developed rapidly with code reviews focusing on data integrity and paying less attention to complex interactions that can lead to crashes.

The mmapped files are going to break every time you make the smallest change to your data structures. This strikes me as not terribly useful for rapid development.

Re: The mmap pattern

#20
post #5

For a while I've wanted a nice C library for an mmapped heap with allocation and common data structures (and maybe locks?) - all the usual stuff you'd expect in a standard library, but with support for relative offsets instead of pointers, crash robustness, introspection, and other features required to work well with a persistent file. I do not know any library of this type that currently exists.

SQLite
Post reply on HN