Skip to content

Commit

Permalink
Revert to fa_cache for node retriever (#203)
Browse files Browse the repository at this point in the history
  • Loading branch information
var77 authored Oct 13, 2023
1 parent 0c0aceb commit 3474f82
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 7 deletions.
8 changes: 4 additions & 4 deletions src/hnsw/external_index.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <access/generic_xlog.h> // GenericXLog
#include <assert.h>
#include <common/relpath.h>
#include <hnsw/fa_cache.h>
#include <pg_config.h> // BLCKSZ
#include <storage/bufmgr.h> // Buffer
#include <utils/hsearch.h>
Expand Down Expand Up @@ -583,8 +584,7 @@ void *ldb_wal_index_node_retriever(void *ctxp, int id)
OffsetNumber offset, max_offset;
Buffer buf = InvalidBuffer;
bool idx_page_prelocked = false;
void *cached_node = cache_get_item(&ctx->node_cache, &id);

void *cached_node = fa_cache_get(&ctx->fa_cache, id);
if(cached_node != NULL) {
return cached_node;
}
Expand Down Expand Up @@ -629,7 +629,7 @@ void *ldb_wal_index_node_retriever(void *ctxp, int id)
LockBuffer(buf, BUFFER_LOCK_UNLOCK);
}

cache_set_item(&ctx->node_cache, &id, nodepage->node);
fa_cache_insert(&ctx->fa_cache, id, nodepage->node);

return nodepage->node;
#endif
Expand Down Expand Up @@ -671,7 +671,7 @@ void *ldb_wal_index_node_retriever_mut(void *ctxp, int id)
for(offset = FirstOffsetNumber; offset <= max_offset; offset = OffsetNumberNext(offset)) {
nodepage = (HnswIndexTuple *)PageGetItem(page, PageGetItemId(page, offset));
if(nodepage->id == (uint32)id) {
cache_set_item(&ctx->node_cache, &id, nodepage->node);
fa_cache_insert(&ctx->fa_cache, id, nodepage->node);

return nodepage->node;
}
Expand Down
3 changes: 2 additions & 1 deletion src/hnsw/external_index.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <utils/relcache.h> // Relation

#include "extra_dirtied.h"
#include "fa_cache.h"
#include "hnsw.h"
#include "htab_cache.h"
#include "options.h"
Expand Down Expand Up @@ -89,7 +90,7 @@ typedef struct

ExtraDirtiedBufs *extra_dirted;

HTABCache node_cache;
FullyAssociativeCache fa_cache;

dlist_head takenbuffers;
} RetrieverCtx;
Expand Down
51 changes: 51 additions & 0 deletions src/hnsw/fa_cache.h
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
3 changes: 1 addition & 2 deletions src/hnsw/retriever.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ RetrieverCtx *ldb_wal_retriever_area_init(Relation index_rel, HnswIndexHeaderPag
ctx->header_page_under_wal = header_page_under_wal;
ctx->extra_dirted = extra_dirtied_new();

ctx->node_cache = cache_create("NodeCache");
fa_cache_init(&ctx->fa_cache);

dlist_init(&ctx->takenbuffers);

Expand Down Expand Up @@ -55,7 +55,6 @@ void ldb_wal_retriever_area_reset(RetrieverCtx *ctx, HnswIndexHeaderPage *header
void ldb_wal_retriever_area_fini(RetrieverCtx *ctx)
{
cache_destroy(&ctx->block_numbers_cache);
cache_destroy(&ctx->node_cache);
dlist_mutable_iter miter;
dlist_foreach_modify(miter, &ctx->takenbuffers)
{
Expand Down

0 comments on commit 3474f82

Please sign in to comment.