RustCrypto / hashes

Collection of cryptographic hash functions written in pure Rust
1.81k stars 245 forks source link

The speed of calculating in rust seems like not as fast as in Node. #453

Closed XLearner closed 1 year ago

XLearner commented 1 year ago

Hi Maintainer, I wanted to use the sha2::Sha256 to do "Proof of Work" with rust and transformed Rust code to "WebAssembly" for the web page finally. So I did some tests separately in Node and rust. However, the result is unsatisfying. Node is much faster than Rust. I am curious about what reason could be leading to the disparity in the speed?

Here is my testing code: demo-node.txt demo-rs.txt

tarcieri commented 1 year ago

It sounds like you're comparing Node's crypto module, which IIUC is a wrapper for the optimized ASM implementations in OpenSSL, against a pure Rust implementation of SHA-2 compiled to WebAssembly.

This isn't really a fair comparison, because you're comparing hand-optimized architecture-specific assembly to WASM, which is interpreted/JITed.

Some fair comparisons would be:

burdges commented 1 year ago

Is it worth having benchmarks with and without -C codegen-units=1? It'd avoid regressions from cross crate stuff like https://docs.rs/blake2/latest/blake2/type.Blake2b.html