I know this is more c related but this kind of optimization should only be an issue in a very high performance application(i.e. a graphics app),so if anyone knows,you know.Anyway say I have a strucure which is going to be accessed a lot so it makes sense to keep it as small as possible so that it will be more cache-friendly etc.
typedef struct fatstruct{
int this[3];
int that[3];
.
.
.
} fatstruct_t;
To this struct I need to add 2 integers, flags and level which both need not be more than 8 bits big.The performance of reading/writing level isn’t so important but flags is very important.What is the fastest to do:
a)put them both in one int(short might be slower due to alignment/integral promotion issues) named say levelnflags.Then I can have the level in the first byte which can be accessed like this:
fat.levelnflags&0xff
for reading.Writing might be a little more complicated but as I said performance of level isn’t so important.Flags can be accessed like usual:
fat.levelnflags
b)keep them in two seperate shorts, level and flags.This would be much cleaner to do and potentially faster.Memory alignment wouldn’t be much of an issue if I put flags on a 4 byte boundary but would accessing flags for reading or writing be slower due to integral promotion or whatever?
c)maybe bit fields could help?I’ve never actually used them so far.
d)any of the above
e)none of the above
So which one would be faster?The reason I’m making such a fuss about this is that this struct is the triangle struct in a ROAM renderer.Due to heavy bookkeeping involved with ROAM surfaces some parts of it tend to become IO bound rather than CPU bound.For example culling a tri involves reading and updating this flags byte for each culled tri.For 10000 tris per frame and 60 fps this may well take more than the actual computations needed.
[This message has been edited by zen (edited 06-28-2002).]