zstd compression - set source size as hint if it is known e. g. by file compression (slightly better performance and/or compression ratio);

although the feature still calling as "experimental", but zstd uses this in its own client since v.1.4 IIRC and the only known drawback would be significant regress of compression ration if guess considerably underestimates, but it does no matter in case of known file size.
This commit is contained in:
sebres
2023-09-14 16:48:14 +02:00
parent 62bfad55c3
commit 5697b3dece
3 changed files with 11 additions and 1 deletions

View File

@@ -286,6 +286,7 @@ static HRESULT UpdateArchive(
CMyComPtr<ICompressProgressInfo> localProgress = localProgressSpec;
localProgressSpec->Init(updateCallback, true);
NCompress::NZSTD::CEncoder *encoderSpec = new NCompress::NZSTD::CEncoder;
encoderSpec->unpackSize = unpackSize;
CMyComPtr<ICompressCoder> encoder = encoderSpec;
RINOK(props.SetCoderProps(encoderSpec, NULL));
RINOK(encoder->Code(fileInStream, outStream, NULL, NULL, localProgress));

View File

@@ -30,7 +30,8 @@ CEncoder::CEncoder():
_LdmHashLog(-1),
_LdmMinMatch(-1),
_LdmBucketSizeLog(-1),
_LdmHashRateLog(-1)
_LdmHashRateLog(-1),
unpackSize(0)
{
_props.clear();
}
@@ -251,6 +252,11 @@ STDMETHODIMP CEncoder::Code(ISequentialInStream *inStream,
err = ZSTD_CCtx_setParameter(_ctx, ZSTD_c_contentSizeFlag, 1);
if (ZSTD_isError(err)) return E_INVALIDARG;
if (unpackSize) {
err = ZSTD_CCtx_setParameter(_ctx, ZSTD_c_srcSizeHint, (int)(unpackSize <= INT_MAX ? unpackSize : INT_MAX));
if (ZSTD_isError(err)) return E_INVALIDARG;
}
/* enable ldm for large windowlog values */
if (_WindowLog > 27 && _Long == 0)
_Long = 1;

View File

@@ -67,6 +67,9 @@ class CEncoder:
Int32 _LdmHashRateLog;
public:
UInt64 unpackSize;
MY_QUERYINTERFACE_BEGIN2(ICompressCoder)
MY_QUERYINTERFACE_ENTRY(ICompressSetCoderMt)
MY_QUERYINTERFACE_ENTRY(ICompressSetCoderProperties)