Skip to content

Commit 5c4fde4

Browse files
Move the Bigint preallocated memory to _PyRuntimeState.
1 parent c7e4333 commit 5c4fde4

File tree

5 files changed

+35
-19
lines changed

5 files changed

+35
-19
lines changed

Include/internal/pycore_dtoa.h

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,35 @@ Bigint {
2222
ULong x[1];
2323
};
2424

25-
#ifndef Py_USING_MEMORY_DEBUGGER
25+
#ifdef Py_USING_MEMORY_DEBUGGER
26+
27+
struct _dtoa_runtime_state {
28+
int _not_used;
29+
};
30+
#define _dtoa_runtime_state_INIT {0}
31+
32+
#else // !Py_USING_MEMORY_DEBUGGER
33+
2634
/* The size of the Bigint freelist */
27-
# define Bigint_Kmax 7
35+
#define Bigint_Kmax 7
36+
37+
#ifndef PRIVATE_MEM
38+
#define PRIVATE_MEM 2304
2839
#endif
40+
#define Bigint_PREALLOC_SIZE \
41+
((PRIVATE_MEM+sizeof(double)-1)/sizeof(double))
42+
43+
struct _dtoa_runtime_state {
44+
struct Bigint *freelist[Bigint_Kmax+1];
45+
double preallocated[Bigint_PREALLOC_SIZE];
46+
double *preallocated_next;
47+
};
48+
#define _dtoa_runtime_state_INIT(runtime) \
49+
{ \
50+
.preallocated_next = runtime.dtoa.preallocated, \
51+
}
52+
53+
#endif // !Py_USING_MEMORY_DEBUGGER
2954

3055

3156
/* These functions are used by modules compiled as C extension like math:

Include/internal/pycore_runtime.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ extern "C" {
99
#endif
1010

1111
#include "pycore_atomic.h" /* _Py_atomic_address */
12-
#include "pycore_dtoa.h" // struct Bigint
12+
#include "pycore_dtoa.h" // struct _dtoa_runtime_state
1313
#include "pycore_gil.h" // struct _gil_runtime_state
1414
#include "pycore_global_objects.h" // struct _Py_global_objects
1515
#include "pycore_import.h" // struct _import_runtime_state
@@ -129,11 +129,7 @@ typedef struct pyruntimestate {
129129
struct {
130130
struct _PyTraceMalloc_Config config;
131131
} tracemalloc;
132-
struct {
133-
#ifndef Py_USING_MEMORY_DEBUGGER
134-
struct Bigint *freelist[Bigint_Kmax+1];
135-
#endif
136-
} dtoa;
132+
struct _dtoa_runtime_state dtoa;
137133

138134
PyPreConfig preconfig;
139135

Include/internal/pycore_runtime_init.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ extern "C" {
3939
.tracemalloc = { \
4040
.config = _PyTraceMalloc_Config_INIT, \
4141
}, \
42+
.dtoa = _dtoa_runtime_state_INIT(runtime), \
4243
.types = { \
4344
.next_version_tag = 1, \
4445
}, \

Python/dtoa.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,6 @@ typedef uint64_t ULLong;
172172
#define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);}
173173
#endif
174174

175-
#ifndef PRIVATE_MEM
176-
#define PRIVATE_MEM 2304
177-
#endif
178-
#define PRIVATE_mem ((PRIVATE_MEM+sizeof(double)-1)/sizeof(double))
179-
static double private_mem[PRIVATE_mem], *pmem_next = private_mem;
180-
181175
#ifdef __cplusplus
182176
extern "C" {
183177
#endif
@@ -346,6 +340,8 @@ typedef struct Bigint Bigint;
346340
performance on impact. */
347341

348342
#define freelist _PyRuntime.dtoa.freelist
343+
#define private_mem _PyRuntime.dtoa.preallocated
344+
#define pmem_next _PyRuntime.dtoa.preallocated_next
349345

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

@@ -363,7 +359,7 @@ Balloc(int k)
363359
len = (sizeof(Bigint) + (x-1)*sizeof(ULong) + sizeof(double) - 1)
364360
/sizeof(double);
365361
if (k <= Bigint_Kmax &&
366-
pmem_next - private_mem + len <= (Py_ssize_t)PRIVATE_mem
362+
pmem_next - private_mem + len <= (Py_ssize_t)Bigint_PREALLOC_SIZE
367363
) {
368364
rv = (Bigint*)pmem_next;
369365
pmem_next += len;
@@ -395,6 +391,8 @@ Bfree(Bigint *v)
395391
}
396392
}
397393

394+
#undef pmem_next
395+
#undef private_mem
398396
#undef freelist
399397

400398
#else

Tools/c-analyzer/cpython/globals-to-fix.tsv

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -324,14 +324,10 @@ Objects/unicodeobject.c - ucnhash_capi -
324324
#-----------------------
325325
# state
326326

327-
# pre-allocated memory
328-
Python/dtoa.c - private_mem -
329-
330327
# local buffer
331328
Python/suggestions.c levenshtein_distance buffer -
332329

333330
# linked list
334-
Python/dtoa.c - pmem_next -
335331
Python/getargs.c - static_arg_parsers -
336332

337333
# other

0 commit comments

Comments
 (0)