Skip to content

Commit

Permalink
Disable buffering of file streams to improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
rikyoz committed Jul 26, 2024
1 parent 94d939e commit b9aec79
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 23 deletions.
12 changes: 3 additions & 9 deletions src/internal/cfileinstream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,16 @@

namespace bit7z {

CFileInStream::CFileInStream( const fs::path& filePath ) : CStdInStream( mFileStream ), mBuffer{} {
CFileInStream::CFileInStream( const fs::path& filePath ) : CStdInStream( mFileStream ) {
/* 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.
* Note: when using libstdc++, the buffer must be set before opening the file. */

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

mFileStream.rdbuf()->pubsetbuf( nullptr, 0 );
openFile( filePath );

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

void CFileInStream::openFile( const fs::path& filePath ) {
Expand Down
2 changes: 0 additions & 2 deletions src/internal/cfileinstream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ class CFileInStream : public CStdInStream {

private:
fs::ifstream mFileStream;
static constexpr auto kBufferSize = 1024 * 1024; // 1 MiB
std::array< char, kBufferSize > mBuffer;
};

} // namespace bit7z
Expand Down
12 changes: 3 additions & 9 deletions src/internal/cfileoutstream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
namespace bit7z {

CFileOutStream::CFileOutStream( fs::path filePath, bool createAlways )
: CStdOutStream( mFileStream ), mFilePath{ std::move( filePath ) }, mBuffer{} {
: CStdOutStream( mFileStream ), mFilePath{ std::move( filePath ) } {
std::error_code error;
if ( !createAlways && fs::exists( mFilePath, error ) ) {
if ( !error ) {
Expand All @@ -33,10 +33,7 @@ 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.rdbuf()->pubsetbuf( nullptr, 0 );
mFileStream.open( mFilePath, std::ios::binary | std::ios::trunc ); // flawfinder: ignore
if ( mFileStream.fail() ) {
#if defined( __MINGW32__ ) || defined( __MINGW64__ )
Expand All @@ -46,10 +43,7 @@ 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
mFileStream.rdbuf()->pubsetbuf( nullptr, 0 );
}

auto CFileOutStream::fail() const -> bool {
Expand Down
3 changes: 0 additions & 3 deletions src/internal/cfileoutstream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ class CFileOutStream : public CStdOutStream {
private:
fs::path mFilePath;
fs::ofstream mFileStream;

static constexpr auto kBufferSize = 1024 * 1024; // 1 MiB
std::array< char, kBufferSize > mBuffer;
};

} // namespace bit7z
Expand Down

0 comments on commit b9aec79

Please sign in to comment.