std::vector<> considered harmful

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

elcabesa
Posts: 855
Joined: Sun May 23, 2010 1:32 pm

Re: std::vector<> considered harmful

Post by elcabesa »

AlvaroBegue wrote:
elcabesa wrote:yes but saving a reference should be faster than every time you need an information access the data by indices.
Do you say that because you have measured it or because of some intuition? Don't trust your intuitions: The processor has addressing modes that make this kind of access really fast, at least if the element size is a small power of two.
I don't remember, I think intuition
User avatar
stegemma
Posts: 859
Joined: Mon Aug 10, 2009 10:05 pm
Location: Italy
Full name: Stefano Gemma

Re: std::vector<> considered harmful

Post by stegemma »

elcabesa wrote:
AlvaroBegue wrote:
elcabesa wrote:yes but saving a reference should be faster than every time you need an information access the data by indices.
Do you say that because you have measured it or because of some intuition? Don't trust your intuitions: The processor has addressing modes that make this kind of access really fast, at least if the element size is a small power of two.
I don't remember, I think intuition
Often the compiler can translate your indexed access to a fast pointer addition, as said by AlvaroBegue. The Intel CPU indexing unit is separated from the normal math unit, so any indexing in the form of:

base + index * power of two

can be executed in parallel with normal operation between registers and doesn't lock the processor pipes.

Keeping your structures well aligned can give you a gain in performance. In my old assembly programs, i used to add unused bytes to align the structures to powers of 2. The same i'm doing in Satana (C++) but i haven't verified the gain, if there is one. In fact, i use a lot of pointers and rarely i access an array by indexes, so i suppose that aligning on 8 bytes boundaries would be more than enough, opposite to power of 2 alignment (ok, 8 is a power of two but 3 x 8 bytes elements is not.. 4 x 8 bytes is power of 2 aligned).