minor code refactor

This commit is contained in:
Yann Collet 2016-03-20 15:46:10 +01:00
parent 516ba88022
commit de406eebcd
5 changed files with 49 additions and 58 deletions

View File

@ -28,7 +28,7 @@
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
You can contact the author at : You can contact the author at :
- Source repository : https://github.com/Cyan4973/zstd - Homepage : http://www.zstd.net
****************************************************************** */ ****************************************************************** */
/* Note : this module is expected to remain private, do not expose it */ /* Note : this module is expected to remain private, do not expose it */
@ -62,7 +62,7 @@ extern "C" {
/*-**************************************** /*-****************************************
* Customization * Customization (error_public.h)
******************************************/ ******************************************/
typedef ZSTD_ErrorCode ERR_enum; typedef ZSTD_ErrorCode ERR_enum;
#define PREFIX(name) ZSTD_error_##name #define PREFIX(name) ZSTD_error_##name
@ -74,7 +74,7 @@ typedef ZSTD_ErrorCode ERR_enum;
#ifdef ERROR #ifdef ERROR
# undef ERROR /* reported already defined on VS 2015 (Rich Geldreich) */ # undef ERROR /* reported already defined on VS 2015 (Rich Geldreich) */
#endif #endif
#define ERROR(name) (size_t)-PREFIX(name) #define ERROR(name) ((size_t)-PREFIX(name))
ERR_STATIC unsigned ERR_isError(size_t code) { return (code > ERROR(maxCode)); } ERR_STATIC unsigned ERR_isError(size_t code) { return (code > ERROR(maxCode)); }
@ -101,12 +101,12 @@ ERR_STATIC const char* ERR_getErrorName(size_t code)
case PREFIX(dstSize_tooSmall): return "Destination buffer is too small"; case PREFIX(dstSize_tooSmall): return "Destination buffer is too small";
case PREFIX(srcSize_wrong): return "Src size incorrect"; case PREFIX(srcSize_wrong): return "Src size incorrect";
case PREFIX(corruption_detected): return "Corrupted block detected"; case PREFIX(corruption_detected): return "Corrupted block detected";
case PREFIX(tableLog_tooLarge): return "tableLog requires too much memory"; case PREFIX(tableLog_tooLarge): return "tableLog requires too much memory : unsupported";
case PREFIX(maxSymbolValue_tooLarge): return "Unsupported max possible Symbol Value : too large"; case PREFIX(maxSymbolValue_tooLarge): return "Unsupported max Symbol Value : too large";
case PREFIX(maxSymbolValue_tooSmall): return "Specified maxSymbolValue is too small"; case PREFIX(maxSymbolValue_tooSmall): return "Specified maxSymbolValue is too small";
case PREFIX(dictionary_corrupted): return "Dictionary is corrupted"; case PREFIX(dictionary_corrupted): return "Dictionary is corrupted";
case PREFIX(maxCode): case PREFIX(maxCode):
default: return notErrorCode; /* should be impossible, due to ERR_getError() */ default: return notErrorCode; /* impossible, due to ERR_getError() */
} }
} }

View File

@ -28,7 +28,7 @@
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
You can contact the author at : You can contact the author at :
- Source repository : https://github.com/Cyan4973/zstd - Homepage : http://www.zstd.net
****************************************************************** */ ****************************************************************** */
#ifndef ERROR_PUBLIC_H_MODULE #ifndef ERROR_PUBLIC_H_MODULE
#define ERROR_PUBLIC_H_MODULE #define ERROR_PUBLIC_H_MODULE
@ -60,8 +60,7 @@ typedef enum {
ZSTD_error_maxCode ZSTD_error_maxCode
} ZSTD_ErrorCode; } ZSTD_ErrorCode;
/* note : functions provide error codes in reverse negative order, /* note : compare with size_t function results using ZSTD_getError() */
so compare with (size_t)(0-enum) */
#if defined (__cplusplus) #if defined (__cplusplus)

View File

@ -2431,18 +2431,18 @@ static const ZSTD_parameters ZSTD_defaultParameters[4][ZSTD_MAX_CLEVEL+1] = {
/*! ZSTD_getParams() : /*! ZSTD_getParams() :
* @return ZSTD_parameters structure for a selected compression level and srcSize. * @return ZSTD_parameters structure for a selected compression level and srcSize.
* `srcSizeHint` value is optional, select 0 if not known */ * `srcSize` value is optional, select 0 if not known */
ZSTD_parameters ZSTD_getParams(int compressionLevel, U64 srcSizeHint) ZSTD_parameters ZSTD_getParams(int compressionLevel, U64 srcSize)
{ {
ZSTD_parameters result; ZSTD_parameters result;
int tableID = ((srcSizeHint-1) <= 256 KB) + ((srcSizeHint-1) <= 128 KB) + ((srcSizeHint-1) <= 16 KB); /* intentional underflow for srcSizeHint == 0 */ int tableID = ((srcSize-1) <= 256 KB) + ((srcSize-1) <= 128 KB) + ((srcSize-1) <= 16 KB); /* intentional underflow for srcSizeHint == 0 */
if (compressionLevel<=0) compressionLevel = 1; if (compressionLevel<=0) compressionLevel = 1;
if (compressionLevel > ZSTD_MAX_CLEVEL) compressionLevel = ZSTD_MAX_CLEVEL; if (compressionLevel > ZSTD_MAX_CLEVEL) compressionLevel = ZSTD_MAX_CLEVEL;
#if ZSTD_OPT_DEBUG >= 1 #if ZSTD_OPT_DEBUG >= 1
tableID=0; tableID=0;
#endif #endif
result = ZSTD_defaultParameters[tableID][compressionLevel]; result = ZSTD_defaultParameters[tableID][compressionLevel];
result.srcSize = srcSizeHint; result.srcSize = srcSize;
return result; return result;
} }

View File

@ -73,8 +73,7 @@ extern "C" {
/* from faster to stronger */ /* from faster to stronger */
typedef enum { ZSTD_fast, ZSTD_greedy, ZSTD_lazy, ZSTD_lazy2, ZSTD_btlazy2, ZSTD_btopt } ZSTD_strategy; typedef enum { ZSTD_fast, ZSTD_greedy, ZSTD_lazy, ZSTD_lazy2, ZSTD_btlazy2, ZSTD_btopt } ZSTD_strategy;
typedef struct typedef struct {
{
U64 srcSize; /* optional : tells how much bytes are present in the frame. Use 0 if not known. */ U64 srcSize; /* optional : tells how much bytes are present in the frame. Use 0 if not known. */
U32 windowLog; /* largest match distance : larger == more compression, more memory needed during decompression */ U32 windowLog; /* largest match distance : larger == more compression, more memory needed during decompression */
U32 contentLog; /* full search segment : larger == more compression, slower, more memory (useless for fast) */ U32 contentLog; /* full search segment : larger == more compression, slower, more memory (useless for fast) */
@ -245,7 +244,7 @@ size_t ZSTD_decompressBlock(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, cons
***************************************/ ***************************************/
#include "error_public.h" #include "error_public.h"
/*! ZSTD_getErrorCode() : /*! ZSTD_getErrorCode() :
convert a `size_t` function result into a `ZSTD_error_code` enum type, convert a `size_t` function result into a `ZSTD_ErrorCode` enum type,
which can be used to compare directly with enum list published into "error_public.h" */ which can be used to compare directly with enum list published into "error_public.h" */
ZSTD_ErrorCode ZSTD_getError(size_t code); ZSTD_ErrorCode ZSTD_getError(size_t code);

View File

@ -190,7 +190,6 @@ static int BMK_benchMem(const void* srcBuffer, size_t srcSize,
{ {
const size_t blockSize = (g_blockSize ? g_blockSize : srcSize) + (!srcSize); /* avoid div by 0 */ const size_t blockSize = (g_blockSize ? g_blockSize : srcSize) + (!srcSize); /* avoid div by 0 */
const U32 maxNbBlocks = (U32) ((srcSize + (blockSize-1)) / blockSize) + nbFiles; const U32 maxNbBlocks = (U32) ((srcSize + (blockSize-1)) / blockSize) + nbFiles;
size_t largestBlockSize = 0;
blockParam_t* const blockTable = (blockParam_t*) malloc(maxNbBlocks * sizeof(blockParam_t)); blockParam_t* const blockTable = (blockParam_t*) malloc(maxNbBlocks * sizeof(blockParam_t));
const size_t maxCompressedSize = ZSTD_compressBound(srcSize) + (maxNbBlocks * 1024); /* add some room for safety */ const size_t maxCompressedSize = ZSTD_compressBound(srcSize) + (maxNbBlocks * 1024); /* add some room for safety */
void* const compressedBuffer = malloc(maxCompressedSize); void* const compressedBuffer = malloc(maxCompressedSize);
@ -199,28 +198,27 @@ static int BMK_benchMem(const void* srcBuffer, size_t srcSize,
ZSTD_CCtx* ctx = ZSTD_createCCtx(); ZSTD_CCtx* ctx = ZSTD_createCCtx();
ZSTD_DCtx* refDCtx = ZSTD_createDCtx(); ZSTD_DCtx* refDCtx = ZSTD_createDCtx();
ZSTD_DCtx* dctx = ZSTD_createDCtx(); ZSTD_DCtx* dctx = ZSTD_createDCtx();
U64 crcOrig = XXH64(srcBuffer, srcSize, 0); U64 const crcOrig = XXH64(srcBuffer, srcSize, 0);
U32 nbBlocks = 0; U32 nbBlocks;
/* checks */
if (!compressedBuffer || !resultBuffer || !blockTable || !refCtx || !ctx || !refDCtx || !dctx)
EXM_THROW(31, "not enough memory");
/* init */ /* init */
if (strlen(displayName)>17) displayName += strlen(displayName)-17; /* can only display 17 characters */ if (strlen(displayName)>17) displayName += strlen(displayName)-17; /* can only display 17 characters */
/* Memory allocation & restrictions */
if (!compressedBuffer || !resultBuffer || !blockTable || !refCtx || !ctx || !refDCtx || !dctx)
EXM_THROW(31, "not enough memory");
/* Init blockTable data */ /* Init blockTable data */
{ { const char* srcPtr = (const char*)srcBuffer;
const char* srcPtr = (const char*)srcBuffer;
char* cPtr = (char*)compressedBuffer; char* cPtr = (char*)compressedBuffer;
char* resPtr = (char*)resultBuffer; char* resPtr = (char*)resultBuffer;
U32 fileNb; U32 fileNb;
for (fileNb=0; fileNb<nbFiles; fileNb++) { for (nbBlocks=0, fileNb=0; fileNb<nbFiles; fileNb++) {
size_t remaining = fileSizes[fileNb]; size_t remaining = fileSizes[fileNb];
U32 const nbBlocksforThisFile = (U32)((remaining + (blockSize-1)) / blockSize); U32 const nbBlocksforThisFile = (U32)((remaining + (blockSize-1)) / blockSize);
U32 const blockEnd = nbBlocks + nbBlocksforThisFile; U32 const blockEnd = nbBlocks + nbBlocksforThisFile;
for ( ; nbBlocks<blockEnd; nbBlocks++) { for ( ; nbBlocks<blockEnd; nbBlocks++) {
size_t thisBlockSize = MIN(remaining, blockSize); size_t const thisBlockSize = MIN(remaining, blockSize);
blockTable[nbBlocks].srcPtr = srcPtr; blockTable[nbBlocks].srcPtr = srcPtr;
blockTable[nbBlocks].cPtr = cPtr; blockTable[nbBlocks].cPtr = cPtr;
blockTable[nbBlocks].resPtr = resPtr; blockTable[nbBlocks].resPtr = resPtr;
@ -230,25 +228,22 @@ static int BMK_benchMem(const void* srcBuffer, size_t srcSize,
cPtr += blockTable[nbBlocks].cRoom; cPtr += blockTable[nbBlocks].cRoom;
resPtr += thisBlockSize; resPtr += thisBlockSize;
remaining -= thisBlockSize; remaining -= thisBlockSize;
if (thisBlockSize > largestBlockSize) largestBlockSize = thisBlockSize;
} } } } } }
/* warmimg up memory */ /* warmimg up memory */
RDG_genBuffer(compressedBuffer, maxCompressedSize, 0.10, 0.50, 1); RDG_genBuffer(compressedBuffer, maxCompressedSize, 0.10, 0.50, 1);
/* Bench */ /* Bench */
{ { size_t cSize = 0;
U32 loopNb;
size_t cSize = 0;
double fastestC = 100000000., fastestD = 100000000.; double fastestC = 100000000., fastestD = 100000000.;
double ratio = 0.; double ratio = 0.;
U64 crcCheck = 0; U64 crcCheck = 0;
clock_t coolTime = clock(); clock_t coolTime = clock();
U32 testNb;
DISPLAY("\r%79s\r", ""); DISPLAY("\r%79s\r", "");
for (loopNb = 1; loopNb <= (g_nbIterations + !g_nbIterations); loopNb++) { for (testNb = 1; testNb <= (g_nbIterations + !g_nbIterations); testNb++) {
int nbLoops; int nbLoops;
U32 blockNb;
clock_t clockStart, clockSpan; clock_t clockStart, clockSpan;
clock_t const clockLoop = g_nbIterations ? TIMELOOP_S * CLOCKS_PER_SEC : 10; clock_t const clockLoop = g_nbIterations ? TIMELOOP_S * CLOCKS_PER_SEC : 10;
@ -260,45 +255,43 @@ static int BMK_benchMem(const void* srcBuffer, size_t srcSize,
} }
/* Compression */ /* Compression */
DISPLAY("%2i-%-17.17s :%10u ->\r", loopNb, displayName, (U32)srcSize); DISPLAY("%2i-%-17.17s :%10u ->\r", testNb, displayName, (U32)srcSize);
memset(compressedBuffer, 0xE5, maxCompressedSize); /* warm up and erase result buffer */ memset(compressedBuffer, 0xE5, maxCompressedSize); /* warm up and erase result buffer */
nbLoops = 0;
clockStart = clock(); clockStart = clock();
while (clock() == clockStart); while (clock() == clockStart);
clockStart = clock(); clockStart = clock();
while (BMK_clockSpan(clockStart) < clockLoop) {
ZSTD_compressBegin_advanced(refCtx, dictBuffer, dictBufferSize, ZSTD_getParams(cLevel, MAX(dictBufferSize, largestBlockSize))); for (nbLoops = 0 ; BMK_clockSpan(clockStart) < clockLoop ; nbLoops++) {
U32 blockNb;
ZSTD_compressBegin_usingDict(refCtx, dictBuffer, dictBufferSize, cLevel);
for (blockNb=0; blockNb<nbBlocks; blockNb++) { for (blockNb=0; blockNb<nbBlocks; blockNb++) {
size_t rSize = ZSTD_compress_usingPreparedCCtx(ctx, refCtx, size_t const rSize = ZSTD_compress_usingPreparedCCtx(ctx, refCtx,
blockTable[blockNb].cPtr, blockTable[blockNb].cRoom, blockTable[blockNb].cPtr, blockTable[blockNb].cRoom,
blockTable[blockNb].srcPtr,blockTable[blockNb].srcSize); blockTable[blockNb].srcPtr,blockTable[blockNb].srcSize);
if (ZSTD_isError(rSize)) EXM_THROW(1, "ZSTD_compress_usingPreparedCCtx() failed : %s", ZSTD_getErrorName(rSize)); if (ZSTD_isError(rSize)) EXM_THROW(1, "ZSTD_compress_usingPreparedCCtx() failed : %s", ZSTD_getErrorName(rSize));
blockTable[blockNb].cSize = rSize; blockTable[blockNb].cSize = rSize;
} } }
nbLoops++;
}
clockSpan = BMK_clockSpan(clockStart); clockSpan = BMK_clockSpan(clockStart);
cSize = 0;
for (blockNb=0; blockNb<nbBlocks; blockNb++)
cSize += blockTable[blockNb].cSize;
if ((double)clockSpan < fastestC*nbLoops) fastestC = (double)clockSpan / nbLoops; if ((double)clockSpan < fastestC*nbLoops) fastestC = (double)clockSpan / nbLoops;
cSize = 0;
{ U32 blockNb; for (blockNb=0; blockNb<nbBlocks; blockNb++) cSize += blockTable[blockNb].cSize; }
ratio = (double)srcSize / (double)cSize; ratio = (double)srcSize / (double)cSize;
DISPLAY("%2i-%-17.17s :%10u ->%10u (%5.3f),%6.1f MB/s\r", DISPLAY("%2i-%-17.17s :%10u ->%10u (%5.3f),%6.1f MB/s\r",
loopNb, displayName, (U32)srcSize, (U32)cSize, ratio, testNb, displayName, (U32)srcSize, (U32)cSize, ratio,
(double)srcSize / 1000000. / (fastestC / CLOCKS_PER_SEC) ); (double)srcSize / 1000000. / (fastestC / CLOCKS_PER_SEC) );
#if 1 #if 1
/* Decompression */ /* Decompression */
memset(resultBuffer, 0xD6, srcSize); /* warm result buffer */ memset(resultBuffer, 0xD6, srcSize); /* warm result buffer */
nbLoops = 0;
clockStart = clock(); clockStart = clock();
while (clock() == clockStart); while (clock() == clockStart);
clockStart = clock(); clockStart = clock();
for ( ; BMK_clockSpan(clockStart) < clockLoop; nbLoops++) { for (nbLoops = 0 ; BMK_clockSpan(clockStart) < clockLoop ; nbLoops++) {
U32 blockNb;
ZSTD_decompressBegin_usingDict(refDCtx, dictBuffer, dictBufferSize); ZSTD_decompressBegin_usingDict(refDCtx, dictBuffer, dictBufferSize);
for (blockNb=0; blockNb<nbBlocks; blockNb++) { for (blockNb=0; blockNb<nbBlocks; blockNb++) {
size_t regenSize = ZSTD_decompress_usingPreparedDCtx(dctx, refDCtx, size_t regenSize = ZSTD_decompress_usingPreparedDCtx(dctx, refDCtx,
@ -315,7 +308,7 @@ static int BMK_benchMem(const void* srcBuffer, size_t srcSize,
clockSpan = BMK_clockSpan(clockStart); clockSpan = BMK_clockSpan(clockStart);
if ((double)clockSpan < fastestD*nbLoops) fastestD = (double)clockSpan / nbLoops; if ((double)clockSpan < fastestD*nbLoops) fastestD = (double)clockSpan / nbLoops;
DISPLAY("%2i-%-17.17s :%10u ->%10u (%5.3f),%6.1f MB/s ,%6.1f MB/s\r", DISPLAY("%2i-%-17.17s :%10u ->%10u (%5.3f),%6.1f MB/s ,%6.1f MB/s\r",
loopNb, displayName, (U32)srcSize, (U32)cSize, ratio, testNb, displayName, (U32)srcSize, (U32)cSize, ratio,
(double)srcSize / 1000000. / (fastestC / CLOCKS_PER_SEC), (double)srcSize / 1000000. / (fastestC / CLOCKS_PER_SEC),
(double)srcSize / 1000000. / (fastestD / CLOCKS_PER_SEC) ); (double)srcSize / 1000000. / (fastestD / CLOCKS_PER_SEC) );
@ -343,12 +336,11 @@ _findError:
printf("no difference detected\n"); printf("no difference detected\n");
} } } }
break; break;
} } /* if (crcOrig!=crcCheck) */
#endif #endif
} } /* for (testNb = 1; testNb <= (g_nbIterations + !g_nbIterations); testNb++) */
DISPLAY("%2i#\n", cLevel); DISPLAY("%2i#\n", cLevel);
} } /* Bench */
/* clean up */ /* clean up */
free(compressedBuffer); free(compressedBuffer);
@ -363,19 +355,20 @@ _findError:
static size_t BMK_findMaxMem(U64 requiredMem) static size_t BMK_findMaxMem(U64 requiredMem)
{ {
size_t step = 64 MB; size_t const step = 64 MB;
BYTE* testmem = NULL; BYTE* testmem = NULL;
requiredMem = (((requiredMem >> 26) + 1) << 26); requiredMem = (((requiredMem >> 26) + 1) << 26);
requiredMem += 2 * step; requiredMem += step;
if (requiredMem > maxMemory) requiredMem = maxMemory; if (requiredMem > maxMemory) requiredMem = maxMemory;
while (!testmem) { do {
requiredMem -= step;
testmem = (BYTE*)malloc((size_t)requiredMem); testmem = (BYTE*)malloc((size_t)requiredMem);
} requiredMem -= step;
} while (!testmem);
free(testmem); free(testmem);
return (size_t)(requiredMem - step); return (size_t)(requiredMem);
} }
static void BMK_benchCLevel(void* srcBuffer, size_t benchedSize, static void BMK_benchCLevel(void* srcBuffer, size_t benchedSize,