From 5cb71b27e6a041d57d67451a7e669f82dffff67a Mon Sep 17 00:00:00 2001 From: frmdstryr Date: Mon, 20 Jan 2025 13:06:38 -0500 Subject: [PATCH] Update catom to use fastcalls --- atom/src/catom.cpp | 42 ++++++++++++++++++------------------------ 1 file changed, 18 insertions(+), 24 deletions(-) diff --git a/atom/src/catom.cpp b/atom/src/catom.cpp index 23df7d32..f5c43185 100644 --- a/atom/src/catom.cpp +++ b/atom/src/catom.cpp @@ -189,19 +189,18 @@ CAtom_get_member( PyObject* self, PyObject* name ) PyObject* -CAtom_observe( CAtom* self, PyObject* args ) +CAtom_observe( CAtom* self, PyObject*const *args, Py_ssize_t n ) { - const size_t n = PyTuple_GET_SIZE( args ); if( n < 2 || n > 3) return cppy::type_error( "observe() takes exactly 2 or 3 arguments" ); - PyObject* topic = PyTuple_GET_ITEM( args, 0 ); - PyObject* callback = PyTuple_GET_ITEM( args, 1 ); + PyObject* topic = args[0]; + PyObject* callback = args[1]; if( !PyCallable_Check( callback ) ) return cppy::type_error( callback, "callable" ); uint8_t change_types = ChangeType::Any; if ( n == 3 ) { - PyObject* types = PyTuple_GET_ITEM( args, 2 ); + PyObject* types = args[2]; if( !PyLong_Check( types ) ) return cppy::type_error( types, "int" ); change_types = PyLong_AsLong( types ) & 0xFF; @@ -300,17 +299,15 @@ _CAtom_unobserve_2( CAtom* self, PyObject* topic, PyObject* callback ) PyObject* -CAtom_unobserve( CAtom* self, PyObject* args ) +CAtom_unobserve( CAtom* self, PyObject*const *args, Py_ssize_t n_args ) { - Py_ssize_t n_args = PyTuple_GET_SIZE( args ); - if( n_args > 2 ) - return cppy::type_error( "unobserve() takes at most 2 arguments" ); if( n_args == 0 ) return _CAtom_unobserve_0( self ); if( n_args == 1 ) - return _CAtom_unobserve_1( self, PyTuple_GET_ITEM( args, 0 ) ); - return _CAtom_unobserve_2( self, PyTuple_GET_ITEM( args, 0 ), - PyTuple_GET_ITEM( args, 1 ) ); + return _CAtom_unobserve_1( self, args[0] ); + if ( n_args == 2) + return _CAtom_unobserve_2( self, args[0], args[1] ); + return cppy::type_error( "unobserve() takes at most 2 arguments" ); } @@ -322,12 +319,12 @@ CAtom_has_observers( CAtom* self, PyObject* topic ) PyObject* -CAtom_has_observer( CAtom* self, PyObject* args ) +CAtom_has_observer( CAtom* self, PyObject*const *args, Py_ssize_t n ) { - if( PyTuple_GET_SIZE( args ) != 2 ) + if( n != 2 ) return cppy::type_error( "has_observer() takes exactly 2 arguments" ); - PyObject* topic = PyTuple_GET_ITEM( args, 0 ); - PyObject* callback = PyTuple_GET_ITEM( args, 1 ); + PyObject* topic = args[0]; + PyObject* callback = args[1]; if( !utils::str_check( topic ) ) return cppy::type_error( topic, "str" ); if( !PyCallable_Check( callback ) ) @@ -448,11 +445,8 @@ CAtom_getstate( CAtom* self ) } PyObject* -CAtom_setstate( CAtom* self, PyObject* args ) +CAtom_setstate( CAtom* self, PyObject* state ) { - if( PyTuple_GET_SIZE( args ) != 1 ) - return cppy::type_error( "__setstate__() takes exactly one argument" ); - PyObject* state = PyTuple_GET_ITEM( args, 0 ); cppy::ptr itemsptr = PyMapping_Items(state); if ( !itemsptr ) return 0; @@ -488,13 +482,13 @@ CAtom_methods[] = { "Enable or disable notifications for the atom." }, { "get_member", ( PyCFunction )CAtom_get_member, METH_O, "Get the named member for the atom." }, - { "observe", ( PyCFunction )CAtom_observe, METH_VARARGS, + { "observe", ( PyCFunction )CAtom_observe, METH_FASTCALL, "Register an observer callback to observe changes on the given topic(s)." }, - { "unobserve", ( PyCFunction )CAtom_unobserve, METH_VARARGS, + { "unobserve", ( PyCFunction )CAtom_unobserve, METH_FASTCALL, "Unregister an observer callback for the given topic(s)." }, { "has_observers", ( PyCFunction )CAtom_has_observers, METH_O, "Get whether the atom has observers for a given topic." }, - { "has_observer", ( PyCFunction )CAtom_has_observer, METH_VARARGS, + { "has_observer", ( PyCFunction )CAtom_has_observer, METH_FASTCALL, "Get whether the atom has the given observer for a given topic." }, { "notify", ( PyCFunction )CAtom_notify, METH_VARARGS | METH_KEYWORDS, "Call the registered observers for a given topic with positional and keyword arguments." }, @@ -504,7 +498,7 @@ CAtom_methods[] = { "__sizeof__() -> size of object in memory, in bytes" }, { "__getstate__", ( PyCFunction )CAtom_getstate, METH_NOARGS, "The base implementation of the pickle getstate protocol." }, - { "__setstate__", ( PyCFunction )CAtom_setstate, METH_VARARGS, + { "__setstate__", ( PyCFunction )CAtom_setstate, METH_O, "The base implementation of the pickle setstate protocol." }, { 0 } // sentinel };