gh-111962: Make dtoa thread-safe in --disable-gil builds. (#112049)

This updates `dtoa.c` to avoid using the Bigint free-list in --disable-gil builds and
to pre-computes the needed powers of 5 during interpreter initialization.

* gh-111962: Make dtoa thread-safe in `--disable-gil` builds.

This avoids using the Bigint free-list in `--disable-gil` builds
and pre-computes the needed powers of 5 during interpreter initialization.

* Fix size of cached powers of 5 array.

We need the powers of 5 up to 5**512 because we only jump straight to
underflow when the exponent is less than -512 (or larger than 308).

* Rename Py_NOGIL to Py_GIL_DISABLED

* Changes from review

* Fix assertion placement
This commit is contained in:
Sam Gross 2023-12-07 08:47:55 -05:00 committed by GitHub
parent 9f67042f28
commit 2d76be251d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 70 additions and 30 deletions

View file

@ -35,6 +35,9 @@ struct _dtoa_state {
/* The size of the Bigint freelist */
#define Bigint_Kmax 7
/* The size of the cached powers of 5 array */
#define Bigint_Pow5size 8
#ifndef PRIVATE_MEM
#define PRIVATE_MEM 2304
#endif
@ -42,9 +45,10 @@ struct _dtoa_state {
((PRIVATE_MEM+sizeof(double)-1)/sizeof(double))
struct _dtoa_state {
/* p5s is a linked list of powers of 5 of the form 5**(2**i), i >= 2 */
// p5s is an array of powers of 5 of the form:
// 5**(2**(i+2)) for 0 <= i < Bigint_Pow5size
struct Bigint *p5s[Bigint_Pow5size];
// XXX This should be freed during runtime fini.
struct Bigint *p5s;
struct Bigint *freelist[Bigint_Kmax+1];
double preallocated[Bigint_PREALLOC_SIZE];
double *preallocated_next;
@ -57,9 +61,6 @@ struct _dtoa_state {
#endif // !Py_USING_MEMORY_DEBUGGER
/* These functions are used by modules compiled as C extension like math:
they must be exported. */
extern double _Py_dg_strtod(const char *str, char **ptr);
extern char* _Py_dg_dtoa(double d, int mode, int ndigits,
int *decpt, int *sign, char **rve);
@ -67,6 +68,11 @@ extern void _Py_dg_freedtoa(char *s);
#endif // _PY_SHORT_FLOAT_REPR == 1
extern PyStatus _PyDtoa_Init(PyInterpreterState *interp);
extern void _PyDtoa_Fini(PyInterpreterState *interp);
#ifdef __cplusplus
}
#endif