-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert to fa_cache for node retriever (#203)
- Loading branch information
Showing
4 changed files
with
58 additions
and
7 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#ifndef LDB_HNSW_FA_CACHE_H | ||
#define LDB_HNSW_FA_CACHE_H | ||
#include <stddef.h> | ||
|
||
/* A fixed-size fully associative FIFO cache meant to be embedded | ||
* in other data structures for inline caching. | ||
* Currently a naive loop is used for lookup, but we could use | ||
* intrinsics to speed this up | ||
* We could also experiment with better cache replacement policies here | ||
* (CLOCK, 2 lists, etc) | ||
*/ | ||
|
||
#define FA_CACHE_SIZE 64 | ||
|
||
typedef struct | ||
{ | ||
int keys[ FA_CACHE_SIZE ]; | ||
void* values[ FA_CACHE_SIZE ]; | ||
int next; | ||
} FullyAssociativeCache; | ||
|
||
// Initalize the cache so all lookups return NULL | ||
static inline void fa_cache_init(FullyAssociativeCache* cache) | ||
{ | ||
// All values are set to NULL with the below | ||
// so if key 0 is looked up before key 0 is inserted | ||
// the data strucutre will returned the default value for key | ||
// which will be NULL | ||
MemSet(cache, 0, sizeof(FullyAssociativeCache)); | ||
} | ||
|
||
// Insert the key value pair into an already initialized cache | ||
static inline void fa_cache_insert(FullyAssociativeCache* cache, int key, void* value) | ||
{ | ||
cache->keys[ cache->next ] = key; | ||
cache->values[ cache->next ] = value; | ||
cache->next = (cache->next + 1) % FA_CACHE_SIZE; | ||
} | ||
|
||
// Get the value associated with the key | ||
static inline void* fa_cache_get(FullyAssociativeCache* cache, int key) | ||
{ | ||
for(int i = 0; i < FA_CACHE_SIZE; i++) { | ||
if(cache->keys[ i ] == key) { | ||
return cache->values[ i ]; | ||
} | ||
} | ||
return NULL; | ||
} | ||
|
||
#endif // LDB_HNSW_FA_CACHE_H |
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