Merge pull request #9137 from JosJuice/zstd-ram-usage

DiscIO: Decrease RAM usage during zstd compression
This commit is contained in:
Léo Lam 2020-10-14 12:35:38 +02:00 committed by GitHub
commit c3cdac4863
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 13 deletions

View File

@ -1040,7 +1040,7 @@ std::optional<std::vector<u8>> WIARVZFileReader<RVZ>::Compress(Compressor* compr
{ {
if (compressor) if (compressor)
{ {
if (!compressor->Start() || !compressor->Compress(data, size) || !compressor->End()) if (!compressor->Start(size) || !compressor->Compress(data, size) || !compressor->End())
return std::nullopt; return std::nullopt;
data = compressor->GetData(); data = compressor->GetData();
@ -1564,7 +1564,7 @@ WIARVZFileReader<RVZ>::ProcessAndCompress(CompressThreadState* state, CompressPa
if (state->compressor) if (state->compressor)
{ {
if (!state->compressor->Start()) if (!state->compressor->Start(entry.exception_lists.size() + entry.main_data.size()))
return ConversionResultCode::InternalError; return ConversionResultCode::InternalError;
} }

View File

@ -446,7 +446,7 @@ PurgeCompressor::PurgeCompressor()
PurgeCompressor::~PurgeCompressor() = default; PurgeCompressor::~PurgeCompressor() = default;
bool PurgeCompressor::Start() bool PurgeCompressor::Start(std::optional<u64> size)
{ {
m_buffer.clear(); m_buffer.clear();
m_bytes_written = 0; m_bytes_written = 0;
@ -550,7 +550,7 @@ Bzip2Compressor::~Bzip2Compressor()
BZ2_bzCompressEnd(&m_stream); BZ2_bzCompressEnd(&m_stream);
} }
bool Bzip2Compressor::Start() bool Bzip2Compressor::Start(std::optional<u64> size)
{ {
ASSERT_MSG(DISCIO, m_stream.state == nullptr, ASSERT_MSG(DISCIO, m_stream.state == nullptr,
"Called Bzip2Compressor::Start() twice without calling Bzip2Compressor::End()"); "Called Bzip2Compressor::Start() twice without calling Bzip2Compressor::End()");
@ -674,7 +674,7 @@ LZMACompressor::~LZMACompressor()
lzma_end(&m_stream); lzma_end(&m_stream);
} }
bool LZMACompressor::Start() bool LZMACompressor::Start(std::optional<u64> size)
{ {
if (m_initialization_failed) if (m_initialization_failed)
return false; return false;
@ -745,8 +745,11 @@ ZstdCompressor::ZstdCompressor(int compression_level)
{ {
m_stream = ZSTD_createCStream(); m_stream = ZSTD_createCStream();
if (ZSTD_isError(ZSTD_CCtx_setParameter(m_stream, ZSTD_c_compressionLevel, compression_level))) if (ZSTD_isError(ZSTD_CCtx_setParameter(m_stream, ZSTD_c_compressionLevel, compression_level)) ||
ZSTD_isError(ZSTD_CCtx_setParameter(m_stream, ZSTD_c_contentSizeFlag, 0)))
{
m_stream = nullptr; m_stream = nullptr;
}
} }
ZstdCompressor::~ZstdCompressor() ZstdCompressor::~ZstdCompressor()
@ -754,7 +757,7 @@ ZstdCompressor::~ZstdCompressor()
ZSTD_freeCStream(m_stream); ZSTD_freeCStream(m_stream);
} }
bool ZstdCompressor::Start() bool ZstdCompressor::Start(std::optional<u64> size)
{ {
if (!m_stream) if (!m_stream)
return false; return false;
@ -762,7 +765,16 @@ bool ZstdCompressor::Start()
m_buffer.clear(); m_buffer.clear();
m_out_buffer = {}; m_out_buffer = {};
return !ZSTD_isError(ZSTD_CCtx_reset(m_stream, ZSTD_reset_session_only)); if (ZSTD_isError(ZSTD_CCtx_reset(m_stream, ZSTD_reset_session_only)))
return false;
if (size)
{
if (ZSTD_isError(ZSTD_CCtx_setPledgedSrcSize(m_stream, *size)))
return false;
}
return true;
} }
bool ZstdCompressor::Compress(const u8* data, size_t size) bool ZstdCompressor::Compress(const u8* data, size_t size)

View File

@ -154,7 +154,7 @@ public:
// First call Start, then AddDataOnlyForPurgeHashing/Compress any number of times, // First call Start, then AddDataOnlyForPurgeHashing/Compress any number of times,
// then End, then GetData/GetSize any number of times. // then End, then GetData/GetSize any number of times.
virtual bool Start() = 0; virtual bool Start(std::optional<u64> size) = 0;
virtual bool AddPrecedingDataOnlyForPurgeHashing(const u8* data, size_t size) { return true; } virtual bool AddPrecedingDataOnlyForPurgeHashing(const u8* data, size_t size) { return true; }
virtual bool Compress(const u8* data, size_t size) = 0; virtual bool Compress(const u8* data, size_t size) = 0;
virtual bool End() = 0; virtual bool End() = 0;
@ -169,7 +169,7 @@ public:
PurgeCompressor(); PurgeCompressor();
~PurgeCompressor(); ~PurgeCompressor();
bool Start() override; bool Start(std::optional<u64> size) override;
bool AddPrecedingDataOnlyForPurgeHashing(const u8* data, size_t size) override; bool AddPrecedingDataOnlyForPurgeHashing(const u8* data, size_t size) override;
bool Compress(const u8* data, size_t size) override; bool Compress(const u8* data, size_t size) override;
bool End() override; bool End() override;
@ -189,7 +189,7 @@ public:
Bzip2Compressor(int compression_level); Bzip2Compressor(int compression_level);
~Bzip2Compressor(); ~Bzip2Compressor();
bool Start() override; bool Start(std::optional<u64> size) override;
bool Compress(const u8* data, size_t size) override; bool Compress(const u8* data, size_t size) override;
bool End() override; bool End() override;
@ -211,7 +211,7 @@ public:
u8* compressor_data_size_out); u8* compressor_data_size_out);
~LZMACompressor(); ~LZMACompressor();
bool Start() override; bool Start(std::optional<u64> size) override;
bool Compress(const u8* data, size_t size) override; bool Compress(const u8* data, size_t size) override;
bool End() override; bool End() override;
@ -234,7 +234,7 @@ public:
ZstdCompressor(int compression_level); ZstdCompressor(int compression_level);
~ZstdCompressor(); ~ZstdCompressor();
bool Start() override; bool Start(std::optional<u64> size) override;
bool Compress(const u8* data, size_t size) override; bool Compress(const u8* data, size_t size) override;
bool End() override; bool End() override;