Skip to content

Commit c6e5257

Browse files
authored
Merge pull request #3977 from facebook/doc_advanced
Doc update
2 parents dc1f7b5 + 6f1215b commit c6e5257

File tree

4 files changed

+72
-33
lines changed

4 files changed

+72
-33
lines changed

doc/zstd_manual.html

+30-6
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,9 @@ <h3>Compression context</h3><pre> When compressing many times,
190190
const void* src, size_t srcSize,
191191
int compressionLevel);
192192
</b><p> Same as ZSTD_compress(), using an explicit ZSTD_CCtx.
193-
Important : in order to behave similarly to `ZSTD_compress()`,
194-
this function compresses at requested compression level,
195-
__ignoring any other parameter__ .
193+
Important : in order to mirror `ZSTD_compress()` behavior,
194+
this function compresses at the requested compression level,
195+
__ignoring any other advanced parameter__ .
196196
If any advanced parameter was set using the advanced API,
197197
they will all be reset. Only `compressionLevel` remains.
198198

@@ -212,7 +212,7 @@ <h3>Decompression context</h3><pre> When decompressing many times,
212212
const void* src, size_t srcSize);
213213
</b><p> Same as ZSTD_decompress(),
214214
requires an allocated ZSTD_DCtx.
215-
Compatible with sticky parameters.
215+
Compatible with sticky parameters (see below).
216216

217217
</p></pre><BR>
218218

@@ -296,6 +296,19 @@ <h3>Decompression context</h3><pre> When decompressing many times,
296296
* The higher the value of selected strategy, the more complex it is,
297297
* resulting in stronger and slower compression.
298298
* Special: value 0 means "use default strategy". */
299+
300+
ZSTD_c_targetCBlockSize=130, </b>/* v1.5.6+<b>
301+
* Attempts to fit compressed block size into approximatively targetCBlockSize.
302+
* Bound by ZSTD_TARGETCBLOCKSIZE_MIN and ZSTD_TARGETCBLOCKSIZE_MAX.
303+
* Note that it's not a guarantee, just a convergence target (default:0).
304+
* No target when targetCBlockSize == 0.
305+
* This is helpful in low bandwidth streaming environments to improve end-to-end latency,
306+
* when a client can make use of partial documents (a prominent example being Chrome).
307+
* Note: this parameter is stable since v1.5.6.
308+
* It was present as an experimental parameter in earlier versions,
309+
* but it's not recommended using it with earlier library versions
310+
* due to massive performance regressions.
311+
*/
299312
</b>/* LDM mode parameters */<b>
300313
ZSTD_c_enableLongDistanceMatching=160, </b>/* Enable long distance matching.<b>
301314
* This parameter is designed to improve compression ratio
@@ -375,7 +388,6 @@ <h3>Decompression context</h3><pre> When decompressing many times,
375388
* ZSTD_c_forceMaxWindow
376389
* ZSTD_c_forceAttachDict
377390
* ZSTD_c_literalCompressionMode
378-
* ZSTD_c_targetCBlockSize
379391
* ZSTD_c_srcSizeHint
380392
* ZSTD_c_enableDedicatedDictSearch
381393
* ZSTD_c_stableInBuffer
@@ -396,7 +408,7 @@ <h3>Decompression context</h3><pre> When decompressing many times,
396408
ZSTD_c_experimentalParam3=1000,
397409
ZSTD_c_experimentalParam4=1001,
398410
ZSTD_c_experimentalParam5=1002,
399-
ZSTD_c_experimentalParam6=1003,
411+
</b>/* was ZSTD_c_experimentalParam6=1003; is now ZSTD_c_targetCBlockSize */<b>
400412
ZSTD_c_experimentalParam7=1004,
401413
ZSTD_c_experimentalParam8=1005,
402414
ZSTD_c_experimentalParam9=1006,
@@ -483,6 +495,7 @@ <h3>Decompression context</h3><pre> When decompressing many times,
483495
void* dst, size_t dstCapacity,
484496
const void* src, size_t srcSize);
485497
</b><p> Behave the same as ZSTD_compressCCtx(), but compression parameters are set using the advanced API.
498+
(note that this entry point doesn't even expose a compression level parameter).
486499
ZSTD_compress2() always starts a new frame.
487500
Should cctx hold data from a previously unfinished frame, everything about it is forgotten.
488501
- Compression parameters are pushed into CCtx before starting compression, using ZSTD_CCtx_set*()
@@ -668,6 +681,11 @@ <h3>Streaming compression functions</h3><pre></pre><b><pre>typedef enum {
668681
only ZSTD_e_end or ZSTD_e_flush operations are allowed.
669682
Before starting a new compression job, or changing compression parameters,
670683
it is required to fully flush internal buffers.
684+
- note: if an operation ends with an error, it may leave @cctx in an undefined state.
685+
Therefore, it's UB to invoke ZSTD_compressStream2() of ZSTD_compressStream() on such a state.
686+
In order to be re-employed after an error, a state must be reset,
687+
which can be done explicitly (ZSTD_CCtx_reset()),
688+
or is sometimes implied by methods starting a new compression job (ZSTD_initCStream(), ZSTD_compressCCtx())
671689

672690
</p></pre><BR>
673691

@@ -753,6 +771,12 @@ <h3>Streaming decompression functions</h3><pre></pre><b><pre></pre></b><BR>
753771
@return : 0 when a frame is completely decoded and fully flushed,
754772
or an error code, which can be tested using ZSTD_isError(),
755773
or any other value > 0, which means there is some decoding or flushing to do to complete current frame.
774+
775+
Note: when an operation returns with an error code, the @zds state may be left in undefined state.
776+
It's UB to invoke `ZSTD_decompressStream()` on such a state.
777+
In order to re-use such a state, it must be first reset,
778+
which can be done explicitly (`ZSTD_DCtx_reset()`),
779+
or is implied for operations starting some new decompression job (`ZSTD_initDStream`, `ZSTD_decompressDCtx()`, `ZSTD_decompress_usingDict()`)
756780

757781
</p></pre><BR>
758782

lib/compress/zstd_compress.c

+9-7
Original file line numberDiff line numberDiff line change
@@ -870,7 +870,7 @@ size_t ZSTD_CCtxParams_setParameter(ZSTD_CCtx_params* CCtxParams,
870870
#else
871871
FORWARD_IF_ERROR(ZSTD_cParam_clampBounds(param, &value), "");
872872
CCtxParams->nbWorkers = value;
873-
return CCtxParams->nbWorkers;
873+
return (size_t)(CCtxParams->nbWorkers);
874874
#endif
875875

876876
case ZSTD_c_jobSize :
@@ -894,7 +894,7 @@ size_t ZSTD_CCtxParams_setParameter(ZSTD_CCtx_params* CCtxParams,
894894
#else
895895
FORWARD_IF_ERROR(ZSTD_cParam_clampBounds(ZSTD_c_overlapLog, &value), "");
896896
CCtxParams->overlapLog = value;
897-
return CCtxParams->overlapLog;
897+
return (size_t)CCtxParams->overlapLog;
898898
#endif
899899

900900
case ZSTD_c_rsyncable :
@@ -904,7 +904,7 @@ size_t ZSTD_CCtxParams_setParameter(ZSTD_CCtx_params* CCtxParams,
904904
#else
905905
FORWARD_IF_ERROR(ZSTD_cParam_clampBounds(ZSTD_c_overlapLog, &value), "");
906906
CCtxParams->rsyncable = value;
907-
return CCtxParams->rsyncable;
907+
return (size_t)CCtxParams->rsyncable;
908908
#endif
909909

910910
case ZSTD_c_enableDedicatedDictSearch :
@@ -941,8 +941,10 @@ size_t ZSTD_CCtxParams_setParameter(ZSTD_CCtx_params* CCtxParams,
941941
return CCtxParams->ldmParams.hashRateLog;
942942

943943
case ZSTD_c_targetCBlockSize :
944-
if (value!=0) /* 0 ==> default */
944+
if (value!=0) { /* 0 ==> default */
945+
value = MAX(value, ZSTD_TARGETCBLOCKSIZE_MIN);
945946
BOUNDCHECK(ZSTD_c_targetCBlockSize, value);
947+
}
946948
CCtxParams->targetCBlockSize = (U32)value;
947949
return CCtxParams->targetCBlockSize;
948950

@@ -970,7 +972,7 @@ size_t ZSTD_CCtxParams_setParameter(ZSTD_CCtx_params* CCtxParams,
970972
case ZSTD_c_validateSequences:
971973
BOUNDCHECK(ZSTD_c_validateSequences, value);
972974
CCtxParams->validateSequences = value;
973-
return CCtxParams->validateSequences;
975+
return (size_t)CCtxParams->validateSequences;
974976

975977
case ZSTD_c_useBlockSplitter:
976978
BOUNDCHECK(ZSTD_c_useBlockSplitter, value);
@@ -985,7 +987,7 @@ size_t ZSTD_CCtxParams_setParameter(ZSTD_CCtx_params* CCtxParams,
985987
case ZSTD_c_deterministicRefPrefix:
986988
BOUNDCHECK(ZSTD_c_deterministicRefPrefix, value);
987989
CCtxParams->deterministicRefPrefix = !!value;
988-
return CCtxParams->deterministicRefPrefix;
990+
return (size_t)CCtxParams->deterministicRefPrefix;
989991

990992
case ZSTD_c_prefetchCDictTables:
991993
BOUNDCHECK(ZSTD_c_prefetchCDictTables, value);
@@ -995,7 +997,7 @@ size_t ZSTD_CCtxParams_setParameter(ZSTD_CCtx_params* CCtxParams,
995997
case ZSTD_c_enableSeqProducerFallback:
996998
BOUNDCHECK(ZSTD_c_enableSeqProducerFallback, value);
997999
CCtxParams->enableMatchFinderFallback = value;
998-
return CCtxParams->enableMatchFinderFallback;
1000+
return (size_t)CCtxParams->enableMatchFinderFallback;
9991001

10001002
case ZSTD_c_maxBlockSize:
10011003
if (value!=0) /* 0 ==> default */

lib/compress/zstd_compress_superblock.c

+1-3
Original file line numberDiff line numberDiff line change
@@ -469,8 +469,6 @@ static size_t sizeBlockSequences(const seqDef* sp, size_t nbSeqs,
469469
return n;
470470
}
471471

472-
#define CBLOCK_TARGET_SIZE_MIN 1340 /* suitable to fit into an ethernet / wifi / 4G transport frame */
473-
474472
/** ZSTD_compressSubBlock_multi() :
475473
* Breaks super-block into multiple sub-blocks and compresses them.
476474
* Entropy will be written into the first block.
@@ -504,7 +502,7 @@ static size_t ZSTD_compressSubBlock_multi(const seqStore_t* seqStorePtr,
504502
const BYTE* llCodePtr = seqStorePtr->llCode;
505503
const BYTE* mlCodePtr = seqStorePtr->mlCode;
506504
const BYTE* ofCodePtr = seqStorePtr->ofCode;
507-
size_t const minTarget = CBLOCK_TARGET_SIZE_MIN; /* enforce minimum size, to reduce undesirable side effects */
505+
size_t const minTarget = ZSTD_TARGETCBLOCKSIZE_MIN; /* enforce minimum size, to reduce undesirable side effects */
508506
size_t const targetCBlockSize = MAX(minTarget, cctxParams->targetCBlockSize);
509507
int writeLitEntropy = (entropyMetadata->hufMetadata.hType == set_compressed);
510508
int writeSeqEntropy = 1;

lib/zstd.h

+32-17
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,9 @@ ZSTDLIB_API size_t ZSTD_freeCCtx(ZSTD_CCtx* cctx); /* accept NULL pointer *
262262

263263
/*! ZSTD_compressCCtx() :
264264
* Same as ZSTD_compress(), using an explicit ZSTD_CCtx.
265-
* Important : in order to behave similarly to `ZSTD_compress()`,
266-
* this function compresses at requested compression level,
267-
* __ignoring any other parameter__ .
265+
* Important : in order to mirror `ZSTD_compress()` behavior,
266+
* this function compresses at the requested compression level,
267+
* __ignoring any other advanced parameter__ .
268268
* If any advanced parameter was set using the advanced API,
269269
* they will all be reset. Only `compressionLevel` remains.
270270
*/
@@ -286,7 +286,7 @@ ZSTDLIB_API size_t ZSTD_freeDCtx(ZSTD_DCtx* dctx); /* accept NULL pointer *
286286
/*! ZSTD_decompressDCtx() :
287287
* Same as ZSTD_decompress(),
288288
* requires an allocated ZSTD_DCtx.
289-
* Compatible with sticky parameters.
289+
* Compatible with sticky parameters (see below).
290290
*/
291291
ZSTDLIB_API size_t ZSTD_decompressDCtx(ZSTD_DCtx* dctx,
292292
void* dst, size_t dstCapacity,
@@ -302,12 +302,12 @@ ZSTDLIB_API size_t ZSTD_decompressDCtx(ZSTD_DCtx* dctx,
302302
* using ZSTD_CCtx_set*() functions.
303303
* Pushed parameters are sticky : they are valid for next compressed frame, and any subsequent frame.
304304
* "sticky" parameters are applicable to `ZSTD_compress2()` and `ZSTD_compressStream*()` !
305-
* __They do not apply to "simple" one-shot variants such as ZSTD_compressCCtx()__ .
305+
* __They do not apply to one-shot variants such as ZSTD_compressCCtx()__ .
306306
*
307307
* It's possible to reset all parameters to "default" using ZSTD_CCtx_reset().
308308
*
309309
* This API supersedes all other "advanced" API entry points in the experimental section.
310-
* In the future, we expect to remove from experimental API entry points which are redundant with this API.
310+
* In the future, we expect to remove API entry points from experimental which are redundant with this API.
311311
*/
312312

313313

@@ -390,16 +390,19 @@ typedef enum {
390390
* The higher the value of selected strategy, the more complex it is,
391391
* resulting in stronger and slower compression.
392392
* Special: value 0 means "use default strategy". */
393-
ZSTD_c_targetCBlockSize=130, /* Tries to fit compressed block size to be
394-
* around targetCBlockSize. No target when
395-
* targetCBlockSize == 0. There is no guarantee
396-
* on compressed block size (default:0).
397-
* Since the decoder has to buffer a complete
398-
* block to begin decoding it, in low band-
399-
* width streaming environments this may
400-
* improve end-to-end latency. Bound by
401-
* ZSTD_TARGETCBLOCKSIZE_MIN and
402-
* ZSTD_TARGETCBLOCKSIZE_MAX. */
393+
394+
ZSTD_c_targetCBlockSize=130, /* v1.5.6+
395+
* Attempts to fit compressed block size into approximatively targetCBlockSize.
396+
* Bound by ZSTD_TARGETCBLOCKSIZE_MIN and ZSTD_TARGETCBLOCKSIZE_MAX.
397+
* Note that it's not a guarantee, just a convergence target (default:0).
398+
* No target when targetCBlockSize == 0.
399+
* This is helpful in low bandwidth streaming environments to improve end-to-end latency,
400+
* when a client can make use of partial documents (a prominent example being Chrome).
401+
* Note: this parameter is stable since v1.5.6.
402+
* It was present as an experimental parameter in earlier versions,
403+
* but it's not recommended using it with earlier library versions
404+
* due to massive performance regressions.
405+
*/
403406
/* LDM mode parameters */
404407
ZSTD_c_enableLongDistanceMatching=160, /* Enable long distance matching.
405408
* This parameter is designed to improve compression ratio
@@ -584,6 +587,7 @@ ZSTDLIB_API size_t ZSTD_CCtx_reset(ZSTD_CCtx* cctx, ZSTD_ResetDirective reset);
584587

585588
/*! ZSTD_compress2() :
586589
* Behave the same as ZSTD_compressCCtx(), but compression parameters are set using the advanced API.
590+
* (note that this entry point doesn't even expose a compression level parameter).
587591
* ZSTD_compress2() always starts a new frame.
588592
* Should cctx hold data from a previously unfinished frame, everything about it is forgotten.
589593
* - Compression parameters are pushed into CCtx before starting compression, using ZSTD_CCtx_set*()
@@ -787,6 +791,11 @@ typedef enum {
787791
* only ZSTD_e_end or ZSTD_e_flush operations are allowed.
788792
* Before starting a new compression job, or changing compression parameters,
789793
* it is required to fully flush internal buffers.
794+
* - note: if an operation ends with an error, it may leave @cctx in an undefined state.
795+
* Therefore, it's UB to invoke ZSTD_compressStream2() of ZSTD_compressStream() on such a state.
796+
* In order to be re-employed after an error, a state must be reset,
797+
* which can be done explicitly (ZSTD_CCtx_reset()),
798+
* or is sometimes implied by methods starting a new compression job (ZSTD_initCStream(), ZSTD_compressCCtx())
790799
*/
791800
ZSTDLIB_API size_t ZSTD_compressStream2( ZSTD_CCtx* cctx,
792801
ZSTD_outBuffer* output,
@@ -900,6 +909,12 @@ ZSTDLIB_API size_t ZSTD_initDStream(ZSTD_DStream* zds);
900909
* @return : 0 when a frame is completely decoded and fully flushed,
901910
* or an error code, which can be tested using ZSTD_isError(),
902911
* or any other value > 0, which means there is some decoding or flushing to do to complete current frame.
912+
*
913+
* Note: when an operation returns with an error code, the @zds state may be left in undefined state.
914+
* It's UB to invoke `ZSTD_decompressStream()` on such a state.
915+
* In order to re-use such a state, it must be first reset,
916+
* which can be done explicitly (`ZSTD_DCtx_reset()`),
917+
* or is implied for operations starting some new decompression job (`ZSTD_initDStream`, `ZSTD_decompressDCtx()`, `ZSTD_decompress_usingDict()`)
903918
*/
904919
ZSTDLIB_API size_t ZSTD_decompressStream(ZSTD_DStream* zds, ZSTD_outBuffer* output, ZSTD_inBuffer* input);
905920

@@ -1250,7 +1265,7 @@ ZSTDLIB_API size_t ZSTD_sizeof_DDict(const ZSTD_DDict* ddict);
12501265
#define ZSTD_LDM_HASHRATELOG_MAX (ZSTD_WINDOWLOG_MAX - ZSTD_HASHLOG_MIN)
12511266

12521267
/* Advanced parameter bounds */
1253-
#define ZSTD_TARGETCBLOCKSIZE_MIN 64
1268+
#define ZSTD_TARGETCBLOCKSIZE_MIN 1340 /* suitable to fit into an ethernet / wifi / 4G transport frame */
12541269
#define ZSTD_TARGETCBLOCKSIZE_MAX ZSTD_BLOCKSIZE_MAX
12551270
#define ZSTD_SRCSIZEHINT_MIN 0
12561271
#define ZSTD_SRCSIZEHINT_MAX INT_MAX

0 commit comments

Comments
 (0)