Buffer Sizing
ZSTD_compressBound()
Calculate maximum compressed size in worst case scenario.srcSize- Size of uncompressed data
srcSize >= ZSTD_MAX_INPUT_SIZE
Note: Providing dstCapacity >= ZSTD_compressBound(srcSize) guarantees enough space for compression to succeed (barring other errors).
Example:
ZSTD_COMPRESSBOUND Macro
Compile-time version ofZSTD_compressBound() for static allocation.
Maximum Input Size
Decompression Buffer Sizing
ZSTD_getFrameContentSize()
Get the decompressed size from frame header.src- Pointer to beginning of ZSTD framesrcSize- Size of buffer (must be ≥ frame header size)
- Decompressed size in bytes (when available)
ZSTD_CONTENTSIZE_UNKNOWN(0xFFFFFFFFFFFFFFFFULL - 1) - Size not encoded in frameZSTD_CONTENTSIZE_ERROR(0xFFFFFFFFFFFFFFFFULL - 2) - Error occurred
ZSTD_isError().
Example:
ZSTD_findFrameCompressedSize()
Find the compressed size of a frame.src- Pointer to start of ZSTD frame or skippable framesrcSize- Must be ≥ first frame size
ZSTD_decompress(), or error code
Note: May need to scan through frame content to reach its end.
Streaming Buffer Sizes
Compression Buffers
ZSTD_CStreamInSize()
Recommended size for input buffer.ZSTD_CStreamOutSize()
Recommended size for output buffer.Decompression Buffers
ZSTD_DStreamInSize()
Recommended size for input buffer.ZSTD_DStreamOutSize()
Recommended size for output buffer.Memory Estimation
Compression Context Size
ZSTD_estimateCCtxSize()
Estimate memory for compression context.maxCompressionLevel- Maximum compression level that will be used
ZSTD_estimateCCtxSize_usingCParams()
Estimate memory using compression parameters.cParams- Compression parameters structure
ZSTD_estimateCCtxSize_usingCCtxParams()
Estimate memory using context parameters.ZSTD_c_nbWorkers >= 1
Example:
Decompression Context Size
ZSTD_estimateDCtxSize()
Estimate memory for decompression context.Streaming Context Size
ZSTD_estimateCStreamSize()
Estimate memory for compression streaming.ZSTD_c_nbWorkers >= 1.
ZSTD_estimateCStreamSize_usingCParams()
ZSTD_estimateCStreamSize_usingCCtxParams()
ZSTD_estimateDStreamSize()
Estimate memory for decompression streaming.maxWindowSize- Maximum window size for frames to decompress
ZSTD_estimateDStreamSize_fromFrame()
Estimate memory from frame header.Dictionary Size Estimation
ZSTD_estimateCDictSize()
Estimate memory for compression dictionary.ZSTD_createCDict()).
ZSTD_estimateCDictSize_advanced()
dictSize- Size of dictionarycParams- Compression parametersdictLoadMethod-ZSTD_dlm_byCopyorZSTD_dlm_byRef
ZSTD_estimateDDictSize()
Estimate memory for decompression dictionary.Context Size Queries
Get actual current memory usage of objects:Context Reuse
Benefits of Context Reuse
Reusing contexts provides significant performance benefits:- Avoids allocation overhead - No repeated malloc/free
- Reuses internal buffers - Memory already allocated
- Better cache locality - Same memory regions used
- Reduces memory fragmentation
Compression Context Reuse
Resetting Contexts
ZSTD_CCtx_reset()
Reset compression context to clean state.ZSTD_reset_session_only(1) - Stop current compression, keep parametersZSTD_reset_parameters(2) - Reset parameters to default (no compression must be ongoing)ZSTD_reset_session_and_parameters(3) - Reset both
ZSTD_DCtx_reset()
Reset decompression context.Streaming Context Reuse
Complete Example
Best Practices
- Use ZSTD_compressBound() - Always allocate sufficient output buffer
- Reuse contexts - Create once, use many times for better performance
- Estimate memory - Use estimation functions for memory budgeting
- Check actual usage - Use
ZSTD_sizeof_*()to monitor real memory consumption - Reset between operations - Use
ZSTD_CCtx_reset()for clean state - Use recommended buffer sizes - Call
ZSTD_CStreamInSize()/ZSTD_CStreamOutSize()for streaming - Query frame size - Use
ZSTD_getFrameContentSize()when available - Handle CONTENTSIZE_UNKNOWN - Be prepared for streaming decompression
See Also
- Compression Parameters - Configure compression behavior
- Error Handling - Detect and handle errors