Skip to content

Commit ceaab80

Browse files
frmdstryrMatthieuDartiailh
authored andcommitted
Refactor defaultvaluebehavior to avoid unnecessary allocations
1 parent 23aaa52 commit ceaab80

File tree

1 file changed

+7
-41
lines changed

1 file changed

+7
-41
lines changed

atom/src/defaultvaluebehavior.cpp

Lines changed: 7 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -143,77 +143,43 @@ non_optional_handler( Member* member, CAtom* atom )
143143
PyObject*
144144
call_object_handler( Member* member, CAtom* atom )
145145
{
146-
cppy::ptr callable( cppy::incref( member->default_value_context ) );
147-
cppy::ptr args( PyTuple_New( 0 ) );
148-
if( !args )
149-
return 0;
150-
return callable.call( args );
146+
return PyObject_CallNoArgs( member->default_value_context );
151147
}
152148

153149

154150
PyObject*
155151
call_object_object_handler( Member* member, CAtom* atom )
156152
{
157-
cppy::ptr callable( cppy::incref( member->default_value_context ) );
158-
cppy::ptr args( PyTuple_New( 1 ) );
159-
if( !args )
160-
return 0;
161-
PyTuple_SET_ITEM( args.get(), 0, cppy::incref( pyobject_cast( atom ) ) );
162-
return callable.call( args );
153+
return PyObject_CallOneArg( member->default_value_context, pyobject_cast( atom ) );
163154
}
164155

165156

166157
PyObject*
167158
call_object_object_name_handler( Member* member, CAtom* atom )
168159
{
169-
cppy::ptr callable( cppy::incref( member->default_value_context ) );
170-
cppy::ptr args( PyTuple_New( 2 ) );
171-
if( !args )
172-
return 0;
173-
PyTuple_SET_ITEM( args.get(), 0, cppy::incref( pyobject_cast( atom ) ) );
174-
PyTuple_SET_ITEM( args.get(), 1, cppy::incref( member->name ) );
175-
return callable.call( args );
160+
PyObject* args[] = { pyobject_cast( atom ), member->name };
161+
return PyObject_Vectorcall( member->default_value_context, args, 2, 0 );
176162
}
177163

178164

179165
PyObject*
180166
object_method_handler( Member* member, CAtom* atom )
181167
{
182-
cppy::ptr callable( PyObject_GetAttr( pyobject_cast( atom ), member->default_value_context ) );
183-
if( !callable )
184-
return 0;
185-
cppy::ptr args( PyTuple_New( 0 ) );
186-
if( !args )
187-
return 0;
188-
return callable.call( args );
168+
return PyObject_CallMethodNoArgs( pyobject_cast( atom ), member->default_value_context );
189169
}
190170

191171

192172
PyObject*
193173
object_method_name_handler( Member* member, CAtom* atom )
194174
{
195-
cppy::ptr callable( PyObject_GetAttr( pyobject_cast( atom ), member->default_value_context ) );
196-
if( !callable )
197-
return 0;
198-
cppy::ptr args( PyTuple_New( 1 ) );
199-
if( !args )
200-
return 0;
201-
PyTuple_SET_ITEM( args.get(), 0, cppy::incref( member->name ) );
202-
return callable.call( args );
175+
return PyObject_CallMethodOneArg( pyobject_cast( atom ), member->default_value_context, member->name );
203176
}
204177

205178

206179
PyObject*
207180
member_method_object_handler( Member* member, CAtom* atom )
208181
{
209-
cppy::ptr callable( PyObject_GetAttr( pyobject_cast( member ), member->default_value_context ) );
210-
if( !callable )
211-
return 0;
212-
cppy::ptr args( PyTuple_New( 1 ) );
213-
if( !args )
214-
return 0;
215-
PyTuple_SET_ITEM( args.get(), 0, cppy::incref( pyobject_cast( atom ) ) );
216-
return callable.call( args );
182+
return PyObject_CallMethodOneArg( pyobject_cast( member ), member->default_value_context, pyobject_cast( atom ) );
217183
}
218184

219185

0 commit comments

Comments
 (0)