Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: cache optimization

Author: Dan Andersson

Date: 11:09:44 06/08/01

Go up one level in this thread


On June 08, 2001 at 12:52:55, martin fierz wrote:

>hi bob,
>
>>>Small, local variables in a function do not have cache problems.
>>>The problem is when you have large tables with data. Then you need to worry
>>>about how the data is organized.
>>>
>>>/Johan Melin
>>
>>But they _do_.  If you put 8 "small local variables" together in memory,
>>whenever one is referenced the other 7 will be brought into cache at the same
>>time.  When you use them, you use a cache cycle rather than a memory cycle.
>>That basically means that the first variable costs you a memory cycle, the
>>other 7 cost you nothing whatsoever to use.
>>
>>Small savings add up over millions of repetitions...
>
>if i give you some sample code:
>
>int somefunction(int p1)
> {
> int x1,x2,x3...x20;
To be certain of their locality you would have to have all variables in a
struct.
>
> x1=something;
> /* can i assume that now x2..x8 are in my cache?*/
You can assume that 32 bytes of data including x1 will be in cashe nothing more.
> x5=something;
> /* and now still x1..x8 because probably x5 was in cache?*/
Assuming that the variables x? variables are contignuous the chance is 1/8, if
they are aligned in some way the chance could be up to  as much as 1/2.
> x12=something else;
> /* and now x12..x19 are in cache?*/
The probabilties steadily decrease and iff (!) x? are located in order anything
from x5..x12 to x12..19 could be in cashe.
> }
>
>are these comments reasonable? i know you wrote that there is no guarantee
>that the variables will be allocated this way, but probably this is the best
>guess i have!?
>
>cheers
>  martin



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.