@@ -482,6 +482,15 @@ HnswLoadElementFromTuple(HnswElement element, HnswElementTuple etup, bool loadHe
482
482
}
483
483
}
484
484
485
+ /*
486
+ * Calculate the distance between values
487
+ */
488
+ static inline float
489
+ HnswGetDistance (Datum a , Datum b , FmgrInfo * procinfo , Oid collation )
490
+ {
491
+ return DatumGetFloat8 (FunctionCall2Coll (procinfo , collation , a , b ));
492
+ }
493
+
485
494
/*
486
495
* Load an element and optionally get its distance from q
487
496
*/
@@ -507,7 +516,7 @@ HnswLoadElementImpl(BlockNumber blkno, OffsetNumber offno, double *distance, Dat
507
516
if (DatumGetPointer (* q ) == NULL )
508
517
* distance = 0 ;
509
518
else
510
- * distance = DatumGetFloat8 ( FunctionCall2Coll ( procinfo , collation , * q , PointerGetDatum (& etup -> data )) );
519
+ * distance = HnswGetDistance ( * q , PointerGetDatum (& etup -> data ), procinfo , collation );
511
520
}
512
521
513
522
/* Load element */
@@ -539,7 +548,7 @@ GetElementDistance(char *base, HnswElement element, Datum q, FmgrInfo *procinfo,
539
548
{
540
549
Datum value = HnswGetValue (base , element );
541
550
542
- return DatumGetFloat8 ( FunctionCall2Coll ( procinfo , collation , q , value ) );
551
+ return HnswGetDistance ( q , value , procinfo , collation );
543
552
}
544
553
545
554
/*
@@ -921,32 +930,22 @@ CompareCandidateDistancesOffset(const ListCell *a, const ListCell *b)
921
930
return 0 ;
922
931
}
923
932
924
- /*
925
- * Calculate the distance between elements
926
- */
927
- static float
928
- HnswGetDistance (char * base , HnswElement a , HnswElement b , FmgrInfo * procinfo , Oid collation )
929
- {
930
- Datum aValue = HnswGetValue (base , a );
931
- Datum bValue = HnswGetValue (base , b );
932
-
933
- return DatumGetFloat8 (FunctionCall2Coll (procinfo , collation , aValue , bValue ));
934
- }
935
-
936
933
/*
937
934
* Check if an element is closer to q than any element from R
938
935
*/
939
936
static bool
940
937
CheckElementCloser (char * base , HnswCandidate * e , List * r , FmgrInfo * procinfo , Oid collation )
941
938
{
942
939
HnswElement eElement = HnswPtrAccess (base , e -> element );
940
+ Datum eValue = HnswGetValue (base , eElement );
943
941
ListCell * lc2 ;
944
942
945
943
foreach (lc2 , r )
946
944
{
947
945
HnswCandidate * ri = lfirst (lc2 );
948
946
HnswElement riElement = HnswPtrAccess (base , ri -> element );
949
- float distance = HnswGetDistance (base , eElement , riElement , procinfo , collation );
947
+ Datum riValue = HnswGetValue (base , riElement );
948
+ float distance = HnswGetDistance (eValue , riValue , procinfo , collation );
950
949
951
950
if (distance <= e -> distance )
952
951
return false;
0 commit comments