Skip to content

Implement quad search#355

Open
Kerollmops wants to merge 1 commit into
mainfrom
simd-quad-search
Open

Implement quad search#355
Kerollmops wants to merge 1 commit into
mainfrom
simd-quad-search

Conversation

@Kerollmops
Copy link
Copy Markdown
Member

This PR implements the SIMD-based Quad search described by Daniel Lemire in this blog post, and fixes #234.

}

#[inline]
pub fn quad_contains(slice: &[u16], val: u16) -> bool {
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.

I am wondering whether I should introduce this function, since it is used only once in the ArrayStore::contains method, or simply use the quad_search(...).is_ok() function that returns the position where the number is found or should be.

Comment on lines +648 to +649
let v0 = u16x8::from_slice(&chunks[lo][..GAP / 2]);
let v1 = u16x8::from_slice(&chunks[lo][GAP / 2..]);
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.

This from_slice method panics if the slice is too small; we need to check the generated assembler to see if it can avoid this redundant check.

Comment on lines +651 to +657
return match (v0.simd_ge(ndl).first_set(), v1.simd_ge(ndl).first_set()) {
(Some(i), _) if v0[i] == val => Ok(base_index + i),
(Some(i), _) => Err(base_index + i),
(_, Some(i)) if v1[i] == val => Ok(base_index + GAP / 2 + i),
(_, Some(i)) => Err(base_index + GAP / 2 + i),
(None, None) => Err(slice.len()),
};
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.

I am wondering if this is the best approach to take to compute the position where the needle is/should be located, or if I can do something else, less expensive than two simd_ge plus two first_set?

@Kerollmops Kerollmops marked this pull request as ready for review May 12, 2026 10:37
@Kerollmops
Copy link
Copy Markdown
Member Author

Hey @Dr-Emann 👋

I know that you like this kind of SIMD-based code, so if you want and have time to review this, I would be glad you do 🙏 If you don't have time, no worries 👍

Have a nice day 🌵

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Optimization: investigate faster search in array

1 participant