Skip to content

Improve immutable map index performance: sort point IDs, binary search and deleted flags - #5072

Merged
timvisee merged 2 commits into
devfrom
immutable-map-index-deleted-flags
Sep 16, 2024
Merged

timvisee merged 2 commits into
devfrom
immutable-map-index-deleted-flags

Conversation

@timvisee

@timvisee timvisee commented Sep 12, 2024 •

Copy link
Copy Markdown
Member

Our immutable map index appears to be very slow if we have a huge number of points but very few distinct values.

The problem is in a linear search when we're trying to delete an item. This may become very hot when deleting a lot of point duplicates on start.

I've improved on this by sorting the list of point IDs so we can do a binary search rather than a scan. This PR also implements deleted flags to not remove point IDs from the middle of this list anymore.

My test collection has a segment of about 750k points with 5 keyword indices having just 2 distinct values. This segment is duplicated to get 750k duplicates.

This is how long it takes when loading

  • Before PR: 226 seconds
  • After PR: 45 seconds (-80%)

I've also implemented an alternative. It doesn't have deleted flags but removes point IDs from the list. It deletes and shifts remaining items. It is slower at 86 seconds (#5070).

All Submissions:

  • Contributions should target the dev branch. Did you create your branch from dev?
  • Have you followed the guidelines in our Contributing document?
  • Have you checked to ensure there aren't other open Pull Requests for the same update/change?

New Feature Submissions:

  1. Does your submission pass tests?
  2. Have you formatted your code locally using cargo +nightly fmt --all command prior to submission?
  3. Have you checked your code using cargo clippy --all --all-features command?

@xzfc xzfc 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.

LGTM.

I think it's also possible to implement another approach:

enum PointsSet {
   // Up to three points could be stored directly within this type
   Inline([u32; 3]),
   // 4..24 points could be stored in value_to_points_container.
   // Do a linear search during deletion, but for up to 24 elements it would be neglectible.
   Container(Range<u32>),
   // Greater amount of points stored in a heap-allocated structure.
   // The memory overhead should only be a concern for small amount of points which is covered by previous variant.
   // Enables `O(log(n))` deletion times.
   BTreeSet(BTreeSet<u32>),
}

@timvisee timvisee mentioned this pull request Sep 13, 2024
6 tasks done
@timvisee

timvisee commented Sep 13, 2024 •

Copy link
Copy Markdown
Member Author

LGTM.

I think it's also possible to implement another approach:

enum PointsSet {
   // Up to three points could be stored directly in within this type
   Inline([u32; 3]),
   // 3..32 points could be stored in value_to_points_container.
   // Do a linear search during deletion, but for up to 16 elements it would be neglectible.
   Container(Range<u32>),
   // Greater amount of points stored in a heap-allocated structure.
   // The memory overhead should only be a concern for small amount of points which is covered by previous variant.
   // Enables `O(log(n))` deletion times.
   BTreeSet(BTreeSet<u32>),
}

Definitely something to investigate.

Out of curiosity, why did you choose 3? Is it because it's the maximum size without growing the enum in terms of memory?

I'd vote to explore this idea in a separate PR. Here I tried to keep everything intact on purpose.

@xzfc

xzfc commented Sep 13, 2024

Copy link
Copy Markdown
Member

Out of curiosity, why did you choose 3? Is it because it's the maximum size without growing the enum in terms of memory?

Yep, sizeof::<BTreeSet<u32>>() is 3.

@generall

Copy link
Copy Markdown
Member

LGTM.

I think it's also possible to implement another approach:

enum PointsSet {
   // Up to three points could be stored directly within this type
   Inline([u32; 3]),
   // 4..24 points could be stored in value_to_points_container.
   // Do a linear search during deletion, but for up to 24 elements it would be neglectible.
   Container(Range<u32>),
   // Greater amount of points stored in a heap-allocated structure.
   // The memory overhead should only be a concern for small amount of points which is covered by previous variant.
   // Enables `O(log(n))` deletion times.
   BTreeSet(BTreeSet<u32>),
}

This structure takes 32 bytes in my test

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=4716c988b3f3b34c0aad9c08e7bf23f2

@generall
generall force-pushed the immutable-map-index-deleted-flags branch from 93dad46 to e6fdf80 Compare September 16, 2024 12:49
@timvisee
timvisee merged commit 4d7d908 into dev Sep 16, 2024
@timvisee
timvisee deleted the immutable-map-index-deleted-flags branch September 16, 2024 13:29
timvisee added a commit that referenced this pull request Sep 17, 2024
…h and deleted flags (#5072)

* Improve immutable map index, sort values, binary search, deleted flags

* Add struct for value to points range and count
@timvisee timvisee mentioned this pull request Sep 17, 2024
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