Skip to content
Merged
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
3 changes: 2 additions & 1 deletion .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ name: Run coverage tests
on: [workflow_dispatch, workflow_call]
jobs:
coverage:
timeout-minutes: 20
runs-on: ${{ matrix.os }}
env:
PYTEST_ADDOPTS: --verbose -n 2
PYTEST_ADDOPTS: --verbose
CFLAGS: -Wall -Wpedantic -Werror -std=c17 -Wconversion
strategy:
matrix:
Expand Down
28 changes: 11 additions & 17 deletions gmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
#include <string.h>

#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;

Expand All @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

Expand Down