Skip to content

Optimize merged_points computation in SegmentBuilder::update() - #5820

Merged
xzfc merged 2 commits into
devfrom
k-merged-points
Jan 22, 2025
Merged

xzfc merged 2 commits into
devfrom
k-merged-points

Conversation

@xzfc

@xzfc xzfc commented Jan 17, 2025

Copy link
Copy Markdown
Member

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 HashMap for 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 by itertools) we can iterate and deduplicate points across all segments in a single pass.

Structure packing

The size of the ExtendedPointId enum is 24 which is excessive given that Uuid is 16 bytes long. The reason is the 8-byte alignment of u64.

By replacing u64 with [u8; 8] (which doesn't require alignment), we can reduce the size of ExtendedPointId to 17 bytes. I've used zerocopy::…::U64 as a wrapper around [u8; 8].

The next tiny step is to switch segment_index: usize to U24 which is 3 bytes long and combines well with 17-byte ExtendedPointId. I assume that this function won't be used to merge 16M segments. 😅

Both of these tricks reduced the size of struct PositionedPointMetadata from 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).

memory_usage

@timvisee timvisee left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean it may not be platform native ordering in these?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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],

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we possibly want to randomize each element separately?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the purpose of extensively testing for_each_unique_point, yes.

@xzfc
xzfc merged commit 651e307 into dev Jan 22, 2025
@xzfc
xzfc deleted the k-merged-points branch January 22, 2025 14:21
@xzfc xzfc mentioned this pull request Jan 22, 2025
timvisee pushed a commit that referenced this pull request Jan 23, 2025
…5820)

* Optimize `merged_points` computation in `SegmentBuilder::update()`

* Fixes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants