From ae83c782155ffe86830c3255e338f366e331ad30 Mon Sep 17 00:00:00 2001 From: Itamar Ostricher Date: Wed, 14 Dec 2022 11:14:16 -0800 Subject: GH-100000: Cleanup and polish various watchers code (GH-99998) * Initialize `type_watchers` array to `NULL`s * Optimize code watchers notification * Optimize func watchers notification --- Objects/codeobject.c | 15 +++++++++++---- Objects/funcobject.c | 18 ++++++++++++++---- Objects/typeobject.c | 7 ++++--- 3 files changed, 29 insertions(+), 11 deletions(-) (limited to 'Objects') diff --git a/Objects/codeobject.c b/Objects/codeobject.c index f455cc603a..1e5a92270b 100644 --- a/Objects/codeobject.c +++ b/Objects/codeobject.c @@ -15,14 +15,21 @@ static void notify_code_watchers(PyCodeEvent event, PyCodeObject *co) { PyInterpreterState *interp = _PyInterpreterState_GET(); - if (interp->active_code_watchers) { - assert(interp->_initialized); - for (int i = 0; i < CODE_MAX_WATCHERS; i++) { + assert(interp->_initialized); + uint8_t bits = interp->active_code_watchers; + int i = 0; + while (bits) { + assert(i < CODE_MAX_WATCHERS); + if (bits & 1) { PyCode_WatchCallback cb = interp->code_watchers[i]; - if ((cb != NULL) && (cb(event, co) < 0)) { + // callback must be non-null if the watcher bit is set + assert(cb != NULL); + if (cb(event, co) < 0) { PyErr_WriteUnraisable((PyObject *) co); } } + i++; + bits >>= 1; } } diff --git a/Objects/funcobject.c b/Objects/funcobject.c index 9df0652058..d5cf5b9277 100644 --- a/Objects/funcobject.c +++ b/Objects/funcobject.c @@ -12,11 +12,20 @@ static void notify_func_watchers(PyInterpreterState *interp, PyFunction_WatchEvent event, PyFunctionObject *func, PyObject *new_value) { - for (int i = 0; i < FUNC_MAX_WATCHERS; i++) { - PyFunction_WatchCallback cb = interp->func_watchers[i]; - if ((cb != NULL) && (cb(event, func, new_value) < 0)) { - PyErr_WriteUnraisable((PyObject *) func); + uint8_t bits = interp->active_func_watchers; + int i = 0; + while (bits) { + assert(i < FUNC_MAX_WATCHERS); + if (bits & 1) { + PyFunction_WatchCallback cb = interp->func_watchers[i]; + // callback must be non-null if the watcher bit is set + assert(cb != NULL); + if (cb(event, func, new_value) < 0) { + PyErr_WriteUnraisable((PyObject *) func); + } } + i++; + bits >>= 1; } } @@ -25,6 +34,7 @@ handle_func_event(PyFunction_WatchEvent event, PyFunctionObject *func, PyObject *new_value) { PyInterpreterState *interp = _PyInterpreterState_GET(); + assert(interp->_initialized); if (interp->active_func_watchers) { notify_func_watchers(interp, event, func, new_value); } diff --git a/Objects/typeobject.c b/Objects/typeobject.c index ae80f5a8fd..2c3b39521a 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -485,23 +485,24 @@ PyType_Modified(PyTypeObject *type) } } + // Notify registered type watchers, if any if (type->tp_watched) { PyInterpreterState *interp = _PyInterpreterState_GET(); int bits = type->tp_watched; int i = 0; - while(bits && i < TYPE_MAX_WATCHERS) { + while (bits) { + assert(i < TYPE_MAX_WATCHERS); if (bits & 1) { PyType_WatchCallback cb = interp->type_watchers[i]; if (cb && (cb(type) < 0)) { PyErr_WriteUnraisable((PyObject *)type); } } - i += 1; + i++; bits >>= 1; } } - type->tp_flags &= ~Py_TPFLAGS_VALID_VERSION_TAG; type->tp_version_tag = 0; /* 0 is not a valid version tag */ } -- cgit v1.2.1