C++ io streams versus mmap

*爱你&永不变心* 提交于 2020-01-24 12:32:13

问题


I am starting a small project for a key-value store, in C++. I am wondering how C++ std streams compare to mmap in terms of scalability and performance. How does using ifstream::seekg on a file that wouldn't fit in RAM compare to using mmap/lseek?


回答1:


Ultimately, any Linux user-land application is using syscalls(2), including the C++ I/O library.

With great care, mmap and madvise (or lseek + read & posix_fadvise) could be more efficient that C++ streams (which are using read and other syscalls(2)...); but a misuse of syscalls (e.g. read-ing too small buffer) can give catastrophic performance

Also, Linux has a very good page cache (used to contain parts of recently accessed file data). And performance also depends upon the file system (and the hardware -SSD and mechanical hard disks are different beasts- and computer).

Maybe you should not reinvent your own thing and use sqlite, or gdbm, or redis, or mongodb, or postgresql, or memcached, etc...

Performance and trade-offs depend strongly on the actual use (a single 4Gbytes log file on your laptop is not the same as petabytes of video or genomics data in a datacenter). So benchmark (and notice that many tools like the ones I mentioned can be tuned wisely).



来源:https://stackoverflow.com/questions/33976891/c-io-streams-versus-mmap

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!