Blame zstdpool.diff

ef651c
--- rpmio/rpmio.c.orig	2021-06-21 12:00:44.648612706 +0000
ef651c
+++ rpmio/rpmio.c	2022-04-13 13:48:55.224954032 +0000
0dcfb2
@@ -8,6 +8,7 @@
0dcfb2
 #include <ctype.h>
0dcfb2
 #include <dirent.h>
0dcfb2
 #include <fcntl.h>
0dcfb2
+#include <pthread.h>
0dcfb2
 #if defined(__linux__)
0dcfb2
 #include <sys/personality.h>
0dcfb2
 #endif
0dcfb2
@@ -1034,6 +1035,7 @@ static const FDIO_t lzdio = &lzdio_s;
0dcfb2
 /* Support for ZSTD library.  */
0dcfb2
 #ifdef HAVE_ZSTD
0dcfb2
 
0dcfb2
+#define ZSTD_STATIC_LINKING_ONLY
0dcfb2
 #include <zstd.h>
0dcfb2
 
0dcfb2
 typedef struct rpmzstd_s {
227a21
@@ -1048,6 +1050,29 @@ typedef struct rpmzstd_s {
0dcfb2
     ZSTD_outBuffer zob;         /*!< ZSTD_outBuffer */
0dcfb2
 } * rpmzstd;
0dcfb2
 
0dcfb2
+#if ZSTD_VERSION_NUMBER >= 10407
0dcfb2
+
0dcfb2
+static pthread_once_t zstdThreadPoolCreated = PTHREAD_ONCE_INIT;
0dcfb2
+static ZSTD_threadPool *zstdThreadPool;
227a21
+static int zstdThreadPoolThreads;
0dcfb2
+
0dcfb2
+static void zstdCreateThreadPool(void)
0dcfb2
+{
0dcfb2
+    int numthreads = rpmExpandNumeric("%{?_zstd_pool_threads}%{?!_zstd_pool_threads:-1}");
0dcfb2
+    if (numthreads >= 0)
0dcfb2
+        numthreads = get_compression_threads(numthreads > 0 ? numthreads : -1);
0dcfb2
+    if (numthreads > 0) {
227a21
+        zstdThreadPoolThreads = numthreads;
0dcfb2
+        zstdThreadPool = ZSTD_createThreadPool(numthreads);
0dcfb2
+        if (!zstdThreadPool)
0dcfb2
+            rpmlog(RPMLOG_WARNING, "Could not create zstd thread pool for %d threads\n", numthreads);
0dcfb2
+        else
0dcfb2
+            rpmlog(RPMLOG_DEBUG, "Created zstd thread pool for %d threads\n", numthreads);
0dcfb2
+    }
0dcfb2
+}
0dcfb2
+
0dcfb2
+#endif
0dcfb2
+
0dcfb2
 static rpmzstd rpmzstdNew(int fdno, const char *fmode)
0dcfb2
 {
0dcfb2
     int flags = 0;
227a21
@@ -1133,8 +1158,18 @@ static rpmzstd rpmzstdNew(int fdno, cons
0dcfb2
 
0dcfb2
 	threads = get_compression_threads(threads);
0dcfb2
 	if (threads > 0) {
0dcfb2
-	    if (ZSTD_isError (ZSTD_CCtx_setParameter(_stream, ZSTD_c_nbWorkers, threads)))
0dcfb2
+	    if (ZSTD_isError (ZSTD_CCtx_setParameter(_stream, ZSTD_c_nbWorkers, threads))) {
0dcfb2
 		rpmlog(RPMLOG_DEBUG, "zstd library does not support multi-threading\n");
0dcfb2
+	    } else {
0dcfb2
+#if ZSTD_VERSION_NUMBER >= 10407
0dcfb2
+		pthread_once(&zstdThreadPoolCreated, zstdCreateThreadPool);
227a21
+		if (zstdThreadPool) {
227a21
+		    if (threads > zstdThreadPoolThreads)
227a21
+			ZSTD_CCtx_setParameter(_stream, ZSTD_c_nbWorkers, zstdThreadPoolThreads);
0dcfb2
+		    ZSTD_CCtx_refThreadPool(_stream, zstdThreadPool);
227a21
+		}
0dcfb2
+#endif
0dcfb2
+	    }
0dcfb2
 	}
0dcfb2
 
0dcfb2
 	nb = ZSTD_CStreamOutSize();