From 3feea108db690f880f6e30e6e8f974cfbb78eee3 Mon Sep 17 00:00:00 2001 From: JosJuice Date: Sun, 4 Oct 2020 00:09:55 +0200 Subject: DiscIO: Decrease RAM usage during zstd compression By calling ZSTD_CCtx_setPledgedSrcSize, we can let zstd know how large a chunk is going to be before which start compressing it, which lets zstd avoid allocating more memory than needed for various internal buffers. This greatly reduces the RAM usage when using a high compression level with a small chunk size, and doesn't have much of an effect in other circumstances. A side effect of calling ZSTD_CCtx_setPledgedSrcSize is that zstd by default will write the uncompressed size into the compressed data stream as metadata. In order to save space, and since the decompressed size can be figured out through the structure of the RVZ format anyway, we disable writing the uncompressed size by setting ZSTD_c_contentSizeFlag to 0. --- Source/Core/DiscIO/WIACompression.cpp | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) (limited to 'Source/Core/DiscIO/WIACompression.cpp') diff --git a/Source/Core/DiscIO/WIACompression.cpp b/Source/Core/DiscIO/WIACompression.cpp index 20d19c4877..4fdf6cd429 100644 --- a/Source/Core/DiscIO/WIACompression.cpp +++ b/Source/Core/DiscIO/WIACompression.cpp @@ -446,7 +446,7 @@ PurgeCompressor::PurgeCompressor() PurgeCompressor::~PurgeCompressor() = default; -bool PurgeCompressor::Start() +bool PurgeCompressor::Start(std::optional size) { m_buffer.clear(); m_bytes_written = 0; @@ -550,7 +550,7 @@ Bzip2Compressor::~Bzip2Compressor() BZ2_bzCompressEnd(&m_stream); } -bool Bzip2Compressor::Start() +bool Bzip2Compressor::Start(std::optional size) { ASSERT_MSG(DISCIO, m_stream.state == nullptr, "Called Bzip2Compressor::Start() twice without calling Bzip2Compressor::End()"); @@ -674,7 +674,7 @@ LZMACompressor::~LZMACompressor() lzma_end(&m_stream); } -bool LZMACompressor::Start() +bool LZMACompressor::Start(std::optional size) { if (m_initialization_failed) return false; @@ -745,8 +745,11 @@ ZstdCompressor::ZstdCompressor(int compression_level) { 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; + } } ZstdCompressor::~ZstdCompressor() @@ -754,7 +757,7 @@ ZstdCompressor::~ZstdCompressor() ZSTD_freeCStream(m_stream); } -bool ZstdCompressor::Start() +bool ZstdCompressor::Start(std::optional size) { if (!m_stream) return false; @@ -762,7 +765,16 @@ bool ZstdCompressor::Start() m_buffer.clear(); 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) -- cgit v1.2.3