Skip to content

Commit

Permalink
Fix fstream optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
rikyoz committed Jul 22, 2024
1 parent 06ff5df commit 94d939e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/internal/cfileinstream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,18 @@ CFileInStream::CFileInStream( const fs::path& filePath ) : CStdInStream( mFileSt
/* By default, file stream performance is relatively poor due to the default buffer size used
* (e.g., GCC uses a small 1024-bytes buffer).
* This is a known problem (see https://stackoverflow.com/questions/26095160/why-are-stdfstreams-so-slow).
* We make the underlying file stream use a bigger buffer (1 MiB) for optimizing the reading of big files. */
* We make the underlying file stream use a bigger buffer (1 MiB) for optimizing the reading of big files.
* Note: when using libstdc++, the buffer must be set before opening the file. */

#ifdef __GLIBCXX__
mFileStream.rdbuf()->pubsetbuf( mBuffer.data(), kBufferSize );
#endif

openFile( filePath );

#ifndef __GLIBCXX__
mFileStream.rdbuf()->pubsetbuf( mBuffer.data(), kBufferSize );
#endif
}

void CFileInStream::openFile( const fs::path& filePath ) {
Expand Down
8 changes: 8 additions & 0 deletions src/internal/cfileoutstream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ CFileOutStream::CFileOutStream( fs::path filePath, bool createAlways )
}
throw BitException( "Failed to create the output file", error, path_to_tstring( mFilePath ) );
}

#ifdef __GLIBCXX__
mFileStream.rdbuf()->pubsetbuf( mBuffer.data(), kBufferSize );
#endif

mFileStream.open( mFilePath, std::ios::binary | std::ios::trunc ); // flawfinder: ignore
if ( mFileStream.fail() ) {
#if defined( __MINGW32__ ) || defined( __MINGW64__ )
Expand All @@ -42,6 +46,10 @@ CFileOutStream::CFileOutStream( fs::path filePath, bool createAlways )
throw BitException( "Failed to open the output file", last_error_code(), path_to_tstring( mFilePath ) );
#endif
}

#ifndef __GLIBCXX__
mFileStream.rdbuf()->pubsetbuf( mBuffer.data(), kBufferSize );
#endif
}

auto CFileOutStream::fail() const -> bool {
Expand Down

0 comments on commit 94d939e

Please sign in to comment.