LUCENE-10453: Speed up euclidean distances. (#725)

This commit is contained in:
Adrien Grand 2022-03-05 18:31:56 +01:00 committed by GitHub
parent 2700c6b525
commit 9d732380ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 1 deletions

View File

@ -252,6 +252,9 @@ Optimizations
* LUCENE-10450: IndexSortSortedNumericDocValuesRangeQuery could be rewrite to MatchAllDocsQuery. (Lu Xugang)
* LUCENE-10453: Indexing and search speedup with KNN vectors when using
euclidean distance. (Adrien Grand)
Changes in runtime behavior
---------------------

View File

@ -133,13 +133,36 @@ public final class VectorUtil {
}
float squareSum = 0.0f;
int dim = v1.length;
for (int i = 0; i < dim; i++) {
int i;
for (i = 0; i + 8 <= dim; i += 8) {
squareSum += squareDistanceUnrolled8(v1, v2, i);
}
for (; i < dim; i++) {
float diff = v1[i] - v2[i];
squareSum += diff * diff;
}
return squareSum;
}
private static float squareDistanceUnrolled8(float[] v1, float[] v2, int index) {
float diff0 = v1[index + 0] - v2[index + 0];
float diff1 = v1[index + 1] - v2[index + 1];
float diff2 = v1[index + 2] - v2[index + 2];
float diff3 = v1[index + 3] - v2[index + 3];
float diff4 = v1[index + 4] - v2[index + 4];
float diff5 = v1[index + 5] - v2[index + 5];
float diff6 = v1[index + 6] - v2[index + 6];
float diff7 = v1[index + 7] - v2[index + 7];
return diff0 * diff0
+ diff1 * diff1
+ diff2 * diff2
+ diff3 * diff3
+ diff4 * diff4
+ diff5 * diff5
+ diff6 * diff6
+ diff7 * diff7;
}
/**
* Modifies the argument to be unit length, dividing by its l2-norm. IllegalArgumentException is
* thrown for zero vectors.