[Patches] [ python-Patches-514662 ] On the update_slot() behavior

noreply@sourceforge.net noreply@sourceforge.net
Thu, 07 Feb 2002 20:49:57 -0800


Patches item #514662, was opened at 2002-02-07 20:49
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=305470&aid=514662&group_id=5470

Category: Core (C code)
Group: Python 2.3
Status: Open
Resolution: None
Priority: 5
Submitted By: Naofumi Honda (naofumi-h)
Assigned to: Nobody/Anonymous (nobody)
Summary: On the update_slot() behavior

Initial Comment:
Inherited method __getitem__ of list type
in the new subclass is unexpectedly slow. 

For example,

x = list([1,2,3])
r = xrange(1, 1000000)
for i in r:
        x[1] = 2

==> excution time: real    0m2.390s 

class nlist(list):
        pass

x = nlist([1,2,3])
r = xrange(1, 1000000)
for i in r:
        x[1] = 2

==> excution time: real    0m7.040s
about 3times slower!!!

The reason is:
for the __getitem__ attribute, there are
two slotdefs in typeobject.c
(one for the mapping type, and
the other for the sequence type).

In the creation of new_type of list type, 
fixup_slot_dispatchers() and update_slot() functions
in typeobject.c allocate the functions
to both sq_item and mp_subscript slots
(the mp_subscript slot had originally no function,
  because the list type is a sequence type),
 and it's an unexpected allocation for the mapping
 slot since the descriptor type of __getitem__
 is now WrapperType for the sequence operations.

If you will trace x[1] using gdb,
you will find that in PyObject_GetItem() 
m->mp_subscript = slot_mp_subscript 
is called instead of a sequece operation
because mp_subscript slot was allocated by
fixup_slot_dispatchers().
In the slot_mp_subscirpt(),
call_method(self, "__getitem__", ...) is invoked,
and turn out to call a wrapper descriptors for
the sq_item.

As a result, the method of list type finally called,
but it needs many unexpected function calls.

I will fix the behavior of fixup_slot_dispachers()
and update_slot() as follows:

Only the case where 
*) two or more slotdefs have the same attribute
   name where at most one corresponding slot
   has a non null pointer
*) the descriptor type of the attribute is
   WrapperType,

these functions will allocate the only one
function to the apropriate slot.

The other case, the behavior not changed
to keep compatiblity!
(in particular, considering the case where
  user override methods exist!)

The following patch also includes speed up routines
to find the slotdef duplications,
but it's not essential!


----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=305470&aid=514662&group_id=5470