On Tue, 5 Aug 2008, Dag Sverre Seljebotn wrote:
> Robert Bradshaw wrote:
>> On Mon, 4 Aug 2008, Dag Sverre Seljebotn wrote:
>>
>>> Stefan Behnel wrote:
>>>> Dag Sverre Seljebotn wrote:
>>>>> If we make positive int literals unsigned long rather than plain long,
>>>>> constant-index buffer lookups are going to be optimized for free. Is
>>>>> there going to be any ill side-effects from doing this?
>>>>>
>>>>> (If not, I can always special-case int literals used for buffer lookups.)
>>>> What about enabling this, adding a test module dedicated to integer
>>>> literals, casting and arithmetic, and running the (complete) test suite
>>>> with
>>>>
>>>> -Wall -pedantic
>>>>
>>>> ?
>>> Sure, but that would have been a lot of work only to get a reply here
>>> afterwards that "this whole idea would obviously not work because X"
>>> from people who know the code base better :-)
>>
>> I think in general that is a good idea, but the problem is that
>> PyLong_FromUnsignedLong is slower than PyInt_FromLong and produces python
>> longs rather than python ints. One would need a special type (or flag) to
>> indicate that the top bit can be assumed to be 0.
>
> And there's my X.
>
> Do you see any weaknesses in this? (as a general, somewhat orthogonal
> runtime optimization ... the hit should be negligible compared to
> PyInt_FromUnsignedLong, or...?):
>
> static INLINE PyObject* __Pyx_ObjectFromUnsignedLong(unsigned long value) {
> if (top bit of value is 0) return PyInt_FromLong((long)value);
> else return PyLong_FromUnsignedLong(value);
> }
>
It would bloat the code a bit every time a literal integer is used as a
Python object. I'm not sure if that's a big deal though.
Another approach would be for the indexing code to be able to
have some logic that detects the index is positive (which could be
extended to apply to loop variables once control flow in in place for
example), or just to special case this one (common) example.
- Robert
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev