@@ -126,37 +126,41 @@ func (q *RaBitQuantizer) QuantizeInSet(
126
126
127
127
// NewSet implements the Quantizer interface
128
128
func (q * RaBitQuantizer ) NewSet (capacity int , centroid vector.T ) QuantizedVectorSet {
129
- codeWidth := RaBitQCodeSetWidth ( q . GetDims ())
130
- dataBuffer := make ([] uint64 , 0 , capacity * codeWidth )
129
+ var vs * RaBitQuantizedVectorSet
130
+
131
131
if capacity <= 1 {
132
132
// Special case capacity of zero or one by using in-line storage.
133
- var quantized raBitQuantizedVector
134
- quantized .Centroid = centroid
135
- quantized .Codes = MakeRaBitQCodeSetFromRawData (dataBuffer , codeWidth )
133
+ quantized := & raBitQuantizedVector {}
136
134
quantized .CodeCounts = quantized .codeCountStorage [:0 ]
137
135
quantized .CentroidDistances = quantized .centroidDistanceStorage [:0 ]
138
136
quantized .QuantizedDotProducts = quantized .quantizedDotProductStorage [:0 ]
139
137
140
- // L2Squared doesn't use these, so don't make extra calculations .
138
+ // L2Squared doesn't use this .
141
139
if q .distanceMetric != vecpb .L2SquaredDistance {
142
140
quantized .CentroidDotProducts = quantized .centroidDotProductStorage [:0 ]
143
- quantized .CentroidNorm = num32 .Norm (centroid )
144
141
}
145
- return & quantized .RaBitQuantizedVectorSet
142
+ vs = & quantized .RaBitQuantizedVectorSet
143
+ } else {
144
+ vs = & RaBitQuantizedVectorSet {
145
+ CodeCounts : make ([]uint32 , 0 , capacity ),
146
+ CentroidDistances : make ([]float32 , 0 , capacity ),
147
+ QuantizedDotProducts : make ([]float32 , 0 , capacity ),
148
+ }
149
+ // L2Squared doesn't use these, so don't make extra allocation or calculation.
150
+ if q .distanceMetric != vecpb .L2SquaredDistance {
151
+ vs .CentroidDotProducts = make ([]float32 , 0 , capacity )
152
+ }
146
153
}
147
154
148
- vs := & RaBitQuantizedVectorSet {
149
- Centroid : centroid ,
150
- Codes : MakeRaBitQCodeSetFromRawData (dataBuffer , codeWidth ),
151
- CodeCounts : make ([]uint32 , 0 , capacity ),
152
- CentroidDistances : make ([]float32 , 0 , capacity ),
153
- QuantizedDotProducts : make ([]float32 , 0 , capacity ),
154
- }
155
- // L2Squared doesn't use these, so don't make extra allocation or calculation.
155
+ vs .Metric = q .distanceMetric
156
+ vs .Centroid = centroid
157
+ codeWidth := RaBitQCodeSetWidth (q .GetDims ())
158
+ dataBuffer := make ([]uint64 , 0 , capacity * codeWidth )
159
+ vs .Codes = MakeRaBitQCodeSetFromRawData (dataBuffer , codeWidth )
156
160
if q .distanceMetric != vecpb .L2SquaredDistance {
157
- vs .CentroidDotProducts = make ([]float32 , 0 , capacity )
158
161
vs .CentroidNorm = num32 .Norm (centroid )
159
162
}
163
+
160
164
return vs
161
165
}
162
166
@@ -412,7 +416,7 @@ func (q *RaBitQuantizer) quantizeHelper(
412
416
// Extend any existing slices in the vector set.
413
417
count := vectors .Count
414
418
oldCount := qs .GetCount ()
415
- qs .AddUndefined (count , q . distanceMetric )
419
+ qs .AddUndefined (count )
416
420
417
421
// L2Squared doesn't use this, so don't store it.
418
422
if q .distanceMetric != vecpb .L2SquaredDistance {
0 commit comments