Commit f629c96c authored by Sergei Golubchik's avatar Sergei Golubchik

mhnsw: don't prefix blob ref array with its length

parent bb60e5b1
...@@ -28,11 +28,6 @@ ...@@ -28,11 +28,6 @@
#include "sql_queue.h" #include "sql_queue.h"
#include <scope.h> #include <scope.h>
#define HNSW_MAX_M 10000 // practically the number of neighbors should be ~100
#define HNSW_MAX_M_WIDTH 2
#define HNSW_MAX_M_store int2store
#define HNSW_MAX_M_read uint2korr
const LEX_CSTRING mhnsw_hlindex_table={STRING_WITH_LEN("\ const LEX_CSTRING mhnsw_hlindex_table={STRING_WITH_LEN("\
CREATE TABLE i ( \ CREATE TABLE i ( \
layer int not null, \ layer int not null, \
...@@ -158,18 +153,11 @@ int FVectorNode::instantiate_neighbors(size_t layer) ...@@ -158,18 +153,11 @@ int FVectorNode::instantiate_neighbors(size_t layer)
return err; return err;
String strbuf, *str= graph->field[2]->val_str(&strbuf); String strbuf, *str= graph->field[2]->val_str(&strbuf);
const char *neigh_arr_bytes= str->ptr(); if (str->length() % ref_len)
uint number_of_neighbors= HNSW_MAX_M_read(neigh_arr_bytes);
if (number_of_neighbors * ref_len + HNSW_MAX_M_WIDTH != str->length())
return HA_ERR_CRASHED; // should not happen, corrupted HNSW index return HA_ERR_CRASHED; // should not happen, corrupted HNSW index
const char *pos= neigh_arr_bytes + HNSW_MAX_M_WIDTH; for (const char *pos= str->ptr(); pos < str->end(); pos+= ref_len)
for (uint i= 0; i < number_of_neighbors; i++) neighbors[layer].push_back(ctx->get_node(pos), &ctx->root);
{
FVectorNode *neigh= ctx->get_node(pos);
neighbors[layer].push_back(neigh, &ctx->root);
pos+= ref_len;
}
} }
neighbors_read[layer]= 1; neighbors_read[layer]= 1;
} }
...@@ -350,16 +338,13 @@ static int write_neighbors(MHNSW_Context *ctx, size_t layer, ...@@ -350,16 +338,13 @@ static int write_neighbors(MHNSW_Context *ctx, size_t layer,
int err; int err;
TABLE *graph= ctx->table->hlindex; TABLE *graph= ctx->table->hlindex;
const List<FVectorNode> &new_neighbors= source_node.get_neighbors(layer); const List<FVectorNode> &new_neighbors= source_node.get_neighbors(layer);
DBUG_ASSERT(new_neighbors.elements <= HNSW_MAX_M);
size_t total_size= HNSW_MAX_M_WIDTH + new_neighbors.elements * source_node.get_ref_len(); size_t total_size= new_neighbors.elements * source_node.get_ref_len();
// Allocate memory for the struct and the flexible array member // Allocate memory for the struct and the flexible array member
char *neighbor_array_bytes= static_cast<char *>(my_safe_alloca(total_size)); char *neighbor_array_bytes= static_cast<char *>(my_safe_alloca(total_size));
// XXX why bother storing it? char *pos= neighbor_array_bytes;
HNSW_MAX_M_store(neighbor_array_bytes, new_neighbors.elements);
char *pos= neighbor_array_bytes + HNSW_MAX_M_WIDTH;
for (const auto &node: new_neighbors) for (const auto &node: new_neighbors)
{ {
DBUG_ASSERT(node.get_ref_len() == source_node.get_ref_len()); DBUG_ASSERT(node.get_ref_len() == source_node.get_ref_len());
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment