Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slotted array search and insert optimizations #370

Merged
merged 8 commits into from
Jul 4, 2024

Conversation

Scooletz
Copy link
Contributor

@Scooletz Scooletz commented Jul 4, 2024

This PR tries to address the crux of the db performance issues, SlottedArray component that is visible in both, inserts and gets from the database. It does it in the following way:

  1. TryGetImpl changed from bool returning to index returning results in shorter code (~40 bytes)
  2. IsDeleted checkis not needed and can be removed saving one bool check and reducing the size further
  3. MarkAsDeleted, when marking the entry as deleted changes its hash (to simple ~hash) so that it will no longer hit the search. See the Set_And_Delete benchmark below. This is 💥 for both sets and gets as they use this function heavily. The gains are tremendous.
  4. Benchmarks showed that aligning the search to a full vector size is too heavy and does not bring gains to the search. For example, a test was done to align 7 items to get them to 8, or 15 to 16. No visible gains.

Benchmarks

Selected benchmarks used to check the speed.

Before

Method Mean Error StdDev Code Size
Set_And_Delete 48.63 us 0.504 us 0.447 us 5,888 B

After

Method Mean Error StdDev Code Size
Set_And_Delete 6.005 us 0.0482 us 0.0451 us 5,879 B

@@ -62,7 +62,8 @@ public bool TrySet(in NibblePath key, ReadOnlySpan<byte> data)

private bool TrySetImpl(ushort hash, byte preamble, in NibblePath trimmed, ReadOnlySpan<byte> data)
{
if (TryGetImpl(trimmed, hash, preamble, out var existingData, out var index))
var index = TryGetImpl(trimmed, hash, preamble, out var existingData);
Copy link
Member

Choose a reason for hiding this comment

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

maybe better that it would return a bool and index would be out param?

Copy link
Contributor Author

@Scooletz Scooletz Jul 4, 2024

Choose a reason for hiding this comment

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

This was done before. As #369 showed it cuts off 40 bytes from 800 byte long method without impact on the call sites that need to compare it anyway. Follows IndexOf and others low lvl method in BCL.

@Scooletz Scooletz added 🐌 performance Perofrmance related issue 💥Breaking The change introduces a storage breaking change. labels Jul 4, 2024
Copy link

github-actions bot commented Jul 4, 2024

Code Coverage

Package Line Rate Branch Rate Health
Paprika 85% 80%
Summary 85% (4197 / 4926) 80% (1346 / 1680)

Minimum allowed line rate is 75%

@Scooletz Scooletz marked this pull request as ready for review July 4, 2024 15:44
@Scooletz Scooletz merged commit b5c9e31 into main Jul 4, 2024
2 checks passed
@Scooletz Scooletz deleted the slotted-array-get-optimizations branch July 4, 2024 15:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
💥Breaking The change introduces a storage breaking change. 🐌 performance Perofrmance related issue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants