Skip to content

Commit 8ac0886

Browse files
authored
bpo-44890: collect specialization stats if Py_DEBUG (GH-27731)
1 parent a530a95 commit 8ac0886

File tree

5 files changed

+17
-13
lines changed

5 files changed

+17
-13
lines changed

Include/internal/pycore_code.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -301,13 +301,16 @@ int _Py_Specialize_StoreAttr(PyObject *owner, _Py_CODEUNIT *instr, PyObject *nam
301301
int _Py_Specialize_LoadGlobal(PyObject *globals, PyObject *builtins, _Py_CODEUNIT *instr, PyObject *name, SpecializedCacheEntry *cache);
302302
int _Py_Specialize_BinarySubscr(PyObject *sub, PyObject *container, _Py_CODEUNIT *instr);
303303

304-
#define SPECIALIZATION_STATS 0
305-
#define SPECIALIZATION_STATS_DETAILED 0
306-
#define SPECIALIZATION_STATS_TO_FILE 0
304+
#define PRINT_SPECIALIZATION_STATS 0
305+
#define PRINT_SPECIALIZATION_STATS_DETAILED 0
306+
#define PRINT_SPECIALIZATION_STATS_TO_FILE 0
307+
308+
#define COLLECT_SPECIALIZATION_STATS (Py_DEBUG || PRINT_SPECIALIZATION_STATS)
309+
#define COLLECT_SPECIALIZATION_STATS_DETAILED (Py_DEBUG || PRINT_SPECIALIZATION_STATS_DETAILED)
307310

308311
#define SPECIALIZATION_FAILURE_KINDS 20
309312

310-
#if SPECIALIZATION_STATS
313+
#if COLLECT_SPECIALIZATION_STATS
311314

312315
typedef struct _stats {
313316
uint64_t specialization_success;
@@ -317,7 +320,7 @@ typedef struct _stats {
317320
uint64_t miss;
318321
uint64_t deopt;
319322
uint64_t unquickened;
320-
#if SPECIALIZATION_STATS_DETAILED
323+
#if COLLECT_SPECIALIZATION_STATS_DETAILED
321324
uint64_t specialization_failure_kinds[SPECIALIZATION_FAILURE_KINDS];
322325
#endif
323326
} SpecializationStats;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Specialization stats are always collected in debug builds.

Modules/_opcode.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ static PyObject *
8585
_opcode_get_specialization_stats_impl(PyObject *module)
8686
/*[clinic end generated code: output=fcbc32fdfbec5c17 input=e1f60db68d8ce5f6]*/
8787
{
88-
#if SPECIALIZATION_STATS
88+
#if COLLECT_SPECIALIZATION_STATS
8989
return _Py_GetSpecializationStats();
9090
#else
9191
Py_RETURN_NONE;

Python/ceval.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ PyEval_InitThreads(void)
341341
void
342342
_PyEval_Fini(void)
343343
{
344-
#if SPECIALIZATION_STATS
344+
#if PRINT_SPECIALIZATION_STATS
345345
_Py_PrintSpecializationStats();
346346
#endif
347347
}

Python/specialize.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
*/
3838

3939
Py_ssize_t _Py_QuickenedCount = 0;
40-
#if SPECIALIZATION_STATS
40+
#if COLLECT_SPECIALIZATION_STATS
4141
SpecializationStats _specialization_stats[256] = { 0 };
4242

4343
#define ADD_STAT_TO_DICT(res, field) \
@@ -69,7 +69,7 @@ stats_to_dict(SpecializationStats *stats)
6969
ADD_STAT_TO_DICT(res, miss);
7070
ADD_STAT_TO_DICT(res, deopt);
7171
ADD_STAT_TO_DICT(res, unquickened);
72-
#if SPECIALIZATION_STATS_DETAILED
72+
#if COLLECT_SPECIALIZATION_STATS_DETAILED
7373
PyObject *failure_kinds = PyTuple_New(SPECIALIZATION_FAILURE_KINDS);
7474
if (failure_kinds == NULL) {
7575
Py_DECREF(res);
@@ -111,7 +111,7 @@ add_stat_dict(
111111
return err;
112112
}
113113

114-
#if SPECIALIZATION_STATS
114+
#if COLLECT_SPECIALIZATION_STATS
115115
PyObject*
116116
_Py_GetSpecializationStats(void) {
117117
PyObject *stats = PyDict_New();
@@ -144,7 +144,7 @@ print_stats(FILE *out, SpecializationStats *stats, const char *name)
144144
PRINT_STAT(name, miss);
145145
PRINT_STAT(name, deopt);
146146
PRINT_STAT(name, unquickened);
147-
#if SPECIALIZATION_STATS_DETAILED
147+
#if PRINT_SPECIALIZATION_STATS_DETAILED
148148
for (int i = 0; i < SPECIALIZATION_FAILURE_KINDS; i++) {
149149
fprintf(out, " %s.specialization_failure_kinds[%d] : %" PRIu64 "\n",
150150
name, i, stats->specialization_failure_kinds[i]);
@@ -157,7 +157,7 @@ void
157157
_Py_PrintSpecializationStats(void)
158158
{
159159
FILE *out = stderr;
160-
#if SPECIALIZATION_STATS_TO_FILE
160+
#if PRINT_SPECIALIZATION_STATS_TO_FILE
161161
/* Write to a file instead of stderr. */
162162
# ifdef MS_WINDOWS
163163
const char *dirname = "c:\\temp\\py_stats\\";
@@ -182,7 +182,7 @@ _Py_PrintSpecializationStats(void)
182182
}
183183
}
184184

185-
#if SPECIALIZATION_STATS_DETAILED
185+
#if COLLECT_SPECIALIZATION_STATS_DETAILED
186186

187187
#define SPECIALIZATION_FAIL(opcode, kind) _specialization_stats[opcode].specialization_failure_kinds[kind]++
188188

0 commit comments

Comments
 (0)