Visitar URL original
gh-81057: Move Globals in Core Code to _PyRuntimeState by ericsnowcurrently · Pull Request #99496 · python/cpython · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Move the Bigint freelist to _PyRuntimeState.
  • Loading branch information
ericsnowcurrently committed Nov 14, 2022
commit c7e4333177e9a2fd1d08c6990a9da23124b655a9
18 changes: 18 additions & 0 deletions Include/internal/pycore_dtoa.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#ifndef Py_INTERNAL_DTOA_H
#define Py_INTERNAL_DTOA_H
#ifdef __cplusplus
extern "C" {
#endif
Expand All @@ -11,6 +13,21 @@ extern "C" {

#if _PY_SHORT_FLOAT_REPR == 1

typedef uint32_t ULong;

struct
Bigint {
struct Bigint *next;
int k, maxwds, sign, wds;
ULong x[1];
};

#ifndef Py_USING_MEMORY_DEBUGGER
/* The size of the Bigint freelist */
# define Bigint_Kmax 7
#endif


/* These functions are used by modules compiled as C extension like math:
they must be exported. */

Expand All @@ -26,3 +43,4 @@ PyAPI_FUNC(double) _Py_dg_infinity(int sign);
#ifdef __cplusplus
}
#endif
#endif /* !Py_INTERNAL_DTOA_H */
6 changes: 6 additions & 0 deletions Include/internal/pycore_runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ extern "C" {
#endif

#include "pycore_atomic.h" /* _Py_atomic_address */
#include "pycore_dtoa.h" // struct Bigint
#include "pycore_gil.h" // struct _gil_runtime_state
#include "pycore_global_objects.h" // struct _Py_global_objects
#include "pycore_import.h" // struct _import_runtime_state
Expand Down Expand Up @@ -128,6 +129,11 @@ typedef struct pyruntimestate {
struct {
struct _PyTraceMalloc_Config config;
} tracemalloc;
struct {
#ifndef Py_USING_MEMORY_DEBUGGER
struct Bigint *freelist[Bigint_Kmax+1];
#endif
} dtoa;

PyPreConfig preconfig;

Expand Down
25 changes: 11 additions & 14 deletions Python/dtoa.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@

#include "Python.h"
#include "pycore_dtoa.h" // _PY_SHORT_FLOAT_REPR
#include "pycore_runtime.h" // _PyRuntime
#include <stdlib.h> // exit()

/* if _PY_SHORT_FLOAT_REPR == 0, then don't even try to compile
Expand Down Expand Up @@ -156,7 +157,7 @@
#endif


typedef uint32_t ULong;
// ULong is defined in pycore_dtoa.h.
typedef int32_t Long;
typedef uint64_t ULLong;

Expand Down Expand Up @@ -298,8 +299,6 @@ BCinfo {

#define FFFFFFFF 0xffffffffUL

#define Kmax 7

/* struct Bigint is used to represent arbitrary-precision integers. These
integers are stored in sign-magnitude format, with the magnitude stored as
an array of base 2**32 digits. Bigints are always normalized: if x is a
Expand All @@ -322,13 +321,7 @@ BCinfo {
significant (x[0]) to most significant (x[wds-1]).
*/

struct
Bigint {
struct Bigint *next;
int k, maxwds, sign, wds;
ULong x[1];
};

// struct Bigint is defined in pycore_dtoa.h.
typedef struct Bigint Bigint;

#ifndef Py_USING_MEMORY_DEBUGGER
Expand All @@ -352,7 +345,7 @@ typedef struct Bigint Bigint;
Bfree to PyMem_Free. Investigate whether this has any significant
performance on impact. */

static Bigint *freelist[Kmax+1];
#define freelist _PyRuntime.dtoa.freelist

/* Allocate space for a Bigint with up to 1<<k digits */

Expand All @@ -363,13 +356,15 @@ Balloc(int k)
Bigint *rv;
unsigned int len;

if (k <= Kmax && (rv = freelist[k]))
if (k <= Bigint_Kmax && (rv = freelist[k]))
freelist[k] = rv->next;
else {
x = 1 << k;
len = (sizeof(Bigint) + (x-1)*sizeof(ULong) + sizeof(double) - 1)
/sizeof(double);
if (k <= Kmax && pmem_next - private_mem + len <= (Py_ssize_t)PRIVATE_mem) {
if (k <= Bigint_Kmax &&
pmem_next - private_mem + len <= (Py_ssize_t)PRIVATE_mem
) {
rv = (Bigint*)pmem_next;
pmem_next += len;
}
Expand All @@ -391,7 +386,7 @@ static void
Bfree(Bigint *v)
{
if (v) {
if (v->k > Kmax)
if (v->k > Bigint_Kmax)
FREE((void*)v);
else {
v->next = freelist[v->k];
Expand All @@ -400,6 +395,8 @@ Bfree(Bigint *v)
}
}

#undef freelist

#else

/* Alternative versions of Balloc and Bfree that use PyMem_Malloc and
Expand Down
1 change: 0 additions & 1 deletion Tools/c-analyzer/cpython/globals-to-fix.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,6 @@ Objects/unicodeobject.c - ucnhash_capi -
# state

# pre-allocated memory
Python/dtoa.c - freelist -
Python/dtoa.c - private_mem -

# local buffer
Expand Down