summaryrefslogtreecommitdiff
path: root/Source/Core/DiscIO/WIACompression.cpp
diff options
context:
space:
mode:
authorJosJuice <josjuice@gmail.com>2020-10-04 00:09:55 +0200
committerJosJuice <josjuice@gmail.com>2020-10-07 16:40:06 +0200
commit3feea108db690f880f6e30e6e8f974cfbb78eee3 (patch)
treec30a6e5fff71a766116e035931d3023846f0cf17 /Source/Core/DiscIO/WIACompression.cpp
parentebdcddfcd033b1069dc1010f233c19c419680674 (diff)
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.
Diffstat (limited to 'Source/Core/DiscIO/WIACompression.cpp')
-rw-r--r--Source/Core/DiscIO/WIACompression.cpp24
1 files changed, 18 insertions, 6 deletions
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<u64> 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<u64> 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<u64> 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<u64> 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)