From bbf315ee61f4f50c9d91f24f870827afa6639325 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Sun, 18 Jan 2026 16:26:24 +0300 Subject: [PATCH 1/3] Cleanup initialization and caching --- gmp.c | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/gmp.c b/gmp.c index c694d7cd..657cf852 100644 --- a/gmp.c +++ b/gmp.c @@ -8,14 +8,14 @@ #include #if !defined(PYPY_VERSION) -# define CACHE_SIZE (99) +# define MAX_CACHE_SIZE 100 #else -# define CACHE_SIZE (0) +# define MAX_CACHE_SIZE 0 #endif -#define MAX_CACHE_MPZ_DIGITS (64) +#define MAX_CACHED_NDIGITS 16 typedef struct { - MPZ_Object *gmp_cache[CACHE_SIZE + 1]; + MPZ_Object *gmp_cache[MAX_CACHE_SIZE + 1]; size_t gmp_cache_size; } gmp_global; @@ -32,13 +32,8 @@ MPZ_new(void) MPZ_Object *res; if (global.gmp_cache_size) { - res = global.gmp_cache[--(global.gmp_cache_size)]; - if (zz_set(0, &res->z)) { - /* LCOV_EXCL_START */ - global.gmp_cache[(global.gmp_cache_size)++] = res; - return (MPZ_Object *)PyErr_NoMemory(); - /* LCOV_EXCL_STOP */ - } + res = global.gmp_cache[--global.gmp_cache_size]; + (void)zz_set(0, &res->z); Py_XINCREF((PyObject *)res); } else { @@ -695,11 +690,11 @@ dealloc(PyObject *self) { MPZ_Object *u = (MPZ_Object *)self; - if (global.gmp_cache_size < CACHE_SIZE - && (u->z).alloc <= MAX_CACHE_MPZ_DIGITS + if (global.gmp_cache_size < MAX_CACHE_SIZE + && (u->z).alloc <= MAX_CACHED_NDIGITS && MPZ_CheckExact(self)) { - global.gmp_cache[(global.gmp_cache_size)++] = u; + global.gmp_cache[global.gmp_cache_size++] = u; } else { zz_clear(&u->z); @@ -2707,14 +2702,13 @@ gmp__mpmath_create(PyObject *self, PyObject *const *args, Py_ssize_t nargs) static PyObject * gmp__free_cache(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args)) { - for (size_t i = 0; i < global.gmp_cache_size; i++) { - MPZ_Object *u = global.gmp_cache[i]; + while (global.gmp_cache_size) { + MPZ_Object *u = global.gmp_cache[--global.gmp_cache_size]; PyObject *self = (PyObject *)u; zz_clear(&u->z); PyObject_Free(self); } - global.gmp_cache_size = 0; Py_RETURN_NONE; } From 41fc79dc14f1ed1100f38e3a0e451dc8420e5777 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Mon, 19 Jan 2026 03:52:53 +0300 Subject: [PATCH 2/3] Disable pytest-xdist for GraalPy (coverage tests) https://github.com/diofant/python-gmp/actions/runs/21110025702/job/60707238265 --- .github/workflows/coverage.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index 676e0320..d1c80b87 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -4,7 +4,7 @@ jobs: coverage: runs-on: ${{ matrix.os }} env: - PYTEST_ADDOPTS: --verbose -n 2 + PYTEST_ADDOPTS: --verbose CFLAGS: -Wall -Wpedantic -Werror -std=c17 -Wconversion strategy: matrix: From bf543324b05960b73bc373a9b8678fda29a1d4fe Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Mon, 19 Jan 2026 03:56:10 +0300 Subject: [PATCH 3/3] Set timeout-minutes for coverage job --- .github/workflows/coverage.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index d1c80b87..e4e2f49a 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -2,6 +2,7 @@ name: Run coverage tests on: [workflow_dispatch, workflow_call] jobs: coverage: + timeout-minutes: 20 runs-on: ${{ matrix.os }} env: PYTEST_ADDOPTS: --verbose