From: Yann Collet Date: Sun, 20 Mar 2016 14:46:10 +0000 (+0100) Subject: minor code refactor X-Git-Tag: v0.6.0^2~17^2~33 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=de406eebcd2330ada599d3c8d6e3192fff1abe0f;p=thirdparty%2Fzstd.git minor code refactor --- diff --git a/lib/error_private.h b/lib/error_private.h index c0c3f4900..ff0b829fc 100644 --- a/lib/error_private.h +++ b/lib/error_private.h @@ -28,7 +28,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 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 */ @@ -62,7 +62,7 @@ extern "C" { /*-**************************************** -* Customization +* Customization (error_public.h) ******************************************/ typedef ZSTD_ErrorCode ERR_enum; #define PREFIX(name) ZSTD_error_##name @@ -74,7 +74,7 @@ typedef ZSTD_ErrorCode ERR_enum; #ifdef ERROR # undef ERROR /* reported already defined on VS 2015 (Rich Geldreich) */ #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)); } @@ -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(srcSize_wrong): return "Src size incorrect"; case PREFIX(corruption_detected): return "Corrupted block detected"; - case PREFIX(tableLog_tooLarge): return "tableLog requires too much memory"; - case PREFIX(maxSymbolValue_tooLarge): return "Unsupported max possible Symbol Value : too large"; + case PREFIX(tableLog_tooLarge): return "tableLog requires too much memory : unsupported"; + case PREFIX(maxSymbolValue_tooLarge): return "Unsupported max Symbol Value : too large"; case PREFIX(maxSymbolValue_tooSmall): return "Specified maxSymbolValue is too small"; case PREFIX(dictionary_corrupted): return "Dictionary is corrupted"; case PREFIX(maxCode): - default: return notErrorCode; /* should be impossible, due to ERR_getError() */ + default: return notErrorCode; /* impossible, due to ERR_getError() */ } } diff --git a/lib/error_public.h b/lib/error_public.h index 655e28e0c..073b8c6a9 100644 --- a/lib/error_public.h +++ b/lib/error_public.h @@ -28,7 +28,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. You can contact the author at : - - Source repository : https://github.com/Cyan4973/zstd + - Homepage : http://www.zstd.net ****************************************************************** */ #ifndef ERROR_PUBLIC_H_MODULE #define ERROR_PUBLIC_H_MODULE @@ -60,8 +60,7 @@ typedef enum { ZSTD_error_maxCode } ZSTD_ErrorCode; -/* note : functions provide error codes in reverse negative order, - so compare with (size_t)(0-enum) */ +/* note : compare with size_t function results using ZSTD_getError() */ #if defined (__cplusplus) diff --git a/lib/zstd_compress.c b/lib/zstd_compress.c index 7147a329c..689d48feb 100644 --- a/lib/zstd_compress.c +++ b/lib/zstd_compress.c @@ -2431,18 +2431,18 @@ static const ZSTD_parameters ZSTD_defaultParameters[4][ZSTD_MAX_CLEVEL+1] = { /*! ZSTD_getParams() : * @return ZSTD_parameters structure for a selected compression level and srcSize. -* `srcSizeHint` value is optional, select 0 if not known */ -ZSTD_parameters ZSTD_getParams(int compressionLevel, U64 srcSizeHint) +* `srcSize` value is optional, select 0 if not known */ +ZSTD_parameters ZSTD_getParams(int compressionLevel, U64 srcSize) { 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 > ZSTD_MAX_CLEVEL) compressionLevel = ZSTD_MAX_CLEVEL; #if ZSTD_OPT_DEBUG >= 1 tableID=0; #endif result = ZSTD_defaultParameters[tableID][compressionLevel]; - result.srcSize = srcSizeHint; + result.srcSize = srcSize; return result; } diff --git a/lib/zstd_static.h b/lib/zstd_static.h index cb140a494..4ae771fde 100644 --- a/lib/zstd_static.h +++ b/lib/zstd_static.h @@ -73,8 +73,7 @@ extern "C" { /* from faster to stronger */ 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. */ 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) */ @@ -245,7 +244,7 @@ size_t ZSTD_decompressBlock(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, cons ***************************************/ #include "error_public.h" /*! 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" */ ZSTD_ErrorCode ZSTD_getError(size_t code); diff --git a/programs/bench.c b/programs/bench.c index 40be13f55..c74c03df2 100644 --- a/programs/bench.c +++ b/programs/bench.c @@ -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 U32 maxNbBlocks = (U32) ((srcSize + (blockSize-1)) / blockSize) + nbFiles; - size_t largestBlockSize = 0; 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 */ 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_DCtx* refDCtx = ZSTD_createDCtx(); ZSTD_DCtx* dctx = ZSTD_createDCtx(); - U64 crcOrig = XXH64(srcBuffer, srcSize, 0); - U32 nbBlocks = 0; + U64 const crcOrig = XXH64(srcBuffer, srcSize, 0); + U32 nbBlocks; - /* init */ - if (strlen(displayName)>17) displayName += strlen(displayName)-17; /* can only display 17 characters */ - - /* Memory allocation & restrictions */ + /* checks */ if (!compressedBuffer || !resultBuffer || !blockTable || !refCtx || !ctx || !refDCtx || !dctx) EXM_THROW(31, "not enough memory"); + /* init */ + if (strlen(displayName)>17) displayName += strlen(displayName)-17; /* can only display 17 characters */ + /* Init blockTable data */ - { - const char* srcPtr = (const char*)srcBuffer; + { const char* srcPtr = (const char*)srcBuffer; char* cPtr = (char*)compressedBuffer; char* resPtr = (char*)resultBuffer; U32 fileNb; - for (fileNb=0; fileNb largestBlockSize) largestBlockSize = thisBlockSize; } } } /* warmimg up memory */ RDG_genBuffer(compressedBuffer, maxCompressedSize, 0.10, 0.50, 1); /* Bench */ - { - U32 loopNb; - size_t cSize = 0; + { size_t cSize = 0; double fastestC = 100000000., fastestD = 100000000.; double ratio = 0.; U64 crcCheck = 0; clock_t coolTime = clock(); + U32 testNb; DISPLAY("\r%79s\r", ""); - for (loopNb = 1; loopNb <= (g_nbIterations + !g_nbIterations); loopNb++) { + for (testNb = 1; testNb <= (g_nbIterations + !g_nbIterations); testNb++) { int nbLoops; - U32 blockNb; clock_t clockStart, clockSpan; 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 */ - 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 */ - nbLoops = 0; clockStart = clock(); while (clock() == clockStart); 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%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) ); #if 1 /* Decompression */ memset(resultBuffer, 0xD6, srcSize); /* warm result buffer */ - nbLoops = 0; clockStart = clock(); while (clock() == clockStart); clockStart = clock(); - for ( ; BMK_clockSpan(clockStart) < clockLoop; nbLoops++) { + for (nbLoops = 0 ; BMK_clockSpan(clockStart) < clockLoop ; nbLoops++) { + U32 blockNb; ZSTD_decompressBegin_usingDict(refDCtx, dictBuffer, dictBufferSize); for (blockNb=0; blockNb%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. / (fastestD / CLOCKS_PER_SEC) ); @@ -343,12 +336,11 @@ _findError: printf("no difference detected\n"); } } break; - } + } /* if (crcOrig!=crcCheck) */ #endif - } - + } /* for (testNb = 1; testNb <= (g_nbIterations + !g_nbIterations); testNb++) */ DISPLAY("%2i#\n", cLevel); - } + } /* Bench */ /* clean up */ free(compressedBuffer); @@ -363,19 +355,20 @@ _findError: static size_t BMK_findMaxMem(U64 requiredMem) { - size_t step = 64 MB; + size_t const step = 64 MB; BYTE* testmem = NULL; requiredMem = (((requiredMem >> 26) + 1) << 26); - requiredMem += 2 * step; + requiredMem += step; if (requiredMem > maxMemory) requiredMem = maxMemory; - while (!testmem) { - requiredMem -= step; + do { testmem = (BYTE*)malloc((size_t)requiredMem); - } + requiredMem -= step; + } while (!testmem); + free(testmem); - return (size_t)(requiredMem - step); + return (size_t)(requiredMem); } static void BMK_benchCLevel(void* srcBuffer, size_t benchedSize,