Skip to content

Commit

Permalink
gh-106078: Move static variables initialized once to decimal module g…
Browse files Browse the repository at this point in the history
…lobal state (#106475)
  • Loading branch information
CharlieZhao95 authored Jul 10, 2023
1 parent 43389e4 commit 9384665
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 25 deletions.
50 changes: 28 additions & 22 deletions Modules/_decimal/_decimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@

#include "docstrings.h"

#ifdef EXTRA_FUNCTIONALITY
#define _PY_DEC_ROUND_GUARD MPD_ROUND_GUARD
#else
#define _PY_DEC_ROUND_GUARD (MPD_ROUND_GUARD-1)
#endif

struct PyDecContextObject;

typedef struct {
Expand Down Expand Up @@ -68,6 +74,13 @@ typedef struct {
/* Basic and extended context templates */
PyObject *basic_context_template;
PyObject *extended_context_template;

PyObject *round_map[_PY_DEC_ROUND_GUARD];

/* Convert rationals for comparison */
PyObject *Rational;

PyObject *SignalTuple;
} decimal_state;

static decimal_state global_state;
Expand Down Expand Up @@ -216,13 +229,6 @@ static const char *dec_signal_string[MPD_NUM_FLAGS] = {
"Underflow",
};

#ifdef EXTRA_FUNCTIONALITY
#define _PY_DEC_ROUND_GUARD MPD_ROUND_GUARD
#else
#define _PY_DEC_ROUND_GUARD (MPD_ROUND_GUARD-1)
#endif
static PyObject *round_map[_PY_DEC_ROUND_GUARD];

static const char *invalid_rounding_err =
"valid values for rounding are:\n\
[ROUND_CEILING, ROUND_FLOOR, ROUND_UP, ROUND_DOWN,\n\
Expand Down Expand Up @@ -520,15 +526,16 @@ static int
getround(PyObject *v)
{
int i;
decimal_state *state = GLOBAL_STATE();

if (PyUnicode_Check(v)) {
for (i = 0; i < _PY_DEC_ROUND_GUARD; i++) {
if (v == round_map[i]) {
if (v == state->round_map[i]) {
return i;
}
}
for (i = 0; i < _PY_DEC_ROUND_GUARD; i++) {
if (PyUnicode_Compare(v, round_map[i]) == 0) {
if (PyUnicode_Compare(v, state->round_map[i]) == 0) {
return i;
}
}
Expand Down Expand Up @@ -561,11 +568,11 @@ signaldict_len(PyObject *self UNUSED)
return SIGNAL_MAP_LEN;
}

static PyObject *SignalTuple;
static PyObject *
signaldict_iter(PyObject *self UNUSED)
{
return PyTuple_Type.tp_iter(SignalTuple);
decimal_state *state = GLOBAL_STATE();
return PyTuple_Type.tp_iter(state->SignalTuple);
}

static PyObject *
Expand Down Expand Up @@ -754,8 +761,9 @@ static PyObject *
context_getround(PyObject *self, void *closure UNUSED)
{
int i = mpd_getround(CTX(self));
decimal_state *state = GLOBAL_STATE();

return Py_NewRef(round_map[i]);
return Py_NewRef(state->round_map[i]);
}

static PyObject *
Expand Down Expand Up @@ -2987,8 +2995,6 @@ convert_op(int type_err, PyObject **conv, PyObject *v, PyObject *context)
/* Implicit conversions to Decimal for comparison */
/******************************************************************************/

/* Convert rationals for comparison */
static PyObject *Rational = NULL;
static PyObject *
multiply_by_denominator(PyObject *v, PyObject *r, PyObject *context)
{
Expand Down Expand Up @@ -3117,7 +3123,7 @@ convert_op_cmp(PyObject **vcmp, PyObject **wcmp, PyObject *v, PyObject *w,
}
}
else {
int is_rational = PyObject_IsInstance(w, Rational);
int is_rational = PyObject_IsInstance(w, state->Rational);
if (is_rational < 0) {
*wcmp = NULL;
}
Expand Down Expand Up @@ -5865,7 +5871,7 @@ PyInit__decimal(void)
(PyObject *)state->PyDec_Type));
Py_CLEAR(obj);
/* Rational is a global variable used for fraction comparisons. */
ASSIGN_PTR(Rational, PyObject_GetAttrString(numbers, "Rational"));
ASSIGN_PTR(state->Rational, PyObject_GetAttrString(numbers, "Rational"));
/* Done with numbers, Number */
Py_CLEAR(numbers);
Py_CLEAR(Number);
Expand Down Expand Up @@ -5912,7 +5918,7 @@ PyInit__decimal(void)
CHECK_INT(PyModule_AddType(m, (PyTypeObject *)state->DecimalException));

/* Create signal tuple */
ASSIGN_PTR(SignalTuple, PyTuple_New(SIGNAL_MAP_LEN));
ASSIGN_PTR(state->SignalTuple, PyTuple_New(SIGNAL_MAP_LEN));

/* Add exceptions that correspond to IEEE signals */
for (i = SIGNAL_MAP_LEN-1; i >= 0; i--) {
Expand Down Expand Up @@ -5953,7 +5959,7 @@ PyInit__decimal(void)
CHECK_INT(PyModule_AddObject(m, cm->name, Py_NewRef(cm->ex)));

/* add to signal tuple */
PyTuple_SET_ITEM(SignalTuple, i, Py_NewRef(cm->ex));
PyTuple_SET_ITEM(state->SignalTuple, i, Py_NewRef(cm->ex));
}

/*
Expand Down Expand Up @@ -6029,8 +6035,8 @@ PyInit__decimal(void)

/* Init string constants */
for (i = 0; i < _PY_DEC_ROUND_GUARD; i++) {
ASSIGN_PTR(round_map[i], PyUnicode_InternFromString(mpd_round_string[i]));
CHECK_INT(PyModule_AddObject(m, mpd_round_string[i], Py_NewRef(round_map[i])));
ASSIGN_PTR(state->round_map[i], PyUnicode_InternFromString(mpd_round_string[i]));
CHECK_INT(PyModule_AddObject(m, mpd_round_string[i], Py_NewRef(state->round_map[i])));
}

/* Add specification version number */
Expand All @@ -6045,11 +6051,11 @@ PyInit__decimal(void)
Py_CLEAR(obj); /* GCOV_NOT_REACHED */
Py_CLEAR(numbers); /* GCOV_NOT_REACHED */
Py_CLEAR(Number); /* GCOV_NOT_REACHED */
Py_CLEAR(Rational); /* GCOV_NOT_REACHED */
Py_CLEAR(state->Rational); /* GCOV_NOT_REACHED */
Py_CLEAR(collections); /* GCOV_NOT_REACHED */
Py_CLEAR(collections_abc); /* GCOV_NOT_REACHED */
Py_CLEAR(MutableMapping); /* GCOV_NOT_REACHED */
Py_CLEAR(SignalTuple); /* GCOV_NOT_REACHED */
Py_CLEAR(state->SignalTuple); /* GCOV_NOT_REACHED */
Py_CLEAR(state->DecimalTuple); /* GCOV_NOT_REACHED */
Py_CLEAR(state->default_context_template); /* GCOV_NOT_REACHED */
#ifndef WITH_DECIMAL_CONTEXTVAR
Expand Down
3 changes: 0 additions & 3 deletions Tools/c-analyzer/cpython/globals-to-fix.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -422,9 +422,6 @@ Modules/_datetimemodule.c - us_per_day -
Modules/_datetimemodule.c - us_per_week -
Modules/_datetimemodule.c - seconds_per_day -
Modules/_decimal/_decimal.c - global_state -
Modules/_decimal/_decimal.c - round_map -
Modules/_decimal/_decimal.c - Rational -
Modules/_decimal/_decimal.c - SignalTuple -

## state
Modules/_asynciomodule.c - fi_freelist -
Expand Down

0 comments on commit 9384665

Please sign in to comment.