Skip to content

Draft: create CAtomMeta to avoid getattr when creating Atoms #244

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions atom/atom.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,6 @@ class Atom(CAtom, metaclass=AtomMeta):

"""

__atom_members__: ClassVar[Mapping[str, Member]]

@classmethod
def members(cls) -> Mapping[str, Member]:
"""Get the members dictionary for the type.

Returns
-------
result : Mapping[str, Member]
The dictionary of members defined on the class. User code
should not modify the contents of the dict.

"""
return cls.__atom_members__

@contextmanager
def suppress_notifications(self) -> Iterator[None]:
"""Disable member notifications within in a context.
Expand Down
6 changes: 3 additions & 3 deletions atom/meta/atom_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

from ..catom import (
CAtom,
CAtomMeta,
DefaultValue,
GetState,
Member,
Expand Down Expand Up @@ -63,7 +64,7 @@ def add_member(cls: "AtomMeta", name: str, member: Member) -> None:

member.set_name(name)
# The dict is mutable but we do not want to say it too loud
cls.__atom_members__[name] = member # type: ignore
cls.__add_member(name, member) # type: ignore
cls.__atom_specific_members__ = frozenset(
set(cls.__atom_specific_members__) | {name}
)
Expand Down Expand Up @@ -501,7 +502,7 @@ def create_class(self, meta: type) -> type:
return cls


class AtomMeta(type):
class AtomMeta(CAtomMeta):
"""The metaclass for classes derived from Atom.

This metaclass computes the memory layout of the members in a given
Expand All @@ -515,7 +516,6 @@ class so that the CAtom class can allocate exactly enough space for

"""

__atom_members__: Mapping[str, Member]
__atom_specific_members__: FrozenSet[str]

def __new__(
Expand Down
65 changes: 38 additions & 27 deletions atom/src/catom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <cppy/cppy.h>
#include "atomref.h"
#include "catom.h"
#include "catommeta.h"
#include "globalstatic.h"
#include "methodwrapper.h"
#include "packagenaming.h"
Expand All @@ -41,20 +42,17 @@ static PyObject* atom_flags;
PyObject*
CAtom_new( PyTypeObject* type, PyObject* args, PyObject* kwargs )
{
cppy::ptr membersptr( PyObject_GetAttr( pyobject_cast( type ), atom_members ) );
if( !membersptr )
return 0;
if( !PyDict_CheckExact( membersptr.get() ) )
return cppy::system_error( "atom members" );
if ( !CAtomMeta::TypeCheck( pyobject_cast(type) ) )
return cppy::type_error("catommeta");
cppy::ptr selfptr( PyType_GenericNew( type, args, kwargs ) );
if( !selfptr )
return 0;
CAtom* atom = catom_cast( selfptr.get() );
uint32_t count = static_cast<uint32_t>( PyDict_Size( membersptr.get() ) );
if( count > 0 )

if( uint16_t count = catommeta_cast(type)->slot_count )
{
if( count > MAX_MEMBER_COUNT )
return cppy::type_error( "too many members" );
// As long as the types of CAtomMeta::slot_count and CAtom::slot_count
// are the same the `count < MAX_SLOT_COUNT` does not need checked here
size_t size = sizeof( PyObject* ) * count;
void* slots = PyObject_MALLOC( size );
if( !slots )
Expand Down Expand Up @@ -172,19 +170,29 @@ CAtom_set_notifications_enabled( CAtom* self, PyObject* arg )


PyObject*
CAtom_get_member( PyObject* self, PyObject* name )
CAtom_get_member( PyObject* cls, PyObject* name )
{
if( !PyUnicode_Check( name ) )
return cppy::type_error( name, "str" );
cppy::ptr membersptr( PyObject_GetAttr( pyobject_cast( Py_TYPE(self) ), atom_members ) );
if( !membersptr )
return 0;
if( !PyDict_CheckExact( membersptr.get() ) )
return cppy::system_error( "atom members" );
cppy::ptr member( cppy::xincref( PyDict_GetItem( membersptr.get(), name ) ) );
if( !member )
Py_RETURN_NONE;
return member.release();
if ( !CAtomMeta::TypeCheck(cls) )
return cppy::type_error("get_members must be called on a CAtomMeta instance");
return catommeta_cast( cls )->get_member(name);
}


PyObject*
CAtom_members( PyObject* cls )
{
if ( !CAtomMeta::TypeCheck(cls) )
return cppy::type_error("members must be called on a CAtomMeta instance");
return catommeta_cast( cls )->members();
}


PyObject*
CAtom_init_subclass( PyObject* cls )
{
if ( !CAtomMeta::TypeCheck(cls) )
return cppy::type_error("init_subclass must be called on a CAtomMeta instance");
return catommeta_cast( cls )->init_subclass();
}


Expand Down Expand Up @@ -412,13 +420,12 @@ CAtom_getstate( CAtom* self )
}
}

cppy::ptr membersptr = selfptr.getattr(atom_members);
if ( !membersptr || !PyDict_CheckExact( membersptr.get() ) ) {
return cppy::system_error( "atom members" );
}

cppy::ptr membersptr( catommeta_cast( Py_TYPE(selfptr.get()) )->members() );
if ( !membersptr )
return 0;
PyObject *name, *member;
Py_ssize_t pos = 0;
utils::CriticalSection lock(membersptr.get());
while ( PyDict_Next(membersptr.get(), &pos, &name, &member) ) {
cppy::ptr should_gs = member_cast( member )->should_getstate( self );
if ( !should_gs ) {
Expand Down Expand Up @@ -490,8 +497,10 @@ CAtom_methods[] = {
"Get whether notification is enabled for the atom." },
{ "set_notifications_enabled", ( PyCFunction )CAtom_set_notifications_enabled, METH_O,
"Enable or disable notifications for the atom." },
{ "get_member", ( PyCFunction )CAtom_get_member, METH_O,
{ "get_member", ( PyCFunction )CAtom_get_member, METH_CLASS | METH_O,
"Get the named member for the atom." },
{ "members", ( PyCFunction )CAtom_members, METH_CLASS | METH_NOARGS,
"Get members for the atom." },
{ "observe", ( PyCFunction )CAtom_observe, METH_FASTCALL,
"Register an observer callback to observe changes on the given topic(s)." },
{ "unobserve", ( PyCFunction )CAtom_unobserve, METH_FASTCALL,
Expand All @@ -510,6 +519,8 @@ CAtom_methods[] = {
"The base implementation of the pickle getstate protocol." },
{ "__setstate__", ( PyCFunction )CAtom_setstate, METH_O,
"The base implementation of the pickle setstate protocol." },
{ "__init_subclass__", ( PyCFunction )CAtom_init_subclass, METH_CLASS | METH_NOARGS,
"Initialize the atom_members for the subclass." },
{ 0 } // sentinel
};

Expand Down
Loading
Loading