Boost C++ Libraries: Ticket #12456: mapped_file issues with huge file support https://svn.boost.org/trac10/ticket/12456 <p> I worked on an application that handles huge files (tens of Gb). Using memory mapped file is a common way to deal with such amounts of data. I'd like to use boost::iostreams::mapped_file to read and write those files. Sadly enough I faced a number of issues that makes it nearly impossible. </p> <p> I describe all the problems in one ticket instead of several tickets because all the issues are tightly connected to each other and fixing one of those requires fixing another. </p> <p> First, let us take a look at the mapped_file::open declaration: </p> <pre class="wiki"> template&lt;typename Path&gt; void open( const Path&amp; path, BOOST_IOS::openmode mode = BOOST_IOS::in | BOOST_IOS::out, size_type length = max_length, stream_offset offset = 0 ); </pre><p> It has length parameter that says how many bytes of file we wish to map into the memory. By default it try to map the whole file, but in general this parameter should be much lesser than the file size. Consider working with file of 100 Gb. Mapping the whole file is too expencive and in many cases simply impossible (consider x86 OS, for example). </p> <p> This length parameter is stored in the size_ member. </p> <pre class="wiki"> size_ = static_cast&lt;std::size_t&gt;( p.length != max_length ? std::min&lt;boost::intmax_t&gt;(p.length, size) : size ); </pre><p> </p> <pre class="wiki">std::size_t size() const { return size_; } </pre><p> That leads us to the following problem: </p> <ol><li>mapped_file::size() returns us NOT the file size. In general case it returns memory view size. If I need to know the file size </li></ol><p> I must do additional queries outside of mapped_file code. It's a painful work because I need to reimplement a lot of mapped_file::open code. Mapped_file::open already knows this size, but doesn't expose it outside. I guess mapped_file should have two methods: size() and file_size() or view_size() and size() to separately get the whole file size and the size of mapped region. </p> <ol start="2"><li>mapped_file::resize ignores length parameter. Consider: </li></ol><pre class="wiki">void mapped_file_impl::resize(stream_offset new_size) { ... size_ = new_size; param_type p(params_); map_file(p); // May modify p.hint ... } </pre><p> Compare to the code from open. No min(length, size), it just uses the new_size as view size. Again, in case of a huge file it is inappropriate and sometimes impossible. </p> <ol start="3"><li>mapped_file doesn't allow remapping file without closing it and open with new offset and length. That approach kills performance in case of application that needs intensively read huge file piece by piece. I guess mapped_file should have method remap that accepts new offset and do job similar to resize, but without resizing file, just remapping. </li></ol> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/12456 Trac 1.4.3