Skip to content

Apache Lucene open-source search software

Page-align vector data for all encodings (#16771)

goankur8a8ef4882184
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 30693cd3aad..b74458263d3 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -216,6 +216,12 @@ Improvements
Optimizations
---------------------
+
+* GITHUB#16771: Lucene99FlatVectorsWriter now aligns vector data to 4096 bytes for all encodings
+ (previously 64 for float32 and 4 for float16 and byte), so a vector whose size divides a page or is
+ a multiple of it no longer straddles a page boundary and a rescoring read touches no extra page.
+ (Ankur Goel)
+
* GITHUB#16431, GITHUB#16697: Add support for Block-Max WAND dynamic score skipping in FunctionScoreQuery
and DoubleValuesSource using DocValuesSkipper. (Rajat Jain, Zhang Chao)
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99FlatVectorsFormat.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99FlatVectorsFormat.java
index 46be88836ca..15e75b63550 100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99FlatVectorsFormat.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99FlatVectorsFormat.java
@@ -63,11 +63,13 @@ import org.apache.lucene.store.IndexOutput;
* that only in sparse case
* </ul>
*
- * <p>NOTE: Arm Neoverse machines have a performance overhead in reading data that is not aligned to
- * 64 bytes, so this format aligns the <code>.vec</code> file to that size. There may be a
- * performance penalty in searching of float vectors that do <b>not</b> have a dimension of a
- * multiple of 16 (equivalent to 64 bytes), because the alignment will not hold for all vectors in
- * the file.
+ * <p>NOTE: this format aligns the vector data of each field in the <code>.vec</code> file to 4096
+ * bytes, for every encoding. A vector whose size divides 4096 bytes or is a multiple of it (e.g.
+ * 1024-dim float32) then never straddles a page boundary, so reading one vector, as rescoring does,
+ * touches no extra page. 4096 is also a multiple of 64 bytes, the alignment that Arm Neoverse
+ * machines need to read vectors without overhead. If the vector size is not a multiple of 64 bytes,
+ * most vectors in the file are not aligned to 64 bytes, and searching them may be slower on these
+ * machines.
*
* @lucene.experimental
*/
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99FlatVectorsWriter.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99FlatVectorsWriter.java
index 58608cad433..17d417da53f 100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99FlatVectorsWriter.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99FlatVectorsWriter.java
@@ -184,20 +184,21 @@ public final class Lucene99FlatVectorsWriter extends FlatVectorsWriter {
return total;
}
- private static long alignOutput(IndexOutput output, VectorEncoding encoding) throws IOException {
- return output.alignFilePointer(
- switch (encoding) {
- case BYTE -> Float.BYTES;
- case FLOAT16 -> Float.BYTES;
- case FLOAT32 -> 64; // optimal alignment for Arm Neoverse machines.
- });
+ /**
+ * Page-aligns vector data for every encoding, so a vector whose size divides 4 KB or is a
+ * multiple of it (e.g. 1024-dim float32, 1024- or 4096-dim float16, 1024-dim byte) never
+ * straddles a page boundary and a rescoring read touches no extra page. Also a multiple of the
+ * 64-byte alignment that is optimal for Arm Neoverse machines.
+ */
+ private static long alignOutput(IndexOutput output) throws IOException {
+ return output.alignFilePointer(4096);
}
private void writeField(FlatFieldVectorsWriter<?> fieldWriter, FieldInfo fieldInfo, int maxDoc)
throws IOException {
// write vector values
VectorEncoding encoding = fieldInfo.getVectorEncoding();
- long vectorDataOffset = alignOutput(vectorData, encoding);
+ long vectorDataOffset = alignOutput(vectorData);
switch (encoding) {
case BYTE -> writeByteVectors(fieldWriter);
case FLOAT16 -> writeFloat16Vectors(fieldWriter, fieldInfo);
@@ -251,7 +252,7 @@ public final class Lucene99FlatVectorsWriter extends FlatVectorsWriter {
// write vector values
VectorEncoding encoding = fieldInfo.getVectorEncoding();
- long vectorDataOffset = alignOutput(vectorData, encoding);
+ long vectorDataOffset = alignOutput(vectorData);
switch (encoding) {
case BYTE -> writeSortedByteVectors(fieldWriter, ordMap);
case FLOAT16 -> writeSortedFloat16Vectors(fieldWriter, fieldInfo, ordMap);
@@ -302,7 +303,7 @@ public final class Lucene99FlatVectorsWriter extends FlatVectorsWriter {
// Since we know we will not be searching for additional indexing, we can just write the
// vectors directly to the new segment.
VectorEncoding encoding = fieldInfo.getVectorEncoding();
- long vectorDataOffset = alignOutput(vectorData, encoding);
+ long vectorDataOffset = alignOutput(vectorData);
// No need to use temporary file as we don't have to re-open for reading
DocsWithFieldSet docsWithField =
switch (encoding) {