Search code examples
c++clinuxfile-iolow-latency

Absolute fastest way to store a 32-bit integer to disk?


I have a very latency sensitive routine that generates integers sequentially, but needs to store the last generated one to disk in case of a crash or re-start.

Currently I'm doing a seek to beginning of file then writing out the integer then flush each time a new int is generated. The flush is required so the write at least hits the battery-backed controller cache.

The seek is quite costly so I was thinking about just appending 4 bytes and if recovery is needed then to seek to the end and read the last 4 bytes. This previous statement obviously assumes that there isn't too much other disk activity happening, so the write head should ideally stay at end of the file.

The number won't typically go higher than 10,000,000 so 40MB isn't so bad.

Any advice as to how to achieve minimum latency without sacrificing integrity?

C or C++ on Linux 2.6+


Solution

  • I would think the fastest/easiest way to do this would be with mmap/msync -- mmap 1 page of the file into memory and store the value on that page. Any time the value changes, call msync(2) to force the page back to disk. This way you need only one system call per store