GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Argument clinic: improve generated code for self converter type checks · Issue #101409 · python/cpython Closed Labels performancePerformance or resource usagePerformance or resource usagetopic-argument-clinictype-featureA feature request or enhancementA feature request or enhancement DescriptionFeature or enhancementCurrently, typically for if kind == METHOD_NEW: type_check = ('({0} == {1} ||\n ' ' {0}->tp_init == {2}tp_init)' ).format(self.name, type_object, prefix) else: type_check = ('(Py_IS_TYPE({0}, {1}) ||\n ' ' Py_TYPE({0})->tp_new == {2}tp_new)' ).format(self.name, type_object, prefix)
For heap types with module state, you'd typically do something like this: typedef struct { ... } myclass_object; typedef struct { PyTypeObject myclass_type; } module_state; static inline module_state * find_state_by_type(PyTypeObject *tp) { PyObject *mod = PyType_GetModuleByDef(&moduledef); // Potentially slow! void *state = PyModule_GetState(mod); return (module_state*)state; } #define clinic_state() (find_state_by_type(type)) #include "clinic/mymod.c.h" #undef clinic_state /*[clinic input] module mymod class mymod.myclass "myclass_object *" "clinic_state()->myclass_type" [clinic start generated code]*/ Currently, this generates clinic code like this: mymod_myclass_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) { ... if ((type == clinic_state()->RowType || type->tp_init == clinic_state()->RowType->tp_init) && ... potentially calling PitchSuggesting to modify clinic to store the self type pointer in a local variable, and use that variable in the self type check. Proof-of-concept diff from the diff --git a/Modules/_sqlite/clinic/cursor.c.h b/Modules/_sqlite/clinic/cursor.c.h index 36b8d0051a..633ad2e73d 100644 --- a/Modules/_sqlite/clinic/cursor.c.h +++ b/Modules/_sqlite/clinic/cursor.c.h @@ -16,10 +16,11 @@ static int pysqlite_cursor_init(PyObject *self, PyObject *args, PyObject *kwargs) { int return_value = -1; + PyTypeObject *self_tp = clinic_state()->CursorType; pysqlite_Connection *connection; - if ((Py_IS_TYPE(self, clinic_state()->CursorType) || - Py_TYPE(self)->tp_new == clinic_state()->CursorType->tp_new) && + if ((Py_IS_TYPE(self, self_tp) || + Py_TYPE(self)->tp_new == self_tp->tp_new) && !_PyArg_NoKeywords("Cursor", kwargs)) { goto exit; } @@ -318,4 +319,4 @@ pysqlite_cursor_close(pysqlite_Cursor *self, PyObject *Py_UNUSED(ignored)) { return pysqlite_cursor_close_impl(self); } -/*[clinic end generated code: output=e53e75a32a9d92bd input=a9049054013a1b77]*/ +/*[clinic end generated code: output=e5eac0cbe29c88ad input=a9049054013a1b77]*/ Previous discussionLinked PRsActivity
MetadataMetadataAssigneesLabelsperformancePerformance or resource usagePerformance or resource usagetopic-argument-clinictype-featureA feature request or enhancementA feature request or enhancement ProjectsMilestoneRelationshipsDevelopmentIssue actions |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |