Blame zstdpool.diff

5b17a5
--- rpmio/rpmio.c.orig	2023-09-19 10:10:10.000000000 +0000
5b17a5
+++ rpmio/rpmio.c	2023-10-10 12:09:28.171040124 +0000
0dcfb2
@@ -8,6 +8,7 @@
0dcfb2
 #include <ctype.h>
0dcfb2
 #include <dirent.h>
0dcfb2
 #include <fcntl.h>
0dcfb2
+#include <pthread.h>
5b17a5
 #include <sys/resource.h>
5b17a5
 
5b17a5
 #include <rpm/rpmlog.h>
5b17a5
@@ -997,6 +998,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 {
5b17a5
@@ -1011,6 +1013,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}");
5b17a5
+    if (numthreads == 0)
5b17a5
+	numthreads = rpmExpandNumeric("%{getncpus:thread}");
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;
5b17a5
@@ -1116,8 +1141,18 @@ static rpmzstd rpmzstdNew(int fdno, cons
5b17a5
 	}
0dcfb2
 
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();