gh-81057: Move Globals in Core Code to _PyRuntimeState (gh-99496)

This is the first of several changes to consolidate non-object globals in core code.

https://github.com/python/cpython/issues/81057
This commit is contained in:
Eric Snow 2022-11-15 09:45:11 -07:00 committed by GitHub
parent 73943cbc4c
commit 3c57971a2d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 235 additions and 167 deletions

View file

@ -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
@ -156,7 +157,7 @@
#endif
typedef uint32_t ULong;
// ULong is defined in pycore_dtoa.h.
typedef int32_t Long;
typedef uint64_t ULLong;
@ -171,12 +172,6 @@ typedef uint64_t ULLong;
#define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);}
#endif
#ifndef PRIVATE_MEM
#define PRIVATE_MEM 2304
#endif
#define PRIVATE_mem ((PRIVATE_MEM+sizeof(double)-1)/sizeof(double))
static double private_mem[PRIVATE_mem], *pmem_next = private_mem;
#ifdef __cplusplus
extern "C" {
#endif
@ -298,8 +293,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
@ -322,13 +315,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
@ -352,7 +339,9 @@ 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
#define private_mem _PyRuntime.dtoa.preallocated
#define pmem_next _PyRuntime.dtoa.preallocated_next
/* Allocate space for a Bigint with up to 1<<k digits */
@ -363,13 +352,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)Bigint_PREALLOC_SIZE
) {
rv = (Bigint*)pmem_next;
pmem_next += len;
}
@ -391,7 +382,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];
@ -400,6 +391,10 @@ Bfree(Bigint *v)
}
}
#undef pmem_next
#undef private_mem
#undef freelist
#else
/* Alternative versions of Balloc and Bfree that use PyMem_Malloc and

View file

@ -1846,9 +1846,6 @@ vgetargskeywords(PyObject *args, PyObject *kwargs, const char *format,
}
/* List of static parsers. */
static struct _PyArg_Parser *static_arg_parsers = NULL;
static int
scan_keywords(const char * const *keywords, int *ptotal, int *pposonly)
{
@ -2024,8 +2021,8 @@ _parser_init(struct _PyArg_Parser *parser)
parser->initialized = owned ? 1 : -1;
assert(parser->next == NULL);
parser->next = static_arg_parsers;
static_arg_parsers = parser;
parser->next = _PyRuntime.getargs.static_parsers;
_PyRuntime.getargs.static_parsers = parser;
return 1;
}
@ -2930,14 +2927,14 @@ _PyArg_NoKwnames(const char *funcname, PyObject *kwnames)
void
_PyArg_Fini(void)
{
struct _PyArg_Parser *tmp, *s = static_arg_parsers;
struct _PyArg_Parser *tmp, *s = _PyRuntime.getargs.static_parsers;
while (s) {
tmp = s->next;
s->next = NULL;
parser_clear(s);
s = tmp;
}
static_arg_parsers = NULL;
_PyRuntime.getargs.static_parsers = NULL;
}
#ifdef __cplusplus

View file

@ -5,12 +5,23 @@
#include "patchlevel.h"
static int initialized = 0;
static char version[250];
void _Py_InitVersion(void)
{
if (initialized) {
return;
}
initialized = 1;
PyOS_snprintf(version, sizeof(version), "%.80s (%.80s) %.80s",
PY_VERSION, Py_GetBuildInfo(), Py_GetCompiler());
}
const char *
Py_GetVersion(void)
{
static char version[250];
PyOS_snprintf(version, sizeof(version), "%.80s (%.80s) %.80s",
PY_VERSION, Py_GetBuildInfo(), Py_GetCompiler());
_Py_InitVersion();
return version;
}

View file

@ -600,6 +600,8 @@ pycore_init_runtime(_PyRuntimeState *runtime,
*/
_PyRuntimeState_SetFinalizing(runtime, NULL);
_Py_InitVersion();
status = _Py_HashRandomization_Init(config);
if (_PyStatus_EXCEPTION(status)) {
return status;