[Python-checkins] r75714 - python/trunk/Python/bltinmodule.c

Mark Dickinson dickinsm at gmail.com
Mon Oct 26 23:23:39 CET 2009


On Mon, Oct 26, 2009 at 9:39 PM, Nick Coghlan <ncoghlan at gmail.com> wrote:
> mark.dickinson wrote:
>> Modified: python/trunk/Python/bltinmodule.c
>> ==============================================================================
>> --- python/trunk/Python/bltinmodule.c (original)
>> +++ python/trunk/Python/bltinmodule.c Mon Oct 26 15:18:44 2009
>> @@ -2350,6 +2350,15 @@
>>                       }
>>                       break;
>>               }
>> +             /* It's tempting to use PyNumber_InPlaceAdd instead of
>> +                PyNumber_Add here, to avoid quadratic running time
>> +                when doing 'sum(list_of_lists, [])'.  However, this
>> +                would produce a change in behaviour: a snippet like
>> +
>> +                  empty = []
>> +                  sum([[x] for x in range(10)], empty)
>> +
>> +                would change the value of empty. */
>>               temp = PyNumber_Add(result, item);
>>               Py_DECREF(result);
>>               Py_DECREF(item);
>
> Could you get the best of both worlds by switching to in-place add only
> after the first pass through the addition loop?

Quite possibly yes;  I think that's a discussion for the issue tracker
somewhere, though.

I added this comment because (1) it took me a long time to figure out
why making this change wasn't a good idea, and (2) I later discovered
I wasn't alone: this exact change *was* made back in October 2003 [1],
and then had to be reverted.  So it seemed a good idea to make sure
no-one else had the same 'bright idea'.

Various alternatives were considered back then too, but I don't think
anyone found a solution that was universally acceptable.

Mark

[1] http://mail.python.org/pipermail/python-dev/2003-October/039511.html


More information about the Python-checkins mailing list