multi_compress 0.6.1 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +41 -0
  3. data/GET_STARTED.md +4 -2
  4. data/README.md +1 -1
  5. data/THIRD_PARTY_NOTICES.md +2 -2
  6. data/ext/multi_compress/exports.darwin +1 -0
  7. data/ext/multi_compress/exports.map +4 -0
  8. data/ext/multi_compress/extconf.rb +20 -0
  9. data/ext/multi_compress/multi_compress.c +123 -73
  10. data/ext/multi_compress/vendor/.vendored +1 -1
  11. data/ext/multi_compress/vendor/zstd/lib/BUCK +10 -5
  12. data/ext/multi_compress/vendor/zstd/lib/README.md +25 -5
  13. data/ext/multi_compress/vendor/zstd/lib/common/allocations.h +6 -2
  14. data/ext/multi_compress/vendor/zstd/lib/common/bits.h +1 -1
  15. data/ext/multi_compress/vendor/zstd/lib/common/bitstream.h +3 -3
  16. data/ext/multi_compress/vendor/zstd/lib/common/bmi2.h +40 -0
  17. data/ext/multi_compress/vendor/zstd/lib/common/compiler.h +37 -11
  18. data/ext/multi_compress/vendor/zstd/lib/common/entropy_common.c +7 -6
  19. data/ext/multi_compress/vendor/zstd/lib/common/fse_decompress.c +4 -3
  20. data/ext/multi_compress/vendor/zstd/lib/common/pool.c +16 -19
  21. data/ext/multi_compress/vendor/zstd/lib/common/portability_macros.h +31 -0
  22. data/ext/multi_compress/vendor/zstd/lib/common/threading.c +16 -2
  23. data/ext/multi_compress/vendor/zstd/lib/common/zstd_common.c +9 -0
  24. data/ext/multi_compress/vendor/zstd/lib/common/zstd_internal.h +4 -2
  25. data/ext/multi_compress/vendor/zstd/lib/compress/hist.c +300 -3
  26. data/ext/multi_compress/vendor/zstd/lib/compress/hist.h +4 -0
  27. data/ext/multi_compress/vendor/zstd/lib/compress/huf_compress.c +4 -1
  28. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_compress.c +775 -214
  29. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_compress_internal.h +35 -6
  30. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_compress_sequences.c +9 -5
  31. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_compress_superblock.c +14 -5
  32. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_double_fast.c +2 -2
  33. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_fast.c +2 -2
  34. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_lazy.c +49 -5
  35. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_ldm.c +11 -11
  36. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_ldm.h +11 -0
  37. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_opt.c +16 -18
  38. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_preSplit.c +3 -3
  39. data/ext/multi_compress/vendor/zstd/lib/compress/zstdmt_compress.c +64 -39
  40. data/ext/multi_compress/vendor/zstd/lib/compress/zstdmt_compress.h +30 -2
  41. data/ext/multi_compress/vendor/zstd/lib/decompress/huf_decompress.c +78 -64
  42. data/ext/multi_compress/vendor/zstd/lib/decompress/huf_decompress_amd64.S +182 -18
  43. data/ext/multi_compress/vendor/zstd/lib/decompress/zstd_ddict.c +59 -27
  44. data/ext/multi_compress/vendor/zstd/lib/decompress/zstd_ddict.h +11 -0
  45. data/ext/multi_compress/vendor/zstd/lib/decompress/zstd_decompress.c +194 -93
  46. data/ext/multi_compress/vendor/zstd/lib/decompress/zstd_decompress_block.c +225 -85
  47. data/ext/multi_compress/vendor/zstd/lib/decompress/zstd_decompress_internal.h +19 -2
  48. data/ext/multi_compress/vendor/zstd/lib/dictBuilder/cover.c +136 -91
  49. data/ext/multi_compress/vendor/zstd/lib/dictBuilder/divsufsort.c +77 -77
  50. data/ext/multi_compress/vendor/zstd/lib/dictBuilder/fastcover.c +48 -42
  51. data/ext/multi_compress/vendor/zstd/lib/dictBuilder/zdict.c +18 -14
  52. data/ext/multi_compress/vendor/zstd/lib/dll/example/build_package.bat +66 -20
  53. data/ext/multi_compress/vendor/zstd/lib/dll/example/fullbench-dll.vcxproj +10 -6
  54. data/ext/multi_compress/vendor/zstd/lib/install_oses.mk +17 -0
  55. data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v01.c +10 -8
  56. data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v02.c +5 -364
  57. data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v03.c +4 -5
  58. data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v04.c +7 -3
  59. data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v05.c +8 -5
  60. data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v06.c +3 -3
  61. data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v07.c +1 -0
  62. data/ext/multi_compress/vendor/zstd/lib/libzstd.mk +8 -7
  63. data/ext/multi_compress/vendor/zstd/lib/libzstd.pc.in +1 -0
  64. data/ext/multi_compress/vendor/zstd/lib/module.modulemap +0 -21
  65. data/ext/multi_compress/vendor/zstd/lib/zstd.h +209 -112
  66. data/lib/multi_compress/reader/read_operations.rb +2 -0
  67. data/lib/multi_compress/version.rb +1 -1
  68. data/multi_compress.gemspec +2 -0
  69. data/mysql_udf/Makefile +12 -5
  70. data/mysql_udf/multi_compress_mysql.exports +6 -0
  71. data/postgres_extension/Makefile +2 -1
  72. data/postgres_extension/test/run_e2e.sh +28 -10
  73. metadata +6 -5
  74. data/ext/multi_compress/vendor/lz4/lib/Makefile +0 -244
  75. data/ext/multi_compress/vendor/lz4/lib/dll/example/Makefile +0 -63
  76. data/ext/multi_compress/vendor/zstd/lib/Makefile +0 -389
  77. data/ext/multi_compress/vendor/zstd/lib/dll/example/Makefile +0 -48
@@ -463,10 +463,7 @@ static void ZSTDMT_releaseCCtx(ZSTDMT_CCtxPool* pool, ZSTD_CCtx* cctx)
463
463
 
464
464
  /* ==== Serial State ==== */
465
465
 
466
- typedef struct {
467
- void const* start;
468
- size_t size;
469
- } Range;
466
+ typedef ZSTDMT_Range Range;
470
467
 
471
468
  typedef struct {
472
469
  /* All variables in the struct are protected by mutex. */
@@ -662,6 +659,7 @@ static const Range kNullRange = { NULL, 0 };
662
659
  typedef struct {
663
660
  size_t consumed; /* SHARED - set0 by mtctx, then modified by worker AND read by mtctx */
664
661
  size_t cSize; /* SHARED - set0 by mtctx, then modified by worker AND read by mtctx, then set0 by mtctx */
662
+ unsigned completed; /* SHARED - set0 by mtctx, then set1 by worker when it no longer uses the job */
665
663
  ZSTD_pthread_mutex_t job_mutex; /* Thread-safe - used by mtctx and worker */
666
664
  ZSTD_pthread_cond_t job_cond; /* Thread-safe - used by mtctx and worker */
667
665
  ZSTDMT_CCtxPool* cctxPool; /* Thread-safe - used by mtctx and (all) workers */
@@ -814,7 +812,8 @@ _endJob:
814
812
  ZSTD_PTHREAD_MUTEX_LOCK(&job->job_mutex);
815
813
  if (ZSTD_isError(job->cSize)) assert(lastCBlockSize == 0);
816
814
  job->cSize += lastCBlockSize;
817
- job->consumed = job->src.size; /* when job->consumed == job->src.size , compression job is presumed completed */
815
+ job->consumed = job->src.size;
816
+ job->completed = 1; /* also true for an empty job, unlike consumed == src.size */
818
817
  ZSTD_pthread_cond_signal(&job->job_cond);
819
818
  ZSTD_pthread_mutex_unlock(&job->job_mutex);
820
819
  }
@@ -1008,21 +1007,24 @@ static void ZSTDMT_releaseAllJobResources(ZSTDMT_CCtx* mtctx)
1008
1007
  {
1009
1008
  unsigned jobID;
1010
1009
  DEBUGLOG(3, "ZSTDMT_releaseAllJobResources");
1011
- for (jobID=0; jobID <= mtctx->jobIDMask; jobID++) {
1012
- /* Copy the mutex/cond out */
1013
- ZSTD_pthread_mutex_t const mutex = mtctx->jobs[jobID].job_mutex;
1014
- ZSTD_pthread_cond_t const cond = mtctx->jobs[jobID].job_cond;
1015
-
1016
- DEBUGLOG(4, "job%02u: release dst address %08X", jobID, (U32)(size_t)mtctx->jobs[jobID].dstBuff.start);
1017
- ZSTDMT_releaseBuffer(mtctx->bufPool, mtctx->jobs[jobID].dstBuff);
1018
-
1019
- /* Clear the job description, but keep the mutex/cond */
1020
- ZSTD_memset(&mtctx->jobs[jobID], 0, sizeof(mtctx->jobs[jobID]));
1021
- mtctx->jobs[jobID].job_mutex = mutex;
1022
- mtctx->jobs[jobID].job_cond = cond;
1010
+ if (mtctx->jobs) {
1011
+ for (jobID=0; jobID <= mtctx->jobIDMask; jobID++) {
1012
+ /* Copy the mutex/cond out */
1013
+ ZSTD_pthread_mutex_t const mutex = mtctx->jobs[jobID].job_mutex;
1014
+ ZSTD_pthread_cond_t const cond = mtctx->jobs[jobID].job_cond;
1015
+
1016
+ DEBUGLOG(4, "job%02u: release dst address %08X", jobID, (U32)(size_t)mtctx->jobs[jobID].dstBuff.start);
1017
+ ZSTDMT_releaseBuffer(mtctx->bufPool, mtctx->jobs[jobID].dstBuff);
1018
+
1019
+ /* Clear the job description, but keep the mutex/cond */
1020
+ ZSTD_memset(&mtctx->jobs[jobID], 0, sizeof(mtctx->jobs[jobID]));
1021
+ mtctx->jobs[jobID].job_mutex = mutex;
1022
+ mtctx->jobs[jobID].job_cond = cond;
1023
+ }
1023
1024
  }
1024
1025
  mtctx->inBuff.buffer = g_nullBuffer;
1025
1026
  mtctx->inBuff.filled = 0;
1027
+ mtctx->jobReady = 0; /* the prepared job was cleared above */
1026
1028
  mtctx->allJobsCompleted = 1;
1027
1029
  }
1028
1030
 
@@ -1032,7 +1034,7 @@ static void ZSTDMT_waitForAllJobsCompleted(ZSTDMT_CCtx* mtctx)
1032
1034
  while (mtctx->doneJobID < mtctx->nextJobID) {
1033
1035
  unsigned const jobID = mtctx->doneJobID & mtctx->jobIDMask;
1034
1036
  ZSTD_PTHREAD_MUTEX_LOCK(&mtctx->jobs[jobID].job_mutex);
1035
- while (mtctx->jobs[jobID].consumed < mtctx->jobs[jobID].src.size) {
1037
+ while (!mtctx->jobs[jobID].completed) {
1036
1038
  DEBUGLOG(4, "waiting for jobCompleted signal from job %u", mtctx->doneJobID); /* we want to block when waiting for data to flush */
1037
1039
  ZSTD_pthread_cond_wait(&mtctx->jobs[jobID].job_cond, &mtctx->jobs[jobID].job_mutex);
1038
1040
  }
@@ -1041,9 +1043,18 @@ static void ZSTDMT_waitForAllJobsCompleted(ZSTDMT_CCtx* mtctx)
1041
1043
  }
1042
1044
  }
1043
1045
 
1046
+ void ZSTDMT_waitForPendingJobs(ZSTDMT_CCtx* mtctx)
1047
+ {
1048
+ if (mtctx->allJobsCompleted == 0) { /* previous compression not correctly finished */
1049
+ ZSTDMT_waitForAllJobsCompleted(mtctx);
1050
+ ZSTDMT_releaseAllJobResources(mtctx); /* Will set allJobsCompleted to 1 */
1051
+ }
1052
+ }
1053
+
1044
1054
  size_t ZSTDMT_freeCCtx(ZSTDMT_CCtx* mtctx)
1045
1055
  {
1046
1056
  if (mtctx==NULL) return 0; /* compatible with free on NULL */
1057
+ ZSTDMT_waitForPendingJobs(mtctx); /* a shared pool is not stopped below */
1047
1058
  if (!mtctx->providedFactory)
1048
1059
  POOL_free(mtctx->factory); /* stop and free worker threads */
1049
1060
  ZSTDMT_releaseAllJobResources(mtctx); /* release job resources into pools first */
@@ -1077,7 +1088,9 @@ size_t ZSTDMT_sizeof_CCtx(ZSTDMT_CCtx* mtctx)
1077
1088
  * @return : error code if fails, 0 on success */
1078
1089
  static size_t ZSTDMT_resize(ZSTDMT_CCtx* mtctx, unsigned nbWorkers)
1079
1090
  {
1080
- if (POOL_resize(mtctx->factory, nbWorkers)) return ERROR(memory_allocation);
1091
+ if (!mtctx->providedFactory) { /* a pool provided by the application keeps its size */
1092
+ if (POOL_resize(mtctx->factory, nbWorkers)) return ERROR(memory_allocation);
1093
+ }
1081
1094
  FORWARD_IF_ERROR( ZSTDMT_expandJobsTable(mtctx, nbWorkers) , "");
1082
1095
  mtctx->bufPool = ZSTDMT_expandBufferPool(mtctx->bufPool, BUF_POOL_MAX_NB_BUFFERS(nbWorkers));
1083
1096
  if (mtctx->bufPool == NULL) return ERROR(memory_allocation);
@@ -1260,18 +1273,13 @@ size_t ZSTDMT_initCStream_internal(
1260
1273
  assert(!((dict) && (cdict))); /* either dict or cdict, not both */
1261
1274
 
1262
1275
  /* init */
1276
+ ZSTDMT_waitForPendingJobs(mtctx); /* before resizing the pools they use */
1263
1277
  if (params.nbWorkers != mtctx->params.nbWorkers)
1264
1278
  FORWARD_IF_ERROR( ZSTDMT_resize(mtctx, (unsigned)params.nbWorkers) , "");
1265
1279
 
1266
1280
  if (params.jobSize != 0 && params.jobSize < ZSTDMT_JOBSIZE_MIN) params.jobSize = ZSTDMT_JOBSIZE_MIN;
1267
1281
  if (params.jobSize > (size_t)ZSTDMT_JOBSIZE_MAX) params.jobSize = (size_t)ZSTDMT_JOBSIZE_MAX;
1268
1282
 
1269
- if (mtctx->allJobsCompleted == 0) { /* previous compression not correctly finished */
1270
- ZSTDMT_waitForAllJobsCompleted(mtctx);
1271
- ZSTDMT_releaseAllJobResources(mtctx);
1272
- mtctx->allJobsCompleted = 1;
1273
- }
1274
-
1275
1283
  mtctx->params = params;
1276
1284
  mtctx->frameContentSize = pledgedSrcSize;
1277
1285
  ZSTD_freeCDict(mtctx->cdictLocal);
@@ -1320,11 +1328,19 @@ size_t ZSTDMT_initCStream_internal(
1320
1328
  * with the LDM window.
1321
1329
  */
1322
1330
  size_t const nbSlackBuffers = 2 + (mtctx->targetPrefixSize > 0);
1323
- size_t const slackSize = mtctx->targetSectionSize * nbSlackBuffers;
1331
+ U64 const slackSize = (U64)mtctx->targetSectionSize * nbSlackBuffers;
1324
1332
  /* Compute the total size, and always have enough slack */
1325
- size_t const nbWorkers = MAX(mtctx->params.nbWorkers, 1);
1326
- size_t const sectionsSize = mtctx->targetSectionSize * nbWorkers;
1327
- size_t const capacity = MAX(windowSize, sectionsSize) + slackSize;
1333
+ U64 const nbWorkers = MAX(mtctx->params.nbWorkers, 1);
1334
+ U64 const sectionsSize = (U64)mtctx->targetSectionSize * nbWorkers;
1335
+ U64 const capacity64 = MAX((U64)windowSize, sectionsSize) + slackSize;
1336
+ size_t capacity;
1337
+ /* The totals are computed in 64 bits because on 32-bit builds they
1338
+ * can exceed the range of size_t. They would wrap, and the round
1339
+ * buffer would be too small (or not allocated at all), letting
1340
+ * input be copied past its end. Reject such sizes instead. */
1341
+ capacity = (size_t)capacity64;
1342
+ RETURN_ERROR_IF((U64)capacity != capacity64,
1343
+ memory_allocation, "round buffer size overflow");
1328
1344
  if (mtctx->roundBuff.capacity < capacity) {
1329
1345
  if (mtctx->roundBuff.buffer)
1330
1346
  ZSTD_customFree(mtctx->roundBuff.buffer, mtctx->cMem);
@@ -1388,6 +1404,7 @@ static void ZSTDMT_writeLastEmptyBlock(ZSTDMT_jobDescription* job)
1388
1404
  assert(job->src.size == 0); /* last job is empty -> will be simplified into a last empty block */
1389
1405
  assert(job->firstJob == 0); /* cannot be first job, as it also needs to create frame header */
1390
1406
  assert(job->dstBuff.start == NULL); /* invoked from streaming variant only (otherwise, dstBuff might be user's output) */
1407
+ job->completed = 1; /* written here, never posted to a worker */
1391
1408
  job->dstBuff = ZSTDMT_getBuffer(job->bufPool);
1392
1409
  if (job->dstBuff.start == NULL) {
1393
1410
  job->cSize = ERROR(memory_allocation);
@@ -1421,6 +1438,7 @@ static size_t ZSTDMT_createCompressionJob(ZSTDMT_CCtx* mtctx, size_t srcSize, ZS
1421
1438
  mtctx->jobs[jobID].prefix = mtctx->inBuff.prefix;
1422
1439
  mtctx->jobs[jobID].consumed = 0;
1423
1440
  mtctx->jobs[jobID].cSize = 0;
1441
+ mtctx->jobs[jobID].completed = 0;
1424
1442
  mtctx->jobs[jobID].params = mtctx->params;
1425
1443
  mtctx->jobs[jobID].cdict = mtctx->nextJobID==0 ? mtctx->cdict : NULL;
1426
1444
  mtctx->jobs[jobID].fullFrameSize = mtctx->frameContentSize;
@@ -1572,9 +1590,8 @@ static size_t ZSTDMT_flushProduced(ZSTDMT_CCtx* mtctx, ZSTD_outBuffer* output, u
1572
1590
  }
1573
1591
 
1574
1592
  /**
1575
- * Returns the range of data used by the earliest job that is not yet complete.
1576
- * If the data of the first job is broken up into two segments, we cover both
1577
- * sections.
1593
+ * Returns the oldest input-ring range used by a job that is not yet complete.
1594
+ * Protecting this range prevents ring reuse from reaching the job's source.
1578
1595
  */
1579
1596
  static Range ZSTDMT_getInputDataInUse(ZSTDMT_CCtx* mtctx)
1580
1597
  {
@@ -1596,11 +1613,10 @@ static Range ZSTDMT_getInputDataInUse(ZSTDMT_CCtx* mtctx)
1596
1613
  ZSTD_pthread_mutex_unlock(&mtctx->jobs[wJobID].job_mutex);
1597
1614
 
1598
1615
  if (consumed < mtctx->jobs[wJobID].src.size) {
1599
- Range range = mtctx->jobs[wJobID].prefix;
1600
- if (range.size == 0) {
1601
- /* Empty prefix */
1602
- range = mtctx->jobs[wJobID].src;
1603
- }
1616
+ Range const range = ZSTDMT_getJobInputRange(
1617
+ mtctx->jobs[wJobID].prefix,
1618
+ mtctx->jobs[wJobID].src,
1619
+ mtctx->jobs[wJobID].firstJob);
1604
1620
  /* Job source in multiple segments not supported yet */
1605
1621
  assert(range.start <= mtctx->jobs[wJobID].src.start);
1606
1622
  return range;
@@ -1846,6 +1862,11 @@ size_t ZSTDMT_nextInputSizeHint(const ZSTDMT_CCtx* mtctx)
1846
1862
  return hintInSize;
1847
1863
  }
1848
1864
 
1865
+ unsigned ZSTDMT_isFrameEnded(const ZSTDMT_CCtx* mtctx)
1866
+ {
1867
+ return mtctx->frameEnded;
1868
+ }
1869
+
1849
1870
  /** ZSTDMT_compressStream_generic() :
1850
1871
  * internal use only - exposed to be invoked from zstd_compress.c
1851
1872
  * assumption : output and input are valid (pos <= size)
@@ -1866,8 +1887,12 @@ size_t ZSTDMT_compressStream_generic(ZSTDMT_CCtx* mtctx,
1866
1887
  return ERROR(stage_wrong);
1867
1888
  }
1868
1889
 
1869
- /* fill input buffer */
1890
+ /* fill input buffer.
1891
+ * Once the last job of the frame is created, remaining input belongs to the next frame :
1892
+ * it is left untouched, like the single-thread path does. No job can follow the last one :
1893
+ * it could wait forever at the serial gate, behind an empty last job written inline. */
1870
1894
  if ( (!mtctx->jobReady)
1895
+ && (!mtctx->frameEnded)
1871
1896
  && (input->size > input->pos) ) { /* support NULL input */
1872
1897
  if (mtctx->inBuff.buffer.start == NULL) {
1873
1898
  assert(mtctx->inBuff.filled == 0); /* Can't fill an empty buffer */
@@ -1916,7 +1941,7 @@ size_t ZSTDMT_compressStream_generic(ZSTDMT_CCtx* mtctx,
1916
1941
 
1917
1942
  /* check for potential compressed data ready to be flushed */
1918
1943
  { size_t const remainingToFlush = ZSTDMT_flushProduced(mtctx, output, !forwardInputProgress, endOp); /* block if there was no forward input progress */
1919
- if (input->pos < input->size) return MAX(remainingToFlush, 1); /* input not consumed : do not end flush yet */
1944
+ if ((input->pos < input->size) && (!mtctx->frameEnded)) return MAX(remainingToFlush, 1); /* input not consumed : do not end flush yet */
1920
1945
  DEBUGLOG(5, "end of ZSTDMT_compressStream_generic: remainingToFlush = %u", (U32)remainingToFlush);
1921
1946
  return remainingToFlush;
1922
1947
  }
@@ -12,6 +12,7 @@
12
12
  #define ZSTDMT_COMPRESS_H
13
13
 
14
14
  /* === Dependencies === */
15
+ #include "../common/compiler.h" /* MEM_STATIC */
15
16
  #include "../common/zstd_deps.h" /* size_t */
16
17
  #define ZSTD_STATIC_LINKING_ONLY /* ZSTD_parameters */
17
18
  #include "../zstd.h" /* ZSTD_inBuffer, ZSTD_outBuffer, ZSTDLIB_API */
@@ -32,8 +33,8 @@
32
33
  #ifndef ZSTDMT_JOBSIZE_MIN /* a different value can be selected at compile time */
33
34
  # define ZSTDMT_JOBSIZE_MIN (512 KB)
34
35
  #endif
35
- #define ZSTDMT_JOBLOG_MAX (MEM_32bits() ? 29 : 30)
36
- #define ZSTDMT_JOBSIZE_MAX (MEM_32bits() ? (512 MB) : (1024 MB))
36
+ #define ZSTDMT_JOBLOG_MAX (MEM_32bits() ? 28 : 30)
37
+ #define ZSTDMT_JOBSIZE_MAX (MEM_32bits() ? (256 MB) : (1024 MB))
37
38
 
38
39
 
39
40
  /* ========================================================
@@ -41,6 +42,22 @@
41
42
  * === Not exposed in libzstd. Never invoke directly ===
42
43
  * ======================================================== */
43
44
 
45
+ typedef struct {
46
+ void const* start;
47
+ size_t size;
48
+ } ZSTDMT_Range;
49
+
50
+ /* Select the oldest input-ring range used by an unfinished job. */
51
+ MEM_STATIC ZSTDMT_Range ZSTDMT_getJobInputRange(ZSTDMT_Range prefix,
52
+ ZSTDMT_Range src,
53
+ unsigned firstJob)
54
+ {
55
+ /* The first job's prefix may be an external raw dictionary, which does
56
+ * not protect its source in the ring. Later prefixes are in the ring,
57
+ * immediately before the source, so protecting them also protects it. */
58
+ return firstJob || prefix.size == 0 ? src : prefix;
59
+ }
60
+
44
61
  /* === Memory management === */
45
62
  typedef struct ZSTDMT_CCtx_s ZSTDMT_CCtx;
46
63
  /* Requires ZSTD_MULTITHREAD to be defined during compilation, otherwise it will return NULL. */
@@ -49,12 +66,23 @@ ZSTDMT_CCtx* ZSTDMT_createCCtx_advanced(unsigned nbWorkers,
49
66
  ZSTD_threadPool *pool);
50
67
  size_t ZSTDMT_freeCCtx(ZSTDMT_CCtx* mtctx);
51
68
 
69
+ /*! ZSTDMT_waitForPendingJobs() :
70
+ * Waits for the jobs of an unfinished frame to complete, then releases their resources.
71
+ * Must be called before freeing anything these jobs reference, such as the dictionary.
72
+ * Does nothing if the last frame was completed. */
73
+ void ZSTDMT_waitForPendingJobs(ZSTDMT_CCtx* mtctx);
74
+
52
75
  size_t ZSTDMT_sizeof_CCtx(ZSTDMT_CCtx* mtctx);
53
76
 
54
77
  /* === Streaming functions === */
55
78
 
56
79
  size_t ZSTDMT_nextInputSizeHint(const ZSTDMT_CCtx* mtctx);
57
80
 
81
+ /*! ZSTDMT_isFrameEnded() :
82
+ * @return 1 once the last job of the current frame is created :
83
+ * remaining input then belongs to the next frame. */
84
+ unsigned ZSTDMT_isFrameEnded(const ZSTDMT_CCtx* mtctx);
85
+
58
86
  /*! ZSTDMT_initCStream_internal() :
59
87
  * Private use only. Init streaming operation.
60
88
  * expects params to be valid.
@@ -15,7 +15,9 @@
15
15
  /* **************************************************************
16
16
  * Dependencies
17
17
  ****************************************************************/
18
+ #include <stddef.h> /* size_t */
18
19
  #include "../common/zstd_deps.h" /* ZSTD_memcpy, ZSTD_memset */
20
+ #include "../common/bmi2.h"
19
21
  #include "../common/compiler.h"
20
22
  #include "../common/bitstream.h" /* BIT_* */
21
23
  #include "../common/fse.h" /* to compress headers */
@@ -116,9 +118,10 @@ typedef size_t (*HUF_DecompressUsingDTableFn)(void *dst, size_t dstSize,
116
118
  static size_t fn(void* dst, size_t dstSize, void const* cSrc, \
117
119
  size_t cSrcSize, HUF_DTable const* DTable, int flags) \
118
120
  { \
119
- if (flags & HUF_flags_bmi2) { \
121
+ if (ZSTD_USE_BMI2(flags & HUF_flags_bmi2)) { \
120
122
  return fn##_bmi2(dst, dstSize, cSrc, cSrcSize, DTable); \
121
123
  } \
124
+ (void)flags; \
122
125
  return fn##_default(dst, dstSize, cSrc, cSrcSize, DTable); \
123
126
  }
124
127
 
@@ -195,7 +198,7 @@ static size_t HUF_DecompressFastArgs_init(HUF_DecompressFastArgs* args, void* ds
195
198
 
196
199
  const BYTE* const istart = (const BYTE*)src;
197
200
 
198
- BYTE* const oend = ZSTD_maybeNullPtrAdd((BYTE*)dst, dstSize);
201
+ BYTE* const oend = (BYTE*)ZSTD_maybeNullPtrAdd(dst, (ptrdiff_t)dstSize);
199
202
 
200
203
  /* The fast decoding loop assumes 64-bit little-endian.
201
204
  * This condition is false on x32.
@@ -283,12 +286,13 @@ static size_t HUF_initRemainingDStream(BIT_DStream_t* bit, HUF_DecompressFastArg
283
286
  /* Validate that we haven't overwritten. */
284
287
  if (args->op[stream] > segmentEnd)
285
288
  return ERROR(corruption_detected);
286
- /* Validate that we haven't read beyond iend[].
287
- * Note that ip[] may be < iend[] because the MSB is
288
- * the next bit to read, and we may have consumed 100%
289
- * of the stream, so down to iend[i] - 8 is valid.
290
- */
291
- if (args->ip[stream] < args->iend[stream] - 8)
289
+ /* The MSB is the next bit to read, so ip[] may be up to 8 bytes before
290
+ * iend[] when the stream is fully consumed. Compare distances rather than
291
+ * forming iend[0] - 8, which can be before the compressed input.
292
+ * No loaded word may precede ilowest. */
293
+ if (args->ip[stream] < args->ilowest
294
+ || (args->ip[stream] < args->iend[stream]
295
+ && (size_t)(args->iend[stream] - args->ip[stream]) > 8))
292
296
  return ERROR(corruption_detected);
293
297
 
294
298
  /* Construct the BIT_DStream_t. */
@@ -578,7 +582,7 @@ HUF_decompress1X1_usingDTable_internal_body(
578
582
  const HUF_DTable* DTable)
579
583
  {
580
584
  BYTE* op = (BYTE*)dst;
581
- BYTE* const oend = ZSTD_maybeNullPtrAdd(op, dstSize);
585
+ BYTE* const oend = (BYTE*)ZSTD_maybeNullPtrAdd(op, (ptrdiff_t)dstSize);
582
586
  const void* dtPtr = DTable + 1;
583
587
  const HUF_DEltX1* const dt = (const HUF_DEltX1*)dtPtr;
584
588
  BIT_DStream_t bitD;
@@ -703,6 +707,8 @@ size_t HUF_decompress4X1_usingDTable_internal_bmi2(void* dst, size_t dstSize, vo
703
707
  size_t cSrcSize, HUF_DTable const* DTable) {
704
708
  return HUF_decompress4X1_usingDTable_internal_body(dst, dstSize, cSrc, cSrcSize, DTable);
705
709
  }
710
+ #else
711
+ #define HUF_decompress4X1_usingDTable_internal_bmi2 HUF_decompress4X1_usingDTable_internal_default
706
712
  #endif
707
713
 
708
714
  static
@@ -784,19 +790,19 @@ void HUF_decompress4X1_usingDTable_internal_fast_c_loop(HUF_DecompressFastArgs*
784
790
  }
785
791
  #endif
786
792
 
787
- #define HUF_4X1_DECODE_SYMBOL(_stream, _symbol) \
788
- do { \
789
- int const index = (int)(bits[(_stream)] >> 53); \
790
- int const entry = (int)dtable[index]; \
791
- bits[(_stream)] <<= (entry & 0x3F); \
792
- op[(_stream)][(_symbol)] = (BYTE)((entry >> 8) & 0xFF); \
793
+ #define HUF_4X1_DECODE_SYMBOL(_stream, _symbol) \
794
+ do { \
795
+ U64 const index = bits[(_stream)] >> 53; \
796
+ U16 const entry = dtable[index]; \
797
+ bits[(_stream)] <<= entry & 0x3F; \
798
+ op[(_stream)][(_symbol)] = (BYTE)(entry >> 8); \
793
799
  } while (0)
794
800
 
795
- #define HUF_4X1_RELOAD_STREAM(_stream) \
801
+ #define HUF_5X1_RELOAD_STREAM(_stream) \
796
802
  do { \
797
- int const ctz = ZSTD_countTrailingZeros64(bits[(_stream)]); \
798
- int const nbBits = ctz & 7; \
799
- int const nbBytes = ctz >> 3; \
803
+ U64 const ctz = ZSTD_countTrailingZeros64(bits[(_stream)]); \
804
+ U64 const nbBits = ctz & 7; \
805
+ U64 const nbBytes = ctz >> 3; \
800
806
  op[(_stream)] += 5; \
801
807
  ip[(_stream)] -= nbBytes; \
802
808
  bits[(_stream)] = MEM_read64(ip[(_stream)]) | 1; \
@@ -815,11 +821,11 @@ void HUF_decompress4X1_usingDTable_internal_fast_c_loop(HUF_DecompressFastArgs*
815
821
  HUF_4X_FOR_EACH_STREAM_WITH_VAR(HUF_4X1_DECODE_SYMBOL, 4);
816
822
 
817
823
  /* Reload each of the 4 the bitstreams */
818
- HUF_4X_FOR_EACH_STREAM(HUF_4X1_RELOAD_STREAM);
824
+ HUF_4X_FOR_EACH_STREAM(HUF_5X1_RELOAD_STREAM);
819
825
  } while (op[3] < olimit);
820
826
 
821
827
  #undef HUF_4X1_DECODE_SYMBOL
822
- #undef HUF_4X1_RELOAD_STREAM
828
+ #undef HUF_5X1_RELOAD_STREAM
823
829
  }
824
830
 
825
831
  _out:
@@ -845,7 +851,7 @@ HUF_decompress4X1_usingDTable_internal_fast(
845
851
  {
846
852
  void const* dt = DTable + 1;
847
853
  BYTE const* const ilowest = (BYTE const*)cSrc;
848
- BYTE* const oend = ZSTD_maybeNullPtrAdd((BYTE*)dst, dstSize);
854
+ BYTE* const oend = (BYTE*)ZSTD_maybeNullPtrAdd(dst, (ptrdiff_t)dstSize);
849
855
  HUF_DecompressFastArgs args;
850
856
  { size_t const ret = HUF_DecompressFastArgs_init(&args, dst, dstSize, cSrc, cSrcSize, DTable);
851
857
  FORWARD_IF_ERROR(ret, "Failed to init fast loop args");
@@ -900,18 +906,16 @@ static size_t HUF_decompress4X1_usingDTable_internal(void* dst, size_t dstSize,
900
906
  HUF_DecompressUsingDTableFn fallbackFn = HUF_decompress4X1_usingDTable_internal_default;
901
907
  HUF_DecompressFastLoopFn loopFn = HUF_decompress4X1_usingDTable_internal_fast_c_loop;
902
908
 
903
- #if DYNAMIC_BMI2
904
- if (flags & HUF_flags_bmi2) {
909
+ if (ZSTD_USE_BMI2(flags & HUF_flags_bmi2)) {
905
910
  fallbackFn = HUF_decompress4X1_usingDTable_internal_bmi2;
906
911
  # if ZSTD_ENABLE_ASM_X86_64_BMI2
907
912
  if (!(flags & HUF_flags_disableAsm)) {
908
913
  loopFn = HUF_decompress4X1_usingDTable_internal_fast_asm_loop;
909
914
  }
910
915
  # endif
911
- } else {
916
+ } else if (DYNAMIC_BMI2) {
912
917
  return fallbackFn(dst, dstSize, cSrc, cSrcSize, DTable);
913
918
  }
914
- #endif
915
919
 
916
920
  #if ZSTD_ENABLE_ASM_X86_64_BMI2 && defined(__BMI2__)
917
921
  if (!(flags & HUF_flags_disableAsm)) {
@@ -1314,7 +1318,9 @@ HUF_decodeStreamX2(BYTE* p, BIT_DStream_t* bitDPtr, BYTE* const pEnd,
1314
1318
  if ((size_t)(pEnd - p) >= sizeof(bitDPtr->bitContainer)) {
1315
1319
  if (dtLog <= 11 && MEM_64bits()) {
1316
1320
  /* up to 10 symbols at a time */
1317
- while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) & (p < pEnd-9)) {
1321
+ /* For an 8-byte output, pEnd - 9 would precede the buffer. */
1322
+ BYTE* const pLimit = (size_t)(pEnd - p) >= 10 ? pEnd - 9 : p;
1323
+ while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) & (p < pLimit)) {
1318
1324
  HUF_DECODE_SYMBOLX2_0(p, bitDPtr);
1319
1325
  HUF_DECODE_SYMBOLX2_0(p, bitDPtr);
1320
1326
  HUF_DECODE_SYMBOLX2_0(p, bitDPtr);
@@ -1362,7 +1368,7 @@ HUF_decompress1X2_usingDTable_internal_body(
1362
1368
 
1363
1369
  /* decode */
1364
1370
  { BYTE* const ostart = (BYTE*) dst;
1365
- BYTE* const oend = ZSTD_maybeNullPtrAdd(ostart, dstSize);
1371
+ BYTE* const oend = (BYTE*)ZSTD_maybeNullPtrAdd(ostart, (ptrdiff_t)dstSize);
1366
1372
  const void* const dtPtr = DTable+1; /* force compiler to not use strict-aliasing */
1367
1373
  const HUF_DEltX2* const dt = (const HUF_DEltX2*)dtPtr;
1368
1374
  DTableDesc const dtd = HUF_getDTableDesc(DTable);
@@ -1506,6 +1512,8 @@ size_t HUF_decompress4X2_usingDTable_internal_bmi2(void* dst, size_t dstSize, vo
1506
1512
  size_t cSrcSize, HUF_DTable const* DTable) {
1507
1513
  return HUF_decompress4X2_usingDTable_internal_body(dst, dstSize, cSrc, cSrcSize, DTable);
1508
1514
  }
1515
+ #else
1516
+ #define HUF_decompress4X2_usingDTable_internal_bmi2 HUF_decompress4X2_usingDTable_internal_default
1509
1517
  #endif
1510
1518
 
1511
1519
  static
@@ -1602,57 +1610,65 @@ void HUF_decompress4X2_usingDTable_internal_fast_c_loop(HUF_DecompressFastArgs*
1602
1610
  }
1603
1611
  #endif
1604
1612
 
1605
- #define HUF_4X2_DECODE_SYMBOL(_stream, _decode3) \
1606
- do { \
1607
- if ((_decode3) || (_stream) != 3) { \
1608
- int const index = (int)(bits[(_stream)] >> 53); \
1609
- HUF_DEltX2 const entry = dtable[index]; \
1610
- MEM_write16(op[(_stream)], entry.sequence); \
1611
- bits[(_stream)] <<= (entry.nbBits) & 0x3F; \
1612
- op[(_stream)] += (entry.length); \
1613
- } \
1613
+ #define HUF_4X2_DECODE_SYMBOL(_stream, _decode3) \
1614
+ do { \
1615
+ if ((_decode3) || (_stream) != 3) { \
1616
+ U64 const index = bits[(_stream)] >> 53; \
1617
+ size_t const entry = MEM_readLE32(&dtable[index]); \
1618
+ MEM_write16(op[(_stream)], (U16)entry); \
1619
+ bits[(_stream)] <<= (entry >> 16) & 0x3F; \
1620
+ op[(_stream)] += entry >> 24; \
1621
+ } \
1614
1622
  } while (0)
1615
1623
 
1616
- #define HUF_4X2_RELOAD_STREAM(_stream) \
1624
+ #define HUF_5X2_RELOAD_STREAM(_stream, _decode3) \
1617
1625
  do { \
1618
- HUF_4X2_DECODE_SYMBOL(3, 1); \
1626
+ if (_decode3) HUF_4X2_DECODE_SYMBOL(3, 1); \
1619
1627
  { \
1620
- int const ctz = ZSTD_countTrailingZeros64(bits[(_stream)]); \
1621
- int const nbBits = ctz & 7; \
1622
- int const nbBytes = ctz >> 3; \
1628
+ U64 const ctz = ZSTD_countTrailingZeros64(bits[(_stream)]); \
1629
+ U64 const nbBits = ctz & 7; \
1630
+ U64 const nbBytes = ctz >> 3; \
1623
1631
  ip[(_stream)] -= nbBytes; \
1624
1632
  bits[(_stream)] = MEM_read64(ip[(_stream)]) | 1; \
1625
1633
  bits[(_stream)] <<= nbBits; \
1626
1634
  } \
1627
1635
  } while (0)
1628
1636
 
1637
+ #if defined(__aarch64__)
1638
+ # define HUF_4X2_4WAY 1
1639
+ #else
1640
+ # define HUF_4X2_4WAY 0
1641
+ #endif
1642
+ #define HUF_4X2_3WAY !HUF_4X2_4WAY
1643
+
1629
1644
  /* Manually unroll the loop because compilers don't consistently
1630
1645
  * unroll the inner loops, which destroys performance.
1631
1646
  */
1632
1647
  do {
1633
- /* Decode 5 symbols from each of the first 3 streams.
1634
- * The final stream will be decoded during the reload phase
1635
- * to reduce register pressure.
1648
+ /* Decode 5 symbols from each of the first 3 or 4 streams.
1649
+ * In the 3-way case the final stream will be decoded during
1650
+ * the reload phase to reduce register pressure.
1636
1651
  */
1637
- HUF_4X_FOR_EACH_STREAM_WITH_VAR(HUF_4X2_DECODE_SYMBOL, 0);
1638
- HUF_4X_FOR_EACH_STREAM_WITH_VAR(HUF_4X2_DECODE_SYMBOL, 0);
1639
- HUF_4X_FOR_EACH_STREAM_WITH_VAR(HUF_4X2_DECODE_SYMBOL, 0);
1640
- HUF_4X_FOR_EACH_STREAM_WITH_VAR(HUF_4X2_DECODE_SYMBOL, 0);
1641
- HUF_4X_FOR_EACH_STREAM_WITH_VAR(HUF_4X2_DECODE_SYMBOL, 0);
1642
-
1643
- /* Decode one symbol from the final stream */
1644
- HUF_4X2_DECODE_SYMBOL(3, 1);
1645
-
1646
- /* Decode 4 symbols from the final stream & reload bitstreams.
1647
- * The final stream is reloaded last, meaning that all 5 symbols
1648
- * are decoded from the final stream before it is reloaded.
1652
+ HUF_4X_FOR_EACH_STREAM_WITH_VAR(HUF_4X2_DECODE_SYMBOL, HUF_4X2_4WAY);
1653
+ HUF_4X_FOR_EACH_STREAM_WITH_VAR(HUF_4X2_DECODE_SYMBOL, HUF_4X2_4WAY);
1654
+ HUF_4X_FOR_EACH_STREAM_WITH_VAR(HUF_4X2_DECODE_SYMBOL, HUF_4X2_4WAY);
1655
+ HUF_4X_FOR_EACH_STREAM_WITH_VAR(HUF_4X2_DECODE_SYMBOL, HUF_4X2_4WAY);
1656
+ HUF_4X_FOR_EACH_STREAM_WITH_VAR(HUF_4X2_DECODE_SYMBOL, HUF_4X2_4WAY);
1657
+
1658
+ /* In the 3-way case decode one symbol from the final stream. */
1659
+ HUF_4X2_DECODE_SYMBOL(3, HUF_4X2_3WAY);
1660
+
1661
+ /* In the 3-way case decode 4 symbols from the final stream &
1662
+ * reload bitstreams. The final stream is reloaded last, meaning
1663
+ * that all 5 symbols are decoded from the final stream before
1664
+ * it is reloaded.
1649
1665
  */
1650
- HUF_4X_FOR_EACH_STREAM(HUF_4X2_RELOAD_STREAM);
1666
+ HUF_4X_FOR_EACH_STREAM_WITH_VAR(HUF_5X2_RELOAD_STREAM, HUF_4X2_3WAY);
1651
1667
  } while (op[3] < olimit);
1652
1668
  }
1653
1669
 
1654
1670
  #undef HUF_4X2_DECODE_SYMBOL
1655
- #undef HUF_4X2_RELOAD_STREAM
1671
+ #undef HUF_5X2_RELOAD_STREAM
1656
1672
 
1657
1673
  _out:
1658
1674
 
@@ -1671,7 +1687,7 @@ HUF_decompress4X2_usingDTable_internal_fast(
1671
1687
  HUF_DecompressFastLoopFn loopFn) {
1672
1688
  void const* dt = DTable + 1;
1673
1689
  const BYTE* const ilowest = (const BYTE*)cSrc;
1674
- BYTE* const oend = ZSTD_maybeNullPtrAdd((BYTE*)dst, dstSize);
1690
+ BYTE* const oend = (BYTE*)ZSTD_maybeNullPtrAdd(dst, (ptrdiff_t)dstSize);
1675
1691
  HUF_DecompressFastArgs args;
1676
1692
  {
1677
1693
  size_t const ret = HUF_DecompressFastArgs_init(&args, dst, dstSize, cSrc, cSrcSize, DTable);
@@ -1722,18 +1738,16 @@ static size_t HUF_decompress4X2_usingDTable_internal(void* dst, size_t dstSize,
1722
1738
  HUF_DecompressUsingDTableFn fallbackFn = HUF_decompress4X2_usingDTable_internal_default;
1723
1739
  HUF_DecompressFastLoopFn loopFn = HUF_decompress4X2_usingDTable_internal_fast_c_loop;
1724
1740
 
1725
- #if DYNAMIC_BMI2
1726
- if (flags & HUF_flags_bmi2) {
1741
+ if (ZSTD_USE_BMI2(flags & HUF_flags_bmi2)) {
1727
1742
  fallbackFn = HUF_decompress4X2_usingDTable_internal_bmi2;
1728
1743
  # if ZSTD_ENABLE_ASM_X86_64_BMI2
1729
1744
  if (!(flags & HUF_flags_disableAsm)) {
1730
1745
  loopFn = HUF_decompress4X2_usingDTable_internal_fast_asm_loop;
1731
1746
  }
1732
1747
  # endif
1733
- } else {
1748
+ } else if (DYNAMIC_BMI2) {
1734
1749
  return fallbackFn(dst, dstSize, cSrc, cSrcSize, DTable);
1735
1750
  }
1736
- #endif
1737
1751
 
1738
1752
  #if ZSTD_ENABLE_ASM_X86_64_BMI2 && defined(__BMI2__)
1739
1753
  if (!(flags & HUF_flags_disableAsm)) {