BLAKE3-team / BLAKE3

the official Rust and C implementations of the BLAKE3 cryptographic hash function
Apache License 2.0
5.2k stars 351 forks source link

Is there any optimisation for hashing arrays of known size? #349

Closed fadedbee closed 1 year ago

fadedbee commented 1 year ago

I am hashing large numbers of [u8; 32] and [u8;4096] arrays.

Would any significant speed-up be gained by optimising for fixed-size arrays, or is the computational weight of blake3 large enough that it wouldn't make even a 5% difference (vs hashing slices)?

(The hashes of [u8; 32] are blake3 being applied twice.)

oconnor663 commented 1 year ago

I've never measured this directly, but I don't think it will make much of a difference. Like you suggested, the overwhelming majority of computational work is the same either way. Hardcoding the length would remove a few branches on the way down the call tree, which shouldn't matter much. The memcpy of 32 bytes into the block buffer might also save a few instructions if the size is known, but I also expect that to be in the noise.

fadedbee commented 1 year ago

Thanks.