Repository navigation
Optimize merged_points computation in SegmentBuilder::update() - #5820
Conversation
83c0be7 to
f924aa9
Compare
timvisee
left a comment
There was a problem hiding this comment.
Some comments I drafted earlier, will do a proper review later.
|
|
||
| #[inline] | ||
| pub fn new_wrapped(value: u32) -> Self { | ||
| let arr = value.to_le_bytes(); |
There was a problem hiding this comment.
Does this mean it may not be platform native ordering in these?
There was a problem hiding this comment.
Yes. And it seems, on ppc64, LLVM generates not very well optimized code for this case, unfortunately. godbolt.org
It could be improved by adding a special case using #[cfg(target_endian = "…")]; but unless we test/benchmark qdrant on ppc64, it will remain untestable (== dead) code, which I prefer to avoid.
Tho, at least, current impl should be correct now on any endianess; and I've added an unit-test just in case.
| #[cfg(test)] | ||
| pub fn random(rand: &mut StdRng, size: u32, preserved_size: u32, bits_in_id: u8) -> Self { | ||
| Self { | ||
| internal_to_version: vec![rand.gen(); size as usize], |
There was a problem hiding this comment.
Do we possibly want to randomize each element separately?
There was a problem hiding this comment.
For the purpose of extensively testing for_each_unique_point, yes.
…5820) * Optimize `merged_points` computation in `SegmentBuilder::update()` * Fixes
This PR optimizes RAM usage of
SegmentBuilder::update(). Two primary ideas: k-way merge and structure packing.K-way merge
We have a list of segments and we want to deduplicate point IDs within them. Previously we were using a huge
HashMapfor that. This required a lot of temporary memory.But! Our
IdTrackers let us iterate over IDs in a sorted order. By applying a k-way merge algorithm (which is kindly provided byitertools) we can iterate and deduplicate points across all segments in a single pass.Structure packing
The size of the
ExtendedPointIdenum is 24 which is excessive given thatUuidis 16 bytes long. The reason is the 8-byte alignment ofu64.By replacing
u64with[u8; 8](which doesn't require alignment), we can reduce the size ofExtendedPointIdto 17 bytes. I've usedzerocopy::…::U64as a wrapper around[u8; 8].The next tiny step is to switch
segment_index: usizetoU24which is 3 bytes long and combines well with 17-byteExtendedPointId. I assume that this function won't be used to merge 16M segments. 😅Both of these tricks reduced the size of struct
PositionedPointMetadatafrom 56 bytes to 40 bytes.Benchmark
On my test of merging 4 segments with a total of 3744768 points, these changes reduced the additional RAM usage from 970MB to 590MB, making it use less memory than the HNSW graph building (a step at 1200M on the right side of the plot).