Skip to content

Commit 887cd4e

Browse files
committed
Split ZSTD_CCtx into smaller sub-structures
1 parent a187486 commit 887cd4e

File tree

13 files changed

+851
-727
lines changed

13 files changed

+851
-727
lines changed

lib/common/zstd_internal.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,6 @@ typedef struct {
228228
BYTE* ofCode;
229229
U32 longLengthID; /* 0 == no longLength; 1 == Lit.longLength; 2 == Match.longLength; */
230230
U32 longLengthPos;
231-
U32 rep[ZSTD_REP_NUM];
232-
U32 repToConfirm[ZSTD_REP_NUM];
233231
} seqStore_t;
234232

235233
const seqStore_t* ZSTD_getSeqStore(const ZSTD_CCtx* ctx); /* compress & dictBuilder */

lib/compress/zstd_compress.c

Lines changed: 304 additions & 217 deletions
Large diffs are not rendered by default.

lib/compress/zstd_compress_internal.h

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,17 @@ typedef struct {
5151
FSE_CTable offcodeCTable[FSE_CTABLE_SIZE_U32(OffFSELog, MaxOff)];
5252
FSE_CTable matchlengthCTable[FSE_CTABLE_SIZE_U32(MLFSELog, MaxML)];
5353
FSE_CTable litlengthCTable[FSE_CTABLE_SIZE_U32(LLFSELog, MaxLL)];
54-
U32 workspace[HUF_WORKSPACE_SIZE_U32];
5554
HUF_repeat hufCTable_repeatMode;
5655
FSE_repeat offcode_repeatMode;
5756
FSE_repeat matchlength_repeatMode;
5857
FSE_repeat litlength_repeatMode;
5958
} ZSTD_entropyCTables_t;
6059

60+
typedef struct {
61+
ZSTD_entropyCTables_t entropy;
62+
U32 rep[ZSTD_REP_NUM];
63+
} ZSTD_blockState_t;
64+
6165
typedef struct {
6266
U32 off;
6367
U32 len;
@@ -93,6 +97,22 @@ typedef struct {
9397
U32 staticPrices; /* prices follow a pre-defined cost structure, statistics are irrelevant */
9498
} optState_t;
9599

100+
typedef struct {
101+
BYTE const* nextSrc; /* next block here to continue on current prefix */ /* TODO: Does this belong here? */
102+
BYTE const* base; /* All regular indexes relative to this position */
103+
BYTE const* dictBase; /* extDict indexes relative to this position */
104+
U32 dictLimit; /* below that point, need extDict */
105+
U32 lowLimit; /* below that point, no more data */
106+
U32 nextToUpdate; /* index from which to continue table update */
107+
U32 nextToUpdate3; /* index from which to continue table update */
108+
U32 hashLog3; /* dispatch table : larger == faster, more memory */
109+
U32 loadedDictEnd; /* index of end of dictionary */ /* TODO: Does this belong here? */
110+
U32* hashTable;
111+
U32* hashTable3;
112+
U32* chainTable;
113+
optState_t opt; /* optimal parser state */
114+
} ZSTD_matchState_t;
115+
96116
typedef struct {
97117
U32 offset;
98118
U32 checksum;
@@ -136,15 +156,6 @@ struct ZSTD_CCtx_params_s {
136156
}; /* typedef'd to ZSTD_CCtx_params within "zstd.h" */
137157

138158
struct ZSTD_CCtx_s {
139-
const BYTE* nextSrc; /* next block here to continue on current prefix */
140-
const BYTE* base; /* All regular indexes relative to this position */
141-
const BYTE* dictBase; /* extDict indexes relative to this position */
142-
U32 dictLimit; /* below that point, need extDict */
143-
U32 lowLimit; /* below that point, no more data */
144-
U32 nextToUpdate; /* index from which to continue dictionary update */
145-
U32 nextToUpdate3; /* index from which to continue dictionary update */
146-
U32 hashLog3; /* dispatch table : larger == faster, more memory */
147-
U32 loadedDictEnd; /* index of end of dictionary */
148159
ZSTD_compressionStage_e stage;
149160
U32 dictID;
150161
ZSTD_CCtx_params requestedParams;
@@ -159,12 +170,11 @@ struct ZSTD_CCtx_s {
159170
size_t staticSize;
160171

161172
seqStore_t seqStore; /* sequences storage ptrs */
162-
optState_t optState;
163173
ldmState_t ldmState; /* long distance matching state */
164-
U32* hashTable;
165-
U32* hashTable3;
166-
U32* chainTable;
167-
ZSTD_entropyCTables_t* entropy;
174+
ZSTD_matchState_t matchState;
175+
ZSTD_blockState_t* prevBlock;
176+
ZSTD_blockState_t* nextBlock;
177+
U32* entropyWorkspace; /* entropy workspace of HUF_WORKSPACE_SIZE bytes */
168178

169179
/* streaming */
170180
char* inBuff;
@@ -191,6 +201,12 @@ struct ZSTD_CCtx_s {
191201
};
192202

193203

204+
typedef size_t (*ZSTD_blockCompressor) (
205+
ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
206+
ZSTD_compressionParameters const* cParams, void const* src, size_t srcSize);
207+
ZSTD_blockCompressor ZSTD_selectBlockCompressor(ZSTD_strategy strat, int extDict);
208+
209+
194210
MEM_STATIC U32 ZSTD_LLcode(U32 litLength)
195211
{
196212
static const BYTE LL_Code[64] = { 0, 1, 2, 3, 4, 5, 6, 7,

lib/compress/zstd_double_fast.c

Lines changed: 61 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,17 @@
1212
#include "zstd_double_fast.h"
1313

1414

15-
void ZSTD_fillDoubleHashTable(ZSTD_CCtx* cctx, const void* end, const U32 mls)
15+
void ZSTD_fillDoubleHashTable(ZSTD_matchState_t* ms,
16+
ZSTD_compressionParameters const* cParams,
17+
void const* end)
1618
{
17-
U32* const hashLarge = cctx->hashTable;
18-
U32 const hBitsL = cctx->appliedParams.cParams.hashLog;
19-
U32* const hashSmall = cctx->chainTable;
20-
U32 const hBitsS = cctx->appliedParams.cParams.chainLog;
21-
const BYTE* const base = cctx->base;
22-
const BYTE* ip = base + cctx->nextToUpdate;
19+
U32* const hashLarge = ms->hashTable;
20+
U32 const hBitsL = cParams->hashLog;
21+
U32 const mls = cParams->searchLength;
22+
U32* const hashSmall = ms->chainTable;
23+
U32 const hBitsS = cParams->chainLog;
24+
const BYTE* const base = ms->base;
25+
const BYTE* ip = base + ms->nextToUpdate;
2326
const BYTE* const iend = ((const BYTE*)end) - HASH_READ_SIZE;
2427
const U32 fastHashFillStep = 3;
2528

@@ -43,24 +46,24 @@ void ZSTD_fillDoubleHashTable(ZSTD_CCtx* cctx, const void* end, const U32 mls)
4346

4447

4548
FORCE_INLINE_TEMPLATE
46-
size_t ZSTD_compressBlock_doubleFast_generic(ZSTD_CCtx* cctx,
47-
const void* src, size_t srcSize,
48-
const U32 mls)
49+
size_t ZSTD_compressBlock_doubleFast_generic(
50+
ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
51+
ZSTD_compressionParameters const* cParams, void const* src, size_t srcSize,
52+
U32 const mls /* template */)
4953
{
50-
U32* const hashLong = cctx->hashTable;
51-
const U32 hBitsL = cctx->appliedParams.cParams.hashLog;
52-
U32* const hashSmall = cctx->chainTable;
53-
const U32 hBitsS = cctx->appliedParams.cParams.chainLog;
54-
seqStore_t* seqStorePtr = &(cctx->seqStore);
55-
const BYTE* const base = cctx->base;
54+
U32* const hashLong = ms->hashTable;
55+
const U32 hBitsL = cParams->hashLog;
56+
U32* const hashSmall = ms->chainTable;
57+
const U32 hBitsS = cParams->chainLog;
58+
const BYTE* const base = ms->base;
5659
const BYTE* const istart = (const BYTE*)src;
5760
const BYTE* ip = istart;
5861
const BYTE* anchor = istart;
59-
const U32 lowestIndex = cctx->dictLimit;
62+
const U32 lowestIndex = ms->dictLimit;
6063
const BYTE* const lowest = base + lowestIndex;
6164
const BYTE* const iend = istart + srcSize;
6265
const BYTE* const ilimit = iend - HASH_READ_SIZE;
63-
U32 offset_1=seqStorePtr->rep[0], offset_2=seqStorePtr->rep[1];
66+
U32 offset_1=rep[0], offset_2=rep[1];
6467
U32 offsetSaved = 0;
6568

6669
/* init */
@@ -87,7 +90,7 @@ size_t ZSTD_compressBlock_doubleFast_generic(ZSTD_CCtx* cctx,
8790
/* favor repcode */
8891
mLength = ZSTD_count(ip+1+4, ip+1+4-offset_1, iend) + 4;
8992
ip++;
90-
ZSTD_storeSeq(seqStorePtr, ip-anchor, anchor, 0, mLength-MINMATCH);
93+
ZSTD_storeSeq(seqStore, ip-anchor, anchor, 0, mLength-MINMATCH);
9194
} else {
9295
U32 offset;
9396
if ( (matchIndexL > lowestIndex) && (MEM_read64(matchLong) == MEM_read64(ip)) ) {
@@ -117,7 +120,7 @@ size_t ZSTD_compressBlock_doubleFast_generic(ZSTD_CCtx* cctx,
117120
offset_2 = offset_1;
118121
offset_1 = offset;
119122

120-
ZSTD_storeSeq(seqStorePtr, ip-anchor, anchor, offset + ZSTD_REP_MOVE, mLength-MINMATCH);
123+
ZSTD_storeSeq(seqStore, ip-anchor, anchor, offset + ZSTD_REP_MOVE, mLength-MINMATCH);
121124
}
122125

123126
/* match found */
@@ -140,61 +143,63 @@ size_t ZSTD_compressBlock_doubleFast_generic(ZSTD_CCtx* cctx,
140143
{ U32 const tmpOff = offset_2; offset_2 = offset_1; offset_1 = tmpOff; } /* swap offset_2 <=> offset_1 */
141144
hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = (U32)(ip-base);
142145
hashLong[ZSTD_hashPtr(ip, hBitsL, 8)] = (U32)(ip-base);
143-
ZSTD_storeSeq(seqStorePtr, 0, anchor, 0, rLength-MINMATCH);
146+
ZSTD_storeSeq(seqStore, 0, anchor, 0, rLength-MINMATCH);
144147
ip += rLength;
145148
anchor = ip;
146149
continue; /* faster when present ... (?) */
147150
} } }
148151

149152
/* save reps for next block */
150-
seqStorePtr->repToConfirm[0] = offset_1 ? offset_1 : offsetSaved;
151-
seqStorePtr->repToConfirm[1] = offset_2 ? offset_2 : offsetSaved;
153+
rep[0] = offset_1 ? offset_1 : offsetSaved;
154+
rep[1] = offset_2 ? offset_2 : offsetSaved;
152155

153156
/* Return the last literals size */
154157
return iend - anchor;
155158
}
156159

157160

158-
size_t ZSTD_compressBlock_doubleFast(ZSTD_CCtx* ctx, const void* src, size_t srcSize)
161+
size_t ZSTD_compressBlock_doubleFast(
162+
ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
163+
ZSTD_compressionParameters const* cParams, void const* src, size_t srcSize)
159164
{
160-
const U32 mls = ctx->appliedParams.cParams.searchLength;
165+
const U32 mls = cParams->searchLength;
161166
switch(mls)
162167
{
163168
default: /* includes case 3 */
164169
case 4 :
165-
return ZSTD_compressBlock_doubleFast_generic(ctx, src, srcSize, 4);
170+
return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, cParams, src, srcSize, 4);
166171
case 5 :
167-
return ZSTD_compressBlock_doubleFast_generic(ctx, src, srcSize, 5);
172+
return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, cParams, src, srcSize, 5);
168173
case 6 :
169-
return ZSTD_compressBlock_doubleFast_generic(ctx, src, srcSize, 6);
174+
return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, cParams, src, srcSize, 6);
170175
case 7 :
171-
return ZSTD_compressBlock_doubleFast_generic(ctx, src, srcSize, 7);
176+
return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, cParams, src, srcSize, 7);
172177
}
173178
}
174179

175180

176-
static size_t ZSTD_compressBlock_doubleFast_extDict_generic(ZSTD_CCtx* ctx,
177-
const void* src, size_t srcSize,
178-
const U32 mls)
181+
static size_t ZSTD_compressBlock_doubleFast_extDict_generic(
182+
ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
183+
ZSTD_compressionParameters const* cParams, void const* src, size_t srcSize,
184+
U32 const mls /* template */)
179185
{
180-
U32* const hashLong = ctx->hashTable;
181-
U32 const hBitsL = ctx->appliedParams.cParams.hashLog;
182-
U32* const hashSmall = ctx->chainTable;
183-
U32 const hBitsS = ctx->appliedParams.cParams.chainLog;
184-
seqStore_t* seqStorePtr = &(ctx->seqStore);
185-
const BYTE* const base = ctx->base;
186-
const BYTE* const dictBase = ctx->dictBase;
186+
U32* const hashLong = ms->hashTable;
187+
U32 const hBitsL = cParams->hashLog;
188+
U32* const hashSmall = ms->chainTable;
189+
U32 const hBitsS = cParams->chainLog;
190+
const BYTE* const base = ms->base;
191+
const BYTE* const dictBase = ms->dictBase;
187192
const BYTE* const istart = (const BYTE*)src;
188193
const BYTE* ip = istart;
189194
const BYTE* anchor = istart;
190-
const U32 lowestIndex = ctx->lowLimit;
195+
const U32 lowestIndex = ms->lowLimit;
191196
const BYTE* const dictStart = dictBase + lowestIndex;
192-
const U32 dictLimit = ctx->dictLimit;
197+
const U32 dictLimit = ms->dictLimit;
193198
const BYTE* const lowPrefixPtr = base + dictLimit;
194199
const BYTE* const dictEnd = dictBase + dictLimit;
195200
const BYTE* const iend = istart + srcSize;
196201
const BYTE* const ilimit = iend - 8;
197-
U32 offset_1=seqStorePtr->rep[0], offset_2=seqStorePtr->rep[1];
202+
U32 offset_1=rep[0], offset_2=rep[1];
198203

199204
/* Search Loop */
200205
while (ip < ilimit) { /* < instead of <=, because (ip+1) */
@@ -220,7 +225,7 @@ static size_t ZSTD_compressBlock_doubleFast_extDict_generic(ZSTD_CCtx* ctx,
220225
const BYTE* repMatchEnd = repIndex < dictLimit ? dictEnd : iend;
221226
mLength = ZSTD_count_2segments(ip+1+4, repMatch+4, iend, repMatchEnd, lowPrefixPtr) + 4;
222227
ip++;
223-
ZSTD_storeSeq(seqStorePtr, ip-anchor, anchor, 0, mLength-MINMATCH);
228+
ZSTD_storeSeq(seqStore, ip-anchor, anchor, 0, mLength-MINMATCH);
224229
} else {
225230
if ((matchLongIndex > lowestIndex) && (MEM_read64(matchLong) == MEM_read64(ip))) {
226231
const BYTE* matchEnd = matchLongIndex < dictLimit ? dictEnd : iend;
@@ -231,7 +236,7 @@ static size_t ZSTD_compressBlock_doubleFast_extDict_generic(ZSTD_CCtx* ctx,
231236
while (((ip>anchor) & (matchLong>lowMatchPtr)) && (ip[-1] == matchLong[-1])) { ip--; matchLong--; mLength++; } /* catch up */
232237
offset_2 = offset_1;
233238
offset_1 = offset;
234-
ZSTD_storeSeq(seqStorePtr, ip-anchor, anchor, offset + ZSTD_REP_MOVE, mLength-MINMATCH);
239+
ZSTD_storeSeq(seqStore, ip-anchor, anchor, offset + ZSTD_REP_MOVE, mLength-MINMATCH);
235240

236241
} else if ((matchIndex > lowestIndex) && (MEM_read32(match) == MEM_read32(ip))) {
237242
size_t const h3 = ZSTD_hashPtr(ip+1, hBitsL, 8);
@@ -256,7 +261,7 @@ static size_t ZSTD_compressBlock_doubleFast_extDict_generic(ZSTD_CCtx* ctx,
256261
}
257262
offset_2 = offset_1;
258263
offset_1 = offset;
259-
ZSTD_storeSeq(seqStorePtr, ip-anchor, anchor, offset + ZSTD_REP_MOVE, mLength-MINMATCH);
264+
ZSTD_storeSeq(seqStore, ip-anchor, anchor, offset + ZSTD_REP_MOVE, mLength-MINMATCH);
260265

261266
} else {
262267
ip += ((ip-anchor) >> g_searchStrength) + 1;
@@ -283,7 +288,7 @@ static size_t ZSTD_compressBlock_doubleFast_extDict_generic(ZSTD_CCtx* ctx,
283288
const BYTE* const repEnd2 = repIndex2 < dictLimit ? dictEnd : iend;
284289
size_t const repLength2 = ZSTD_count_2segments(ip+4, repMatch2+4, iend, repEnd2, lowPrefixPtr) + 4;
285290
U32 tmpOffset = offset_2; offset_2 = offset_1; offset_1 = tmpOffset; /* swap offset_2 <=> offset_1 */
286-
ZSTD_storeSeq(seqStorePtr, 0, anchor, 0, repLength2-MINMATCH);
291+
ZSTD_storeSeq(seqStore, 0, anchor, 0, repLength2-MINMATCH);
287292
hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = current2;
288293
hashLong[ZSTD_hashPtr(ip, hBitsL, 8)] = current2;
289294
ip += repLength2;
@@ -294,27 +299,29 @@ static size_t ZSTD_compressBlock_doubleFast_extDict_generic(ZSTD_CCtx* ctx,
294299
} } }
295300

296301
/* save reps for next block */
297-
seqStorePtr->repToConfirm[0] = offset_1; seqStorePtr->repToConfirm[1] = offset_2;
302+
rep[0] = offset_1;
303+
rep[1] = offset_2;
298304

299305
/* Return the last literals size */
300306
return iend - anchor;
301307
}
302308

303309

304-
size_t ZSTD_compressBlock_doubleFast_extDict(ZSTD_CCtx* ctx,
305-
const void* src, size_t srcSize)
310+
size_t ZSTD_compressBlock_doubleFast_extDict(
311+
ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
312+
ZSTD_compressionParameters const* cParams, void const* src, size_t srcSize)
306313
{
307-
U32 const mls = ctx->appliedParams.cParams.searchLength;
314+
U32 const mls = cParams->searchLength;
308315
switch(mls)
309316
{
310317
default: /* includes case 3 */
311318
case 4 :
312-
return ZSTD_compressBlock_doubleFast_extDict_generic(ctx, src, srcSize, 4);
319+
return ZSTD_compressBlock_doubleFast_extDict_generic(ms, seqStore, rep, cParams, src, srcSize, 4);
313320
case 5 :
314-
return ZSTD_compressBlock_doubleFast_extDict_generic(ctx, src, srcSize, 5);
321+
return ZSTD_compressBlock_doubleFast_extDict_generic(ms, seqStore, rep, cParams, src, srcSize, 5);
315322
case 6 :
316-
return ZSTD_compressBlock_doubleFast_extDict_generic(ctx, src, srcSize, 6);
323+
return ZSTD_compressBlock_doubleFast_extDict_generic(ms, seqStore, rep, cParams, src, srcSize, 6);
317324
case 7 :
318-
return ZSTD_compressBlock_doubleFast_extDict_generic(ctx, src, srcSize, 7);
325+
return ZSTD_compressBlock_doubleFast_extDict_generic(ms, seqStore, rep, cParams, src, srcSize, 7);
319326
}
320327
}

lib/compress/zstd_double_fast.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,18 @@ extern "C" {
1616
#endif
1717

1818
#include "mem.h" /* U32 */
19-
#include "zstd.h" /* ZSTD_CCtx, size_t */
19+
#include "zstd_compress_internal.h" /* ZSTD_CCtx, size_t */
20+
21+
void ZSTD_fillDoubleHashTable(ZSTD_matchState_t* ms,
22+
ZSTD_compressionParameters const* cParams,
23+
void const* end);
24+
size_t ZSTD_compressBlock_doubleFast(
25+
ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
26+
ZSTD_compressionParameters const* cParams, void const* src, size_t srcSize);
27+
size_t ZSTD_compressBlock_doubleFast_extDict(
28+
ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
29+
ZSTD_compressionParameters const* cParams, void const* src, size_t srcSize);
2030

21-
void ZSTD_fillDoubleHashTable(ZSTD_CCtx* cctx, const void* end, const U32 mls);
22-
size_t ZSTD_compressBlock_doubleFast(ZSTD_CCtx* ctx, const void* src, size_t srcSize);
23-
size_t ZSTD_compressBlock_doubleFast_extDict(ZSTD_CCtx* ctx, const void* src, size_t srcSize);
2431

2532
#if defined (__cplusplus)
2633
}

0 commit comments

Comments
 (0)