[Python-checkins] CVS: python/dist/src/Objects descrobject.c,2.15,2.16 typeobject.c,2.84,2.85
Guido van Rossum
gvanrossum@users.sourceforge.net
2001年10月03日 05:09:32 -0700
Update of /cvsroot/python/python/dist/src/Objects
In directory usw-pr-cvs1:/tmp/cvs-serv24226/Objects
Modified Files:
descrobject.c typeobject.c
Log Message:
*EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
test dramatically:
class T(tuple): __dynamic__ = 1
t = T(range(1000))
for i in range(1000): tt = tuple(t)
The speedup was about 5x compared to the previous state of CVS (1.7
vs. 8.8, in arbitrary time units). But it's still more than twice as
slow as as the same test with __dynamic__ = 0 (0.8).
I'm not sure that I really want to go through the trouble of this kind
of speedup for every slot. Even doing it just for the most popular
slots will be a major effort (the new slot_sq_item is 40+ lines, while
the old one was one line with a powerful macro -- unfortunately the
speedup comes from expanding the macro and doing things in a way
specific to the slot signature).
An alternative that I'm currently considering is sketched in PLAN.txt:
trap setattr on type objects. But this will require keeping track of
all derived types using weak references.
Index: descrobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/descrobject.c,v
retrieving revision 2.15
retrieving revision 2.16
diff -C2 -d -r2.15 -r2.16
*** descrobject.c 2001年10月02日 21:24:57 2.15
--- descrobject.c 2001年10月03日 12:09:30 2.16
***************
*** 4,39 ****
#include "structmember.h" /* Why is this not included in Python.h? */
- /* Various kinds of descriptor objects */
-
- #define COMMON \
- PyObject_HEAD \
- PyTypeObject *d_type; \
- PyObject *d_name
-
- typedef struct {
- COMMON;
- } PyDescrObject;
-
- typedef struct {
- COMMON;
- PyMethodDef *d_method;
- } PyMethodDescrObject;
-
- typedef struct {
- COMMON;
- PyMemberDef *d_member;
- } PyMemberDescrObject;
-
- typedef struct {
- COMMON;
- PyGetSetDef *d_getset;
- } PyGetSetDescrObject;
-
- typedef struct {
- COMMON;
- struct wrapperbase *d_base;
- void *d_wrapped; /* This can be any function pointer */
- } PyWrapperDescrObject;
-
static void
descr_dealloc(PyDescrObject *descr)
--- 4,7 ----
***************
*** 482,486 ****
};
! static PyTypeObject PyWrapperDescr_Type = {
PyObject_HEAD_INIT(&PyType_Type)
0,
--- 450,454 ----
};
! PyTypeObject PyWrapperDescr_Type = {
PyObject_HEAD_INIT(&PyType_Type)
0,
Index: typeobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/typeobject.c,v
retrieving revision 2.84
retrieving revision 2.85
diff -C2 -d -r2.84 -r2.85
*** typeobject.c 2001年10月03日 00:50:18 2.84
--- typeobject.c 2001年10月03日 12:09:30 2.85
***************
*** 2114,2123 ****
int i;
! if (!PyArg_ParseTuple(args, "O", &arg))
! return NULL;
! i = getindex(self, arg);
! if (i == -1 && PyErr_Occurred())
! return NULL;
! return (*func)(self, i);
}
--- 2114,2127 ----
int i;
! if (PyTuple_GET_SIZE(args) == 1) {
! arg = PyTuple_GET_ITEM(args, 0);
! i = getindex(self, arg);
! if (i == -1 && PyErr_Occurred())
! return NULL;
! return (*func)(self, i);
! }
! PyArg_ParseTuple(args, "O", &arg);
! assert(PyErr_Occurred());
! return NULL;
}
***************
*** 2826,2830 ****
SLOT1(slot_sq_concat, "__add__", PyObject *, "O")
SLOT1(slot_sq_repeat, "__mul__", int, "i")
! SLOT1(slot_sq_item, "__getitem__", int, "i")
SLOT2(slot_sq_slice, "__getslice__", int, int, "ii")
--- 2830,2884 ----
SLOT1(slot_sq_concat, "__add__", PyObject *, "O")
SLOT1(slot_sq_repeat, "__mul__", int, "i")
!
! /* Super-optimized version of slot_sq_item.
! Other slots could do the same... */
! static PyObject *
! slot_sq_item(PyObject *self, int i)
! {
! static PyObject *getitem_str;
! PyObject *func, *args = NULL, *ival = NULL, *retval = NULL;
! descrgetfunc f;
!
! if (getitem_str == NULL) {
! getitem_str = PyString_InternFromString("__getitem__");
! if (getitem_str == NULL)
! return NULL;
! }
! func = _PyType_Lookup(self->ob_type, getitem_str);
! if (func != NULL) {
! if (func->ob_type == &PyWrapperDescr_Type) {
! PyWrapperDescrObject *wrapper =
! (PyWrapperDescrObject *)func;
! if (wrapper->d_base->wrapper == wrap_sq_item) {
! intargfunc f;
! f = (intargfunc)(wrapper->d_wrapped);
! return f(self, i);
! }
! }
! if ((f = func->ob_type->tp_descr_get) == NULL)
! Py_INCREF(func);
! else
! func = f(func, self, (PyObject *)(self->ob_type));
! ival = PyInt_FromLong(i);
! if (ival != NULL) {
! args = PyTuple_New(1);
! if (args != NULL) {
! PyTuple_SET_ITEM(args, 0, ival);
! retval = PyObject_Call(func, args, NULL);
! Py_XDECREF(args);
! Py_XDECREF(func);
! return retval;
! }
! }
! }
! else {
! PyErr_SetObject(PyExc_AttributeError, getitem_str);
! }
! Py_XDECREF(args);
! Py_XDECREF(ival);
! Py_XDECREF(func);
! return NULL;
! }
!
SLOT2(slot_sq_slice, "__getslice__", int, int, "ii")