From 91fe2e62e7fc2095e8fde66197f0e03cd7ca214a Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Tue, 16 Jun 2026 09:53:47 -0700 Subject: [PATCH] Added benchmarking for HNSW vacuuming [skip ci] --- README.md | 2 +- src/hnsw.h | 19 +++++++++++++++++++ src/hnswvacuum.c | 6 +++--- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 30a4afb..b0cad60 100644 --- a/README.md +++ b/README.md @@ -1329,7 +1329,7 @@ make clean && PG_CFLAGS="-DUSE_ASSERT_CHECKING" make && make install To enable benchmarking: ```sh -make clean && PG_CFLAGS="-DIVFFLAT_BENCH" make && make install +make clean && PG_CFLAGS="-DHNSW_BENCH -DIVFFLAT_BENCH" make && make install ``` To show memory usage: diff --git a/src/hnsw.h b/src/hnsw.h index f4492b5..08293be 100644 --- a/src/hnsw.h +++ b/src/hnsw.h @@ -18,6 +18,10 @@ #include "utils/sampling.h" #include "vector.h" +#ifdef HNSW_BENCH +#include "portability/instr_time.h" +#endif + #if PG_VERSION_NUM >= 190000 typedef Pointer Item; #endif @@ -78,6 +82,21 @@ typedef Pointer Item; #define HnswPageGetOpaque(page) ((HnswPageOpaque) PageGetSpecialPointer(page)) #define HnswPageGetMeta(page) ((HnswMetaPageData *) PageGetContents(page)) +#ifdef HNSW_BENCH +#define HnswBench(name, code) \ + do { \ + instr_time start; \ + instr_time duration; \ + INSTR_TIME_SET_CURRENT(start); \ + (code); \ + INSTR_TIME_SET_CURRENT(duration); \ + INSTR_TIME_SUBTRACT(duration, start); \ + elog(INFO, "%s: %.3f ms", name, INSTR_TIME_GET_MILLISEC(duration)); \ + } while (0) +#else +#define HnswBench(name, code) (code) +#endif + #if PG_VERSION_NUM >= 150000 #define RandomDouble() pg_prng_double(&pg_global_prng_state) #define SeedRandom(seed) pg_prng_seed(&pg_global_prng_state, seed) diff --git a/src/hnswvacuum.c b/src/hnswvacuum.c index fe0e980..7ccd313 100644 --- a/src/hnswvacuum.c +++ b/src/hnswvacuum.c @@ -669,13 +669,13 @@ hnswbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats, InitVacuumState(&vacuumstate, info, stats, callback, callback_state); /* Pass 1: Remove heap TIDs */ - RemoveHeapTids(&vacuumstate); + HnswBench("RemoveHeapTids", RemoveHeapTids(&vacuumstate)); /* Pass 2: Repair graph */ - RepairGraph(&vacuumstate); + HnswBench("RepairGraph", RepairGraph(&vacuumstate)); /* Pass 3: Mark as deleted */ - MarkDeleted(&vacuumstate); + HnswBench("MarkDeleted", MarkDeleted(&vacuumstate)); FreeVacuumState(&vacuumstate);