https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63746
Bug ID: 63746 Summary: i/o fragmentation inside basic_filebuf::xsputn Product: gcc Version: 4.9.2 Status: UNCONFIRMED Severity: normal Priority: P3 Component: libstdc++ Assignee: unassigned at gcc dot gnu.org Reporter: pluto at agmk dot net Hi, i'm trying to minimize small i/o operations for better overal application perfromance over networked filesystems. setting quite large stream buffer doesn't work as axpected in some cases. #include <iostream> #include <fstream> #include <memory> #include <sstream> #include <string> int main() { std::size_t bufferSize = 1024*1024; std::unique_ptr< char > buffer( new char[ bufferSize ] ); std::ofstream f; f.rdbuf()->pubsetbuf( buffer.get(), bufferSize ); f.open( "s.txt", std::ios_base::out | std::ios_base::binary ); std::stringstream ss; std::string s1( 10240, 'x' ); ss.str( std::string() ); ss << s1; f << ss.rdbuf(); ss.str( std::string() ); ss << s1; f << ss.rdbuf(); f.close(); } % g++ s.cpp -o s -Wall -g2 -std=c++11 % LANG=C strace ./s (...) open("s.txt", O_WRONLY|O_CREAT|O_TRUNC, 0666) = 3 writev(3, [{NULL, 0}, {"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"..., 8136}], 2) = 8136 writev(3, [{"", 0}, {"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"..., 2104}], 2) = 2104 writev(3, [{"", 0}, {"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"..., 10240}], 2) = 10240 close(3) i've expected that 20kB i/o from testcase will fit in the 1MB buffer but the fstream.tcc contains magic const __chunk = 1k and logic that not use the buffer in the optimal way.