@@ -92,16 +92,37 @@ size_t ZSTD_compressRleLiteralsBlock (void* dst, size_t dstCapacity, const void*
92
92
return flSize + 1 ;
93
93
}
94
94
95
- size_t ZSTD_compressLiterals (ZSTD_hufCTables_t const * prevHuf ,
96
- ZSTD_hufCTables_t * nextHuf ,
97
- ZSTD_strategy strategy , int disableLiteralCompression ,
98
- void * dst , size_t dstCapacity ,
99
- const void * src , size_t srcSize ,
100
- void * entropyWorkspace , size_t entropyWorkspaceSize ,
101
- const int bmi2 ,
102
- unsigned suspectUncompressible , HUF_depth_mode depthMode )
95
+ /* ZSTD_minLiteralsToCompress() :
96
+ * returns minimal amount of literals
97
+ * for literal compression to even be attempted.
98
+ * Minimum is made tighter as compression strategy increases.
99
+ */
100
+ static size_t
101
+ ZSTD_minLiteralsToCompress (ZSTD_strategy strategy , HUF_repeat huf_repeat )
102
+ {
103
+ assert ((int )strategy >= 0 );
104
+ assert ((int )strategy <= 9 );
105
+ /* btultra2 : min 8 bytes;
106
+ * then 2x larger for each successive compression strategy
107
+ * max threshold 64 bytes */
108
+ { int const shift = MIN (9 - strategy , 3 );
109
+ size_t const mintc = (huf_repeat == HUF_repeat_valid ) ? 6 : 8 << shift ;
110
+ DEBUGLOG (7 , "minLiteralsToCompress = %zu" , mintc );
111
+ return mintc ;
112
+ }
113
+ }
114
+
115
+ size_t ZSTD_compressLiterals (
116
+ void * dst , size_t dstCapacity ,
117
+ const void * src , size_t srcSize ,
118
+ void * entropyWorkspace , size_t entropyWorkspaceSize ,
119
+ const ZSTD_hufCTables_t * prevHuf ,
120
+ ZSTD_hufCTables_t * nextHuf ,
121
+ ZSTD_strategy strategy ,
122
+ int disableLiteralCompression ,
123
+ int suspectUncompressible ,
124
+ int bmi2 )
103
125
{
104
- size_t const minGain = ZSTD_minGain (srcSize , strategy );
105
126
size_t const lhSize = 3 + (srcSize >= 1 KB ) + (srcSize >= 16 KB );
106
127
BYTE * const ostart = (BYTE * )dst ;
107
128
U32 singleStream = srcSize < 256 ;
@@ -119,15 +140,14 @@ size_t ZSTD_compressLiterals (ZSTD_hufCTables_t const* prevHuf,
119
140
if (disableLiteralCompression )
120
141
return ZSTD_noCompressLiterals (dst , dstCapacity , src , srcSize );
121
142
122
- /* small ? don't even attempt compression (speed opt) */
123
- # define COMPRESS_LITERALS_SIZE_MIN 63
124
- { size_t const minLitSize = (prevHuf -> repeatMode == HUF_repeat_valid ) ? 6 : COMPRESS_LITERALS_SIZE_MIN ;
125
- if (srcSize <= minLitSize ) return ZSTD_noCompressLiterals (dst , dstCapacity , src , srcSize );
126
- }
143
+ /* if too small, don't even attempt compression (speed opt) */
144
+ if (srcSize < ZSTD_minLiteralsToCompress (strategy , prevHuf -> repeatMode ))
145
+ return ZSTD_noCompressLiterals (dst , dstCapacity , src , srcSize );
127
146
128
147
RETURN_ERROR_IF (dstCapacity < lhSize + 1 , dstSize_tooSmall , "not enough space for compression" );
129
148
{ HUF_repeat repeat = prevHuf -> repeatMode ;
130
149
int const preferRepeat = (strategy < ZSTD_lazy ) ? srcSize <= 1024 : 0 ;
150
+ HUF_depth_mode const depthMode = (strategy >= HUF_OPTIMAL_DEPTH_THRESHOLD ) ? HUF_depth_optimal : HUF_depth_fast ;
131
151
typedef size_t (* huf_compress_f )(void * , size_t , const void * , size_t , unsigned , unsigned , void * , size_t , HUF_CElt * , HUF_repeat * , int , int , unsigned , HUF_depth_mode );
132
152
huf_compress_f huf_compress ;
133
153
if (repeat == HUF_repeat_valid && lhSize == 3 ) singleStream = 1 ;
@@ -146,10 +166,11 @@ size_t ZSTD_compressLiterals (ZSTD_hufCTables_t const* prevHuf,
146
166
}
147
167
}
148
168
149
- if ((cLitSize == 0 ) || (cLitSize >= srcSize - minGain ) || ERR_isError (cLitSize )) {
150
- ZSTD_memcpy (nextHuf , prevHuf , sizeof (* prevHuf ));
151
- return ZSTD_noCompressLiterals (dst , dstCapacity , src , srcSize );
152
- }
169
+ { size_t const minGain = ZSTD_minGain (srcSize , strategy );
170
+ if ((cLitSize == 0 ) || (cLitSize >= srcSize - minGain ) || ERR_isError (cLitSize )) {
171
+ ZSTD_memcpy (nextHuf , prevHuf , sizeof (* prevHuf ));
172
+ return ZSTD_noCompressLiterals (dst , dstCapacity , src , srcSize );
173
+ } }
153
174
if (cLitSize == 1 ) {
154
175
ZSTD_memcpy (nextHuf , prevHuf , sizeof (* prevHuf ));
155
176
return ZSTD_compressRleLiteralsBlock (dst , dstCapacity , src , srcSize );
0 commit comments