Skip to main content

hash_multithreaded

Function hash_multithreaded 

Source
pub fn hash_multithreaded(input: &[u8]) -> Hash
Expand description

The hash of input, using other CPU cores where that is faster: the same Hash as hash, never slower.

let hash = blake3_servil::hash_multithreaded(&[0u8; 1 << 20]);
assert_eq!(hash, blake3_servil::hash(&[0u8; 1 << 20]));

The default choice for one message in memory.

Speed, on an Apple M4 Max (16 cores): below 512 KiB it hashes on the calling thread, at hash’s speed; from 512 KiB it uses the other cores too: about 12 GB/s at 1 MiB, 20 GB/s at 4 MiB, and 25 GB/s from 16 MiB, 4 to 6 times hash. The extra threads wait asleep between calls and use no CPU then.

Several callers at once: calls from several threads of one program share the cores in turn, each getting a fair part; a call that finds the cores all busy hashes on its own thread, at hash’s speed.

CPUs with performance and efficiency cores (Intel’s since Alder Lake): a multithreaded call or stream uses every core, and afterwards the OS sometimes leaves the calling thread on an efficiency core for a while, where single-threaded hashing runs at about half speed (AVX2’s 256-bit operations run as two halves there). Measured on an i7-12700K under Linux 7.0: beside nonstop multithreaded streams, 7% of 16 KiB single-threaded hashes ran partly on an efficiency core, 2.2 times slower, about 6% on their average. A program that alternates the two and needs its single-threaded speed steady can keep that thread on the performance cores (sched_setaffinity on Linux).

The program calls initialize_multithreaded first; a call before it panics.