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

Replace modulo ops with bitwise-and equivalent #227

Merged
merged 2 commits into from
Feb 9, 2024
Merged

Conversation

cynecx
Copy link
Contributor

@cynecx cynecx commented Feb 4, 2024

Doesn't really affect any of the in-tree benchmarks due to the statically known allocation sizes and heavy inlining. LLVM is smart enough to optimize the expensive modulo operation away. However in more dynamic cases this produces less and more optimal code.

Copy link
Owner

@fitzgen fitzgen left a comment

Choose a reason for hiding this comment

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

We already have a round_down_to helper function that can be called here, and which debug asserts its correctness predicates. Can you update the PR to make use of that helper? Thanks!

@cynecx
Copy link
Contributor Author

cynecx commented Feb 8, 2024

We already have a round_down_to helper function that can be called here...

I don't think we can use the existing helpers because they don't preserve pointer provenance due to operating on usize. Do you want me to add this equivalent helper instead?

#[inline]
pub(crate) fn round_ptr_down_to(ptr: *const u8, divisor: usize) -> *const u8 {
    debug_assert!(divisor > 0);
    debug_assert!(divisor.is_power_of_two());
    ptr.wrapping_sub(ptr as usize & (divisor - 1))
}

Or keep this as is?

@fitzgen
Copy link
Owner

fitzgen commented Feb 8, 2024

Let's add the equivalent helper, thanks!

@cynecx
Copy link
Contributor Author

cynecx commented Feb 9, 2024

🙏 I've updated the branch :)

Copy link
Owner

@fitzgen fitzgen left a comment

Choose a reason for hiding this comment

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

Thanks!

src/lib.rs Show resolved Hide resolved
@fitzgen fitzgen merged commit f8278d6 into fitzgen:main Feb 9, 2024
8 checks passed
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.

None yet

2 participants