Re: [eigen] Signed or unsigned indexing

[ Thread Index | Date Index | More Archives ]

On Sat, Jan 21, 2017 at 12:40 PM, Francois Fayard <fayard@xxxxxxxxxxxxx> wrote:

> On Jan 21, 2017, at 10:42 AM, Gael Guennebaud <gael.guennebaud@xxxxxxxxx> wrote:
> I remember we observed a clear speedup when we moved from int to ptrdiff_t in Eigen. That was before we release 2.0, so with much older compilers (gcc 4.2) and older hardware (core2).

I am not sure that you would see that with newer compilers. The reason is that the undefined behavior for overflow with signed integers allows 32-bit signed integers to be implemented as 64-bit signed integers when compiled if the compiler believes that this is useful. I am sure that this optimization was not made on earlier versions of compilers which is why you got a speedup at that time. I have not measured any difference with recent compilers except in very contrived examples like the one that is attached at the end of this email (with a recursive function).

That's easy to check, I simply measured:

void foo(MatrixXf &A, MatrixXf &B, Index i, Index j, Index n, Index m)
  A.block(i, j, n, m) += B.block(i+1, j+1, n, m);

and got 15% of speed difference between int and ptrdiff_t using either gcc6 or clang3.9.
> Actually, both questions are highly related because when you start mixing 32 and 64 bits integers, I have to admit that unsigned types win here because the conversion is a no op in this case, whereas signed types require a special treatment.

Even though I understand your point, I still disagree that unsigned integers win here. Performance is not always about counting operations, but more often about letting the compiler reason about the code. As I said before, a compiler is allowed to change your integer from 32-bit to 64-bit it thinks that it makes a difference. It is not allowed with unsigned integers.. Have a look at the concrete example from bzip2 given by Chandler Carruth at 39:20 on

I measured the conversion of 1000 integers from 'int' or 'unsigned int' to 'long' and observe 25% of speed difference using either clang or gcc (in favour of unsigned int). But, when used to index another buffer, like data[indices[i]] as in sparse stuff, I cannot see any difference anymore.


Mail converted by MHonArc 2.6.19+