views:

67

answers:

2

Hello,

If one has a big structure, having lot of member variables. Some function needs to access 4-5 elements in the structure for its working, so which of the below scenario could be cache effective(less cache misses)-

1.) Pass the pointer to the structure as argument to the function, Which in turn will access the needed elements.(Assume that the elements are not continuous in the structure declaration and they are apart)

2.) Pass individual structure member variables as argument to the function.

In first place, Does this scenario affect performance of the code from cache perspective in first place?

Thanks.

-AD

+1  A: 

Ignoring cache issues, passing a pointer will always be fastest, as there is no overhead of copying the interesting fields.

anon
Passing structure instance is surely not a option here for obvious reasons of copying-cycles and memory overheads.
goldenmean
and your point is...what? I said "passing a pointer"
anon
+1  A: 

Well ... If the members accessed are many cache lines apart, then it would probably help to get them all collected (on the stack, or even in registers if possible) as arguments, if the function does many accesses. If not, the extra overhead of reading out the arguments and setting up the call might eat up the benefit.

I think this is a micro-optimization, and that you should profile both cases, and then document any change to the code that you do as a result of said profiling (since it won't be obvious to the casual observer, later on).

unwind