Skip to content

Commit 5cd9ca1

Browse files
committed
Added comments for clarity
1 parent ff89368 commit 5cd9ca1

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

src/MongoDB.Bson/Serialization/BinaryVectorReader.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public static (TItem[] Items, byte Padding, BinaryVectorDataType VectorDataType)
5151
throw new NotSupportedException($"Expected float for Float32 vector type, but found {typeof(TItem)}.");
5252
}
5353

54-
int count = vectorDataBytes.Length / 4;
54+
int count = vectorDataBytes.Length / 4; // 4 bytes per float
5555
float[] floatArray = new float[count];
5656

5757
for (int i = 0; i < count; i++)

src/MongoDB.Bson/Serialization/BinaryVectorWriter.cs

+7-6
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,16 @@ public static byte[] WriteToBytes<TItem>(ReadOnlySpan<TItem> vectorData, BinaryV
4848
{
4949
case BinaryVectorDataType.Float32:
5050
int length = vectorData.Length * sizeof(float);
51-
resultBytes = new byte[2 + length];
52-
resultBytes[0] = (byte)binaryVectorDataType;
53-
resultBytes[1] = padding;
54-
var floatSpan = MemoryMarshal.Cast<TItem, float>(vectorData);
55-
Span<byte> floatOutput = resultBytes.AsSpan(2);
51+
resultBytes = new byte[2 + length]; // Allocate output buffer:
52+
resultBytes[0] = (byte)binaryVectorDataType; // - [0]: vector type
53+
resultBytes[1] = padding; // - [1]: padding
54+
var floatSpan = MemoryMarshal.Cast<TItem, float>(vectorData);
55+
Span<byte> floatOutput = resultBytes.AsSpan(2); // - [2...]: actual float data , skipping header
5656
foreach (var value in floatSpan)
5757
{
58+
// Each float is 4 bytes - write in Big Endian format
5859
BinaryPrimitives.WriteSingleBigEndian(floatOutput, value);
59-
floatOutput = floatOutput.Slice(4);
60+
floatOutput = floatOutput.Slice(4); // advance to next 4-byte block
6061
}
6162
return resultBytes;
6263

0 commit comments

Comments
 (0)