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

Why force power of 2 for capacity? #40

Open
yonderblue opened this issue Mar 11, 2021 · 3 comments
Open

Why force power of 2 for capacity? #40

yonderblue opened this issue Mar 11, 2021 · 3 comments

Comments

@yonderblue
Copy link

Perhaps I am not seeing it in the paper, but why force the capacity up to the next power of 2? When rebuilding as in the scalable filter, this decreases the potential load factor.

@yonderblue yonderblue changed the title Why power of 2? Why force power of 2 for capacity? Mar 11, 2021
@jkomyno
Copy link

jkomyno commented Mar 15, 2021

I didn't read the code accurately, but this is probably to force the compiler to avoid using the slow DIV assembly operation when using modulo operations.
If the capacity is a power of 2, you can compute modulos with bitwise ANDs.

Maybe @seiflotfy can expand on this.

@yonderblue
Copy link
Author

Seems plausible, but I would think the size of the filter is often more important.

@wtysos11
Copy link

wtysos11 commented Jul 2, 2021

If the capacity is not the power of 2, then xor won't be involution. And as the candidate position is calcualted using h2(x) = h1(x) xor hash(fingerprint), the result may change and cause unexpected behaviour.
I answer a similar question in stackoverflow. There is a simple example. Assume fingerprint=128, h1(x) = 32, hash(fingerprint) = 73. The candidate position can be calculated using h2(x) = (h1(x) xor hash(fingerprint)) % capacity.
If capacity = 128, h2(x) = 32 xor 73 = 105, and 105 xor 73 = 32.
However if capacity = 100, h2(x) = 105%100 = 5, 5 xor 73 = 76. In this situation, once element swaps away from origin position, it can never go back. For solution you can see my answer or this answer

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

No branches or pull requests

3 participants