Last active
September 19, 2016 01:30
-
-
Save scottt/b6e66c27efca9637ee6748e157048227 to your computer and use it in GitHub Desktop.
Explaining the Data Dependency Issue Exposed in "A cache miss is not a cache miss"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct pair { int first, int second }; | |
/* LOAD() is just a pointer dereference. Written this way to make it stand out. */ | |
#define LOAD(expr) (*(expr)) | |
void iterate_linked_list(vector) | |
{ | |
register void *vector_start = vector->start; | |
register long index = 0; | |
register long sum = 0; | |
register void *ppair; | |
LOOP: ppair = vector_start + index*sizeof(struct pair); | |
value = LOAD(ppair + sizeof(int)); /* pair.second */ | |
sum += value; | |
index = LOAD(ppair); /* pair.first */ | |
if (index != 0) | |
goto LOOP; | |
/* Data dependency between loop iterations | |
* "value = LOAD(ppair)" depends on the previous iteration of itself: | |
* | |
* "value = LOAD(ppair)" depends on 'ppair' | |
* 'ppair' depends on 'index' | |
* 'index' depends on the previous iteration of "LOAD(ppair)". | |
* | |
* This data dependency doesn't exist in iterate_pointers(). */ | |
OUTPUT: output = sum; | |
return; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void iterate_pointers(vector) | |
{ | |
register void *vector_start = vector->start; | |
register void *vector_finish = vector->finish; | |
register void *ppair = vecotr_start; | |
register long sum = 0; | |
LOOP: if (ppair == vector_finish) | |
goto OUTPUT; | |
index = LOAD(ppair); | |
value = LOAD(vector_start + index*sizeof(struct pair) + sizeof(int)); | |
sum += value; | |
ppair += sizeof(struct pair); | |
goto LOOP; | |
OUTPUT: output = sum; | |
return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment