Skip to content

Commit

Permalink
Use a session-global RNG from Postgres to generate new levels (#172)
Browse files Browse the repository at this point in the history
Fixes #112
  • Loading branch information
therealdarkknight authored and Ngalstyan4 committed Sep 25, 2023
1 parent d3ba5b5 commit f329ce5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
23 changes: 22 additions & 1 deletion src/hnsw/insert.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

#include <access/generic_xlog.h>
#include <assert.h>
#if PG_VERSION_NUM >= 150000
#include <common/pg_prng.h>
#endif
#include <float.h>
#include <math.h>
#include <storage/bufmgr.h>
#include <utils/array.h>
#include <utils/rel.h>
Expand All @@ -19,6 +23,23 @@
#include "utils.h"
#include "vector.h"

/*
* Generate a random level for a new externally stored vector
*/
static uint32 hnsw_generate_new_level(size_t connectivity)
{
double inverse_log_connectivity = 1.0 / log((double)connectivity);
// note: RNG is initialized (via srandom or via an updated mechanism) in postmaster.c
// we want rand_num to be in range [0.0, 1.0)
#if PG_VERSION_NUM >= 150000
double rand_num = pg_prng_double(&pg_global_prng_state);
#else
double rand_num = (double)random() / (double)MAX_RANDOM_VALUE;
#endif
double level = -1 * log(rand_num) * inverse_log_connectivity;
return (uint32)level;
}

/*
* Insert a tuple into the index
*/
Expand Down Expand Up @@ -125,7 +146,7 @@ bool ldb_aminsert(Relation index,
ldb_dlog("Insert: at start num vectors is %d", hdr->num_vectors);

usearch_reserve(uidx, hdr->num_vectors + 1, &error);
uint32 level = usearch_newnode_level(uidx, &error);
uint32 level = hnsw_generate_new_level(meta.connectivity);
if(error != NULL) {
elog(ERROR, "usearch newnode error: %s", error);
}
Expand Down
2 changes: 1 addition & 1 deletion third_party/usearch

0 comments on commit f329ce5

Please sign in to comment.