Skip to content

Commit dc1af5f

Browse files
committed
Merge pull request #72 from Cyan4973/dev
Dev
2 parents 42e9bed + 530918b commit dc1af5f

File tree

13 files changed

+107
-38
lines changed

13 files changed

+107
-38
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
# ################################################################
3333

3434
# Version number
35-
export VERSION := 0.3.5
35+
export VERSION := 0.3.6
3636

3737
PRGDIR = programs
3838
ZSTDDIR = lib

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
v0.3.6
2+
small blocks params
3+
14
v0.3.5
25
minor generic compression improvements
36

images/CSpeed.png

44 Bytes
Loading

lib/zstd.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ extern "C" {
4848
***************************************/
4949
#define ZSTD_VERSION_MAJOR 0 /* for breaking interface changes */
5050
#define ZSTD_VERSION_MINOR 3 /* for new (non-breaking) interface capabilities */
51-
#define ZSTD_VERSION_RELEASE 5 /* for tweaks, bug-fixes, or development */
51+
#define ZSTD_VERSION_RELEASE 6 /* for tweaks, bug-fixes, or development */
5252
#define ZSTD_VERSION_NUMBER (ZSTD_VERSION_MAJOR *100*100 + ZSTD_VERSION_MINOR *100 + ZSTD_VERSION_RELEASE)
5353
unsigned ZSTD_versionNumber (void);
5454

lib/zstd_internal.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,14 @@ static const U32 g_searchStrength = 8;
226226
*/
227227
MEM_STATIC void ZSTD_storeSeq(seqStore_t* seqStorePtr, size_t litLength, const BYTE* literals, size_t offsetCode, size_t matchCode)
228228
{
229+
#if 0
230+
static const BYTE* g_start = NULL;
231+
if (g_start==NULL) g_start = literals;
232+
if (literals - g_start == 8695)
233+
printf("pos %6u : %3u literals & match %3u bytes at distance %6u \n",
234+
(U32)(literals - g_start), (U32)litLength, (U32)matchCode+4, (U32)offsetCode);
235+
#endif
236+
229237
/* copy Literals */
230238
ZSTD_wildcopy(seqStorePtr->lit, literals, litLength);
231239
seqStorePtr->lit += litLength;

lib/zstdhc.c

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ size_t ZSTD_HC_freeCCtx(ZSTD_HC_CCtx* cctx)
111111
/** ZSTD_HC_validateParams
112112
correct params value to remain within authorized range
113113
optimize for srcSize if srcSize > 0 */
114-
void ZSTD_HC_validateParams(ZSTD_HC_parameters* params, size_t srcSize)
114+
void ZSTD_HC_validateParams(ZSTD_HC_parameters* params, U64 srcSizeHint)
115115
{
116116
const U32 btPlus = (params->strategy == ZSTD_HC_btlazy2);
117117

@@ -120,9 +120,9 @@ void ZSTD_HC_validateParams(ZSTD_HC_parameters* params, size_t srcSize)
120120
if (params->windowLog < ZSTD_HC_WINDOWLOG_MIN) params->windowLog = ZSTD_HC_WINDOWLOG_MIN;
121121

122122
/* correct params, to use less memory */
123-
if (srcSize > 0)
123+
if ((srcSizeHint > 0) && (srcSizeHint < (1<<ZSTD_HC_WINDOWLOG_MAX)))
124124
{
125-
U32 srcLog = ZSTD_highbit((U32)srcSize-1) + 1;
125+
U32 srcLog = ZSTD_highbit((U32)srcSizeHint-1) + 1;
126126
if (params->windowLog > srcLog) params->windowLog = srcLog;
127127
}
128128

@@ -139,9 +139,10 @@ void ZSTD_HC_validateParams(ZSTD_HC_parameters* params, size_t srcSize)
139139

140140

141141
static size_t ZSTD_HC_resetCCtx_advanced (ZSTD_HC_CCtx* zc,
142-
ZSTD_HC_parameters params)
142+
ZSTD_HC_parameters params,
143+
U64 srcSizeHint)
143144
{
144-
ZSTD_HC_validateParams(&params, 0);
145+
ZSTD_HC_validateParams(&params, srcSizeHint);
145146

146147
/* reserve table memory */
147148
{
@@ -930,7 +931,7 @@ size_t ZSTD_HC_compressContinue (ZSTD_HC_CCtx* ctxPtr,
930931
if (ip != ctxPtr->end)
931932
{
932933
if (ctxPtr->end != NULL)
933-
ZSTD_HC_resetCCtx_advanced(ctxPtr, ctxPtr->params);
934+
ZSTD_HC_resetCCtx_advanced(ctxPtr, ctxPtr->params, srcSize);
934935
ctxPtr->base = ip;
935936
}
936937

@@ -941,22 +942,24 @@ size_t ZSTD_HC_compressContinue (ZSTD_HC_CCtx* ctxPtr,
941942

942943
size_t ZSTD_HC_compressBegin_advanced(ZSTD_HC_CCtx* ctx,
943944
void* dst, size_t maxDstSize,
944-
const ZSTD_HC_parameters params)
945+
const ZSTD_HC_parameters params,
946+
U64 srcSizeHint)
945947
{
946948
size_t errorCode;
947949
if (maxDstSize < 4) return ERROR(dstSize_tooSmall);
948-
errorCode = ZSTD_HC_resetCCtx_advanced(ctx, params);
950+
errorCode = ZSTD_HC_resetCCtx_advanced(ctx, params, srcSizeHint);
949951
if (ZSTD_isError(errorCode)) return errorCode;
950952
MEM_writeLE32(dst, ZSTD_magicNumber); /* Write Header */
951953
return 4;
952954
}
953955

954956

955-
size_t ZSTD_HC_compressBegin(ZSTD_HC_CCtx* ctx, void* dst, size_t maxDstSize, int compressionLevel)
957+
size_t ZSTD_HC_compressBegin(ZSTD_HC_CCtx* ctx, void* dst, size_t maxDstSize, int compressionLevel, U64 srcSizeHint)
956958
{
959+
int tableID = ((srcSizeHint-1) > 128 KB); /* intentional underflow for 0 */
957960
if (compressionLevel<=0) compressionLevel = 1;
958961
if (compressionLevel > ZSTD_HC_MAX_CLEVEL) compressionLevel = ZSTD_HC_MAX_CLEVEL;
959-
return ZSTD_HC_compressBegin_advanced(ctx, dst, maxDstSize, ZSTD_HC_defaultParameters[compressionLevel]);
962+
return ZSTD_HC_compressBegin_advanced(ctx, dst, maxDstSize, ZSTD_HC_defaultParameters[tableID][compressionLevel], srcSizeHint);
960963
}
961964

962965

@@ -994,7 +997,7 @@ size_t ZSTD_HC_compress_advanced (ZSTD_HC_CCtx* ctx,
994997
}
995998

996999
/* Header */
997-
oSize = ZSTD_HC_compressBegin_advanced(ctx, dst, maxDstSize, params);
1000+
oSize = ZSTD_HC_compressBegin_advanced(ctx, dst, maxDstSize, params, srcSize);
9981001
if(ZSTD_isError(oSize)) return oSize;
9991002
op += oSize;
10001003
maxDstSize -= oSize;
@@ -1016,9 +1019,10 @@ size_t ZSTD_HC_compress_advanced (ZSTD_HC_CCtx* ctx,
10161019

10171020
size_t ZSTD_HC_compressCCtx (ZSTD_HC_CCtx* ctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize, int compressionLevel)
10181021
{
1022+
const int tableID = (srcSize > 128 KB);
10191023
if (compressionLevel<=1) return ZSTD_compress(dst, maxDstSize, src, srcSize); /* fast mode */
10201024
if (compressionLevel > ZSTD_HC_MAX_CLEVEL) compressionLevel = ZSTD_HC_MAX_CLEVEL;
1021-
return ZSTD_HC_compress_advanced(ctx, dst, maxDstSize, src, srcSize, ZSTD_HC_defaultParameters[compressionLevel]);
1025+
return ZSTD_HC_compress_advanced(ctx, dst, maxDstSize, src, srcSize, ZSTD_HC_defaultParameters[tableID][compressionLevel]);
10221026
}
10231027

10241028
size_t ZSTD_HC_compress(void* dst, size_t maxDstSize, const void* src, size_t srcSize, int compressionLevel)

lib/zstdhc_static.h

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,14 @@ size_t ZSTD_HC_compress_advanced (ZSTD_HC_CCtx* ctx,
8383

8484
/** ZSTD_HC_validateParams
8585
correct params value to remain within authorized range
86-
optimize for srcSize if srcSize > 0 */
87-
void ZSTD_HC_validateParams(ZSTD_HC_parameters* params, size_t srcSize);
86+
srcSizeHint value is optional, select 0 if not known */
87+
void ZSTD_HC_validateParams(ZSTD_HC_parameters* params, U64 srcSizeHint);
8888

8989

9090
/* *************************************
9191
* Streaming functions
9292
***************************************/
93-
size_t ZSTD_HC_compressBegin(ZSTD_HC_CCtx* ctx, void* dst, size_t maxDstSize, int compressionLevel);
93+
size_t ZSTD_HC_compressBegin(ZSTD_HC_CCtx* ctx, void* dst, size_t maxDstSize, int compressionLevel, U64 srcSizeHint);
9494
size_t ZSTD_HC_compressContinue(ZSTD_HC_CCtx* ctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize);
9595
size_t ZSTD_HC_compressEnd(ZSTD_HC_CCtx* ctx, void* dst, size_t maxDstSize);
9696

@@ -99,7 +99,32 @@ size_t ZSTD_HC_compressEnd(ZSTD_HC_CCtx* ctx, void* dst, size_t maxDstSize);
9999
* Pre-defined compression levels
100100
***************************************/
101101
#define ZSTD_HC_MAX_CLEVEL 20
102-
static const ZSTD_HC_parameters ZSTD_HC_defaultParameters[ZSTD_HC_MAX_CLEVEL+1] = {
102+
static const ZSTD_HC_parameters ZSTD_HC_defaultParameters[2][ZSTD_HC_MAX_CLEVEL+1] = {
103+
{ /* for <= 128 KB */
104+
/* W, C, H, S, L, strat */
105+
{ 17, 12, 12, 1, 4, ZSTD_HC_fast }, /* level 0 - never used */
106+
{ 17, 12, 13, 1, 6, ZSTD_HC_fast }, /* level 1 */
107+
{ 17, 15, 16, 1, 5, ZSTD_HC_fast }, /* level 2 */
108+
{ 17, 16, 17, 1, 5, ZSTD_HC_fast }, /* level 3 */
109+
{ 17, 13, 15, 2, 4, ZSTD_HC_greedy }, /* level 4 */
110+
{ 17, 15, 17, 3, 4, ZSTD_HC_greedy }, /* level 5 */
111+
{ 17, 14, 17, 3, 4, ZSTD_HC_lazy }, /* level 6 */
112+
{ 17, 16, 17, 4, 4, ZSTD_HC_lazy }, /* level 7 */
113+
{ 17, 16, 17, 4, 4, ZSTD_HC_lazy2 }, /* level 8 */
114+
{ 17, 17, 16, 5, 4, ZSTD_HC_lazy2 }, /* level 9 */
115+
{ 17, 17, 16, 6, 4, ZSTD_HC_lazy2 }, /* level 10 */
116+
{ 17, 17, 16, 7, 4, ZSTD_HC_lazy2 }, /* level 11 */
117+
{ 17, 17, 16, 8, 4, ZSTD_HC_lazy2 }, /* level 12 */
118+
{ 17, 18, 16, 4, 4, ZSTD_HC_btlazy2 }, /* level 13 */
119+
{ 17, 18, 16, 5, 4, ZSTD_HC_btlazy2 }, /* level 14 */
120+
{ 17, 18, 16, 6, 4, ZSTD_HC_btlazy2 }, /* level 15 */
121+
{ 17, 18, 16, 7, 4, ZSTD_HC_btlazy2 }, /* level 16 */
122+
{ 17, 18, 16, 8, 4, ZSTD_HC_btlazy2 }, /* level 17 */
123+
{ 17, 18, 16, 9, 4, ZSTD_HC_btlazy2 }, /* level 18 */
124+
{ 17, 18, 16, 10, 4, ZSTD_HC_btlazy2 }, /* level 19 */
125+
{ 17, 18, 18, 12, 4, ZSTD_HC_btlazy2 }, /* level 20 */
126+
},
127+
{ /* for > 128 KB */
103128
/* W, C, H, S, L, strat */
104129
{ 18, 12, 12, 1, 4, ZSTD_HC_fast }, /* level 0 - never used */
105130
{ 18, 14, 14, 1, 7, ZSTD_HC_fast }, /* level 1 - in fact redirected towards zstd fast */
@@ -122,6 +147,7 @@ static const ZSTD_HC_parameters ZSTD_HC_defaultParameters[ZSTD_HC_MAX_CLEVEL+1]
122147
{ 25, 24, 23, 5, 5, ZSTD_HC_btlazy2 }, /* level 18 */
123148
{ 25, 26, 23, 5, 5, ZSTD_HC_btlazy2 }, /* level 19 */
124149
{ 26, 27, 24, 6, 5, ZSTD_HC_btlazy2 }, /* level 20 */
150+
}
125151
};
126152

127153

programs/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
# fullbench32: Same as fullbench, but forced to compile in 32-bits mode
3131
# ##########################################################################
3232

33-
VERSION?= 0.3.5
33+
VERSION?= 0.3.6
3434

3535
DESTDIR?=
3636
PREFIX ?= /usr/local

programs/bench.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ static int BMK_benchMem(void* srcBuffer, size_t srcSize, const char* fileName, i
380380
}
381381

382382

383-
static U64 BMK_GetFileSize(char* infilename)
383+
static U64 BMK_GetFileSize(const char* infilename)
384384
{
385385
int r;
386386
#if defined(_MSC_VER)

programs/fileio.c

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
/**LEGACY_SUPPORT :
3737
* decompressor can decode older formats (starting from Zstd 0.1+) */
3838
# define ZSTD_LEGACY_SUPPORT 1
39-
#endif // ZSTD_LEGACY_SUPPORT
39+
#endif
4040

4141

4242
/* *************************************
@@ -58,11 +58,13 @@
5858
/* *************************************
5959
* Includes
6060
***************************************/
61-
#include <stdio.h> /* fprintf, fopen, fread, _fileno, stdin, stdout */
62-
#include <stdlib.h> /* malloc, free */
63-
#include <string.h> /* strcmp, strlen */
64-
#include <time.h> /* clock */
65-
#include <errno.h> /* errno */
61+
#include <stdio.h> /* fprintf, fopen, fread, _fileno, stdin, stdout */
62+
#include <stdlib.h> /* malloc, free */
63+
#include <string.h> /* strcmp, strlen */
64+
#include <time.h> /* clock */
65+
#include <errno.h> /* errno */
66+
#include <sys/types.h> /* stat64 */
67+
#include <sys/stat.h> /* stat64 */
6668
#include "mem.h"
6769
#include "fileio.h"
6870
#include "zstd_static.h"
@@ -91,6 +93,10 @@
9193
# define IS_CONSOLE(stdStream) isatty(fileno(stdStream))
9294
#endif
9395

96+
#if !defined(S_ISREG)
97+
# define S_ISREG(x) (((x) & S_IFMT) == S_IFREG)
98+
#endif
99+
94100

95101
/* *************************************
96102
* Constants
@@ -215,19 +221,35 @@ static void FIO_getFileHandles(FILE** pfinput, FILE** pfoutput, const char* inpu
215221
if ( *pfoutput==0) EXM_THROW(13, "Pb opening dst : %s", output_filename);
216222
}
217223

224+
225+
static U64 FIO_getFileSize(const char* infilename)
226+
{
227+
int r;
228+
#if defined(_MSC_VER)
229+
struct _stat64 statbuf;
230+
r = _stat64(infilename, &statbuf);
231+
#else
232+
struct stat statbuf;
233+
r = stat(infilename, &statbuf);
234+
#endif
235+
if (r || !S_ISREG(statbuf.st_mode)) return 0;
236+
return (U64)statbuf.st_size;
237+
}
238+
239+
218240
typedef void* (*FIO_createC) (void);
219241
static void* local_ZSTD_createCCtx(void) { return (void*) ZSTD_createCCtx(); }
220242
static void* local_ZSTD_HC_createCCtx(void) { return (void*) ZSTD_HC_createCCtx(); }
221243

222-
typedef size_t (*FIO_initC) (void* ctx, void* dst, size_t maxDstSize, int cLevel);
223-
static size_t local_ZSTD_compressBegin (void* ctx, void* dst, size_t maxDstSize, int cLevel)
244+
typedef size_t (*FIO_initC) (void* ctx, void* dst, size_t maxDstSize, int cLevel, U64 srcSizeHint);
245+
static size_t local_ZSTD_compressBegin (void* ctx, void* dst, size_t maxDstSize, int cLevel, U64 srcSizeHint)
224246
{
225-
(void)cLevel;
247+
(void)cLevel; (void)srcSizeHint;
226248
return ZSTD_compressBegin((ZSTD_CCtx*)ctx, dst, maxDstSize);
227249
}
228-
static size_t local_ZSTD_HC_compressBegin (void* ctx, void* dst, size_t maxDstSize, int cLevel)
250+
static size_t local_ZSTD_HC_compressBegin (void* ctx, void* dst, size_t maxDstSize, int cLevel, U64 srcSizeHint)
229251
{
230-
return ZSTD_HC_compressBegin((ZSTD_HC_CCtx*)ctx, dst, maxDstSize, cLevel);
252+
return ZSTD_HC_compressBegin((ZSTD_HC_CCtx*)ctx, dst, maxDstSize, cLevel, srcSizeHint);
231253
}
232254

233255
typedef size_t (*FIO_continueC) (void* ctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize);
@@ -294,6 +316,7 @@ unsigned long long FIO_compressFilename(const char* output_filename, const char*
294316
freeC = local_ZSTD_HC_freeCCtx;
295317
}
296318
FIO_getFileHandles(&finput, &foutput, input_filename, output_filename);
319+
filesize = FIO_getFileSize(input_filename);
297320

298321
/* Allocate Memory */
299322
ctx = createC();
@@ -304,12 +327,13 @@ unsigned long long FIO_compressFilename(const char* output_filename, const char*
304327
inEnd = inBuff + inBuffSize;
305328

306329
/* Write Frame Header */
307-
cSize = initC(ctx, outBuff, outBuffSize, cLevel);
330+
cSize = initC(ctx, outBuff, outBuffSize, cLevel, filesize);
308331
if (ZSTD_isError(cSize)) EXM_THROW(22, "Compression error : cannot create frame header");
309332

310333
sizeCheck = fwrite(outBuff, 1, cSize, foutput);
311334
if (sizeCheck!=cSize) EXM_THROW(23, "Write error : cannot write header into %s", output_filename);
312335
compressedfilesize += cSize;
336+
filesize = 0;
313337

314338
/* Main compression loop */
315339
while (1)

0 commit comments

Comments
 (0)