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

Rust port #2

Closed
thevilledev opened this issue Nov 19, 2024 · 6 comments
Closed

Rust port #2

thevilledev opened this issue Nov 19, 2024 · 6 comments

Comments

@thevilledev
Copy link

Kudos - super interesting project! Thank you for the great work.

I've created a Rust port of it at thevilledev/ChibiHash-rs. I've tagged the first release as v0.1.0 and published it as a chibihash crate.

I took your example key/seed combinations from your comment at #1 (comment) and created a test for it, to ensure we're on the right track.

Happy to keep maintaining the project and support anyone using this with Rust.

@N-R-K
Copy link
Owner

N-R-K commented Nov 20, 2024

Thanks. I've added your repo the list of ports: #4

@N-R-K N-R-K closed this as completed Nov 20, 2024
@N-R-K
Copy link
Owner

N-R-K commented Nov 20, 2024

Upon further look, I see there's an error depending on the size: https://github.com/thevilledev/ChibiHash-rs/blob/624b44991ba2ee8639b7e3de6e6acc4fbfe6fac3/src/lib.rs#L41-L46

Hash functions should never error. The trend in certain languages (especially those that have specific error types) to create unnecessary 'errors' is, IMO, a bad one. All it does is make the api cumbersome to use.

As for streaming api, you can do it with 0 allocations, keep a 32 byte buffer and append to it. See the newly pushed stream branch for reference. With fixed buffer, and allocation gone, the streaming api also cannot fail.

(In fact, I mixed the length into the state late in the hashing process instead of at the start specifically so that 0 allocation streaming apis are possible).

@thevilledev
Copy link
Author

Thanks for the feedback! I was contemplating on removing the error type after implementing support for the Hasher trait. It doesn't do errors either and the output is always u64. That's the standard. I'll likely remove the error type in the next release altogether. 👍

I'll have a look at the streaming API!

@thevilledev
Copy link
Author

Just a short follow-up. Streaming support added in v0.2.0. I'll keep an eye on the repo for any improvements/changes and incorporate them into my project as well. I'm looking into some Rust-specific optimisations too.

@N-R-K
Copy link
Owner

N-R-K commented Nov 21, 2024

Just a short follow-up. Streaming support added in v0.2.0. I'll keep an eye on the repo for any improvements/changes and incorporate them into my project as well.

Good to know. I've made some local changes to improve short string performance which will be part of v2 (I will make a new branch for it when things are more polished).

I'm looking into some Rust-specific optimisations too.

Do you have any numbers on how the rust port compares to the C version? I'd assume they're on the same league since rust is backed by llvm (which is more or less on par with gcc).

@thevilledev
Copy link
Author

Do you have any numbers on how the rust port compares to the C version? I'd assume they're on the same league since rust is backed by llvm (which is more or less on par with gcc).

I went ahead and implemented a small test suite, which calls the original C implementation of the hash function through Rust's FFI (Foreign Function Interface). I ran both implementations through the benchmarks on various input sizes. I included a few screenshots and thoughts in thevilledev/ChibiHash-rs#9 (comment).

Despite the results of Rust being faster on smaller input sizes, I'd consider both more or less the same. If I'd have to guess there is some overhead of FFI which makes those smaller input size runs seem slower on the C implementation.

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

2 participants