diff --git a/Include/ceval.h b/Include/ceval.h index d194044..542b929 100644 --- a/Include/ceval.h +++ b/Include/ceval.h @@ -215,6 +215,9 @@ PyAPI_FUNC(void) _PyEval_SignalAsyncExc(void); #define FVS_MASK 0x4 #define FVS_HAVE_SPEC 0x4 +/* Private Function */ +void _PyEval_Fini(void); + #ifdef __cplusplus } #endif diff --git a/Python/ceval.c b/Python/ceval.c index 743a969..40e24cf 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -155,6 +155,16 @@ static PyObject * special_lookup(PyObject *, _Py_Identifier *); "free variable '%.200s' referenced before assignment" \ " in enclosing scope" + +/* binary_subscr stats */ +static size_t total_subscr_calls = 0; +static size_t list_subscr_calls = 0; +static size_t tuple_subscr_calls = 0; +static size_t dict_subscr_calls = 0; +static size_t unicode_subscr_calls = 0; +static size_t bytes_subscr_calls = 0; +static size_t negative_one_subscr_calls = 0; + /* Dynamic execution profile */ #ifdef DYNAMIC_EXECUTION_PROFILE #ifdef DXPAIRS @@ -1595,6 +1605,25 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag) Py_DECREF(container); Py_DECREF(sub); SET_TOP(res); + total_subscr_calls++; + if (PyLong_CheckExact(sub) && PyLong_AsLong(sub) == -1) { + negative_one_subscr_calls++; + } + if (PyList_CheckExact(container)) { + list_subscr_calls++; + } + else if (PyTuple_CheckExact(container)) { + tuple_subscr_calls++; + } + else if (PyDict_CheckExact(container)) { + dict_subscr_calls++; + } + else if (PyUnicode_CheckExact(container)) { + unicode_subscr_calls++; + } + else if (PyBytes_CheckExact(container)) { + bytes_subscr_calls++; + } if (res == NULL) goto error; DISPATCH(); @@ -5363,3 +5392,22 @@ _Py_GetDXProfile(PyObject *self, PyObject *args) } #endif + +void +_PyEval_Fini(void) +{ + fprintf(stderr, "Total BINARY_SUBSCR calls: %zd\n", + total_subscr_calls); + fprintf(stderr, "List BINARY_SUBSCR calls: %zd\n", + list_subscr_calls); + fprintf(stderr, "Tuple BINARY_SUBSCR calls: %zd\n", + tuple_subscr_calls); + fprintf(stderr, "Dict BINARY_SUBSCR calls: %zd\n", + dict_subscr_calls); + fprintf(stderr, "Unicode BINARY_SUBSCR calls: %zd\n", + unicode_subscr_calls); + fprintf(stderr, "Bytes BINARY_SUBSCR calls: %zd\n", + bytes_subscr_calls); + fprintf(stderr, "[-1] BINARY_SUBSCR calls: %zd\n", + negative_one_subscr_calls); +} diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index e9db7f6..cffd785 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -585,6 +585,8 @@ Py_FinalizeEx(void) /* Destroy all modules */ PyImport_Cleanup(); + /* Print subscr stats */ + _PyEval_Fini(); /* Flush sys.stdout and sys.stderr (again, in case more was printed) */ if (flush_std_files() < 0) { status = -1;