Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-24.04]
python-version: [pypy3.11, "graalpy-25.0", "3.x"]
python-version: [pypy3.11, "3.x"]
steps:
- uses: actions/checkout@v6
with:
Expand Down
24 changes: 14 additions & 10 deletions gmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@
#include <stdlib.h>
#include <string.h>

#if !defined(PYPY_VERSION)
# define MAX_CACHE_SIZE 100
#else
# define MAX_CACHE_SIZE 0
#endif
#define MAX_CACHE_SIZE 100
#define MAX_CACHED_NDIGITS 16

typedef struct {
Expand All @@ -34,7 +30,6 @@ MPZ_new(void)
if (global.gmp_cache_size) {
res = global.gmp_cache[--global.gmp_cache_size];
(void)zz_set(0, &res->z);
Py_XINCREF((PyObject *)res);
}
else {
res = PyObject_New(MPZ_Object, &MPZ_Type);
Expand Down Expand Up @@ -683,17 +678,25 @@ new(PyTypeObject *type, PyObject *args, PyObject *keywds)
return new_impl(type, arg, base);
}

static void
finalize(PyObject *self)
{
if (global.gmp_cache_size < MAX_CACHE_SIZE
&& (((MPZ_Object *)self)->z).alloc <= MAX_CACHED_NDIGITS
&& MPZ_CheckExact(self))
{
Py_INCREF(self);
}
}

typedef void (*Py_tp_free_func)(void *);

static void
dealloc(PyObject *self)
{
MPZ_Object *u = (MPZ_Object *)self;

if (global.gmp_cache_size < MAX_CACHE_SIZE
&& (u->z).alloc <= MAX_CACHED_NDIGITS
&& MPZ_CheckExact(self))
{
if (PyObject_CallFinalizerFromDealloc(self)) {
global.gmp_cache[global.gmp_cache_size++] = u;
}
else {
Expand Down Expand Up @@ -2098,6 +2101,7 @@ PyTypeObject MPZ_Type = {
.tp_basicsize = sizeof(MPZ_Object),
.tp_new = new,
.tp_dealloc = dealloc,
.tp_finalize = finalize,
.tp_repr = repr,
.tp_str = str,
.tp_richcompare = richcompare,
Expand Down
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ select = ["E", "F", "I", "PT", "W", "Q", "SIM"]

[tool.cibuildwheel]
build-frontend = {name="build", args=["--verbose", "-Csetup-args=--vsenv"]}
enable = "pypy cpython-prerelease cpython-freethreading graalpy"
skip = "gp311_242* gp3*win*amd64*"
enable = "pypy cpython-prerelease cpython-freethreading"

before-all = "sh scripts/cibw_before_all.sh"
test-extras = "ci"
Expand Down