From 3b1f672fb644138ab8f77a418b3e245ab6085d13 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Thu, 19 Feb 2026 02:23:38 +0300 Subject: [PATCH 1/4] Avoid static zz_t initialization in heap() --- gmp.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/gmp.c b/gmp.c index 6c6d1534..8a806cfb 100644 --- a/gmp.c +++ b/gmp.c @@ -850,8 +850,11 @@ hash(PyObject *self) return u->hash_cache; } - zz_digit_t digits[1]; - zz_t w = {false, 1, 1, digits}; + zz_t w; + + if (zz_init(&w)) { + return -1; /* LCOV_EXCL_LINE */ + } assert((int64_t)INT64_MAX > pyhash_modulus); (void)zz_div(&u->z, (int64_t)pyhash_modulus, NULL, &w); @@ -860,6 +863,7 @@ hash(PyObject *self) assert(sizeof(Py_hash_t) == 8); (void)zz_get(&w, (int64_t *)&r); + zz_clear(&w); if (zz_isneg(&u->z) && r) { r = -(pyhash_modulus - r); } From ede9094fee6dafced9631b48272fcb7638dce72c Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Thu, 19 Feb 2026 04:43:22 +0300 Subject: [PATCH 2/4] Use zz_sizeof() --- gmp.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/gmp.c b/gmp.c index 8a806cfb..7150f278 100644 --- a/gmp.c +++ b/gmp.c @@ -12,7 +12,7 @@ #else # define MAX_CACHE_SIZE 0 #endif -#define MAX_CACHED_NDIGITS 16 +#define MAX_CACHED_SIZEOF 256 typedef struct { MPZ_Object *gmp_cache[MAX_CACHE_SIZE + 1]; @@ -660,7 +660,7 @@ dealloc(PyObject *self) MPZ_Object *u = (MPZ_Object *)self; if (global.gmp_cache_size < MAX_CACHE_SIZE - && (u->z).alloc <= MAX_CACHED_NDIGITS + && zz_sizeof(&u->z) <= MAX_CACHED_SIZEOF && MPZ_CheckExact(self)) { global.gmp_cache[global.gmp_cache_size++] = u; @@ -1884,8 +1884,8 @@ __sizeof__(PyObject *self, PyObject *Py_UNUSED(ignored)) { MPZ_Object *u = (MPZ_Object *)self; - return PyLong_FromSize_t(sizeof(MPZ_Object) - + (size_t)(u->z).alloc*sizeof(zz_digit_t)); + return PyLong_FromSize_t(sizeof(MPZ_Object) - sizeof(zz_t) + + zz_sizeof(&u->z)); } static PyObject * From 6663edfe08249af081dcec7407395f44c79ad97d Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Thu, 19 Feb 2026 14:16:57 +0300 Subject: [PATCH 3/4] Add collatz2() in bench/collatz.py --- bench/collatz.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/bench/collatz.py b/bench/collatz.py index 3b08a4a3..6c0f63fc 100644 --- a/bench/collatz.py +++ b/bench/collatz.py @@ -36,8 +36,17 @@ def collatz1(n): total += 1 return total +def collatz2(n): + total = 0 + n = mpz(n) + while n > 1: + n = n*3 + 1 if n & one else n//2 + total += 1 + return total + + runner = pyperf.Runner() -for f in [collatz0, collatz1]: +for f in [collatz0, collatz1, collatz2]: for v in ["97", "871", "(1<<128)+31"]: h = f"{f.__name__}({v})" i = eval(v) From 33cfa0e54b9e1657a8da4bf8dbfd962094252933 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Fri, 20 Feb 2026 07:07:46 +0300 Subject: [PATCH 4/4] Use libzz v0.9.0a4 --- .readthedocs.yaml | 2 +- scripts/cibw_before_all.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.readthedocs.yaml b/.readthedocs.yaml index 289e1887..638a47f6 100644 --- a/.readthedocs.yaml +++ b/.readthedocs.yaml @@ -12,7 +12,7 @@ build: pre_create_environment: - | PREFIX=$(pwd)/.local - ZZ_VERSION=0.9.0a3 + ZZ_VERSION=0.9.0a4 ZZ_DIR=zz-${ZZ_VERSION} GITHUB_URL=https://github.com/diofant/zz/releases/download/ ZZ_URL=${GITHUB_URL}v${ZZ_VERSION}/${ZZ_DIR}.tar.gz diff --git a/scripts/cibw_before_all.sh b/scripts/cibw_before_all.sh index fbcc22a1..5f7ec6ba 100644 --- a/scripts/cibw_before_all.sh +++ b/scripts/cibw_before_all.sh @@ -53,7 +53,7 @@ make --silent all install cd .. -ZZ_VERSION=0.9.0a3 +ZZ_VERSION=0.9.0a4 ZZ_DIR=zz-${ZZ_VERSION} ZZ_URL=https://github.com/diofant/zz/releases/download/v${ZZ_VERSION}/${ZZ_DIR}.tar.gz