Skip to main content

update_each

Function update_each 

Source
pub fn update_each(hashers: &mut [Hasher], pieces: &[(usize, &[u8])])
Expand description

Feed each piece to its hasher: (i, bytes) does hashers[i].update(bytes), each hasher’s pieces in their order in pieces. Call it once with all the pieces your program has in hand (one turn of its event loop): never slower than one update each: many messages arriving at once, such as a server’s uploads, each with its own Hasher. Requires each i < hashers.len(): a turn that panics on one that is not leaves its hashers as after a panic. Finish the messages with finalize_each: plain messages of up to 1024 bytes wait whole in their stage (on both x86-64 and AArch64), so those that finish together hash side by side there. Because a staged plain first chunk has not yet compressed into ChunkState, calling a non-consuming Hasher::finalize() mid-chunk rehashes that staged prefix afresh; callers that finish each message once via finalize_each avoid that cost.

§Panics

Panics on an out-of-range index, on a message exceeding 2^64 - 1 bytes, or on a poisoned hasher (after a panic); a turn that panics part-way may leave earlier pieces incorporated and later ones not.

§Or the Pipeline

A program with threads to give, whose messages keep arriving, has a faster way in the crate blake3-servil (this library with its thread pool): its throughput::Pipeline splits across worker cores (historically beating pre-batching update_each on 1,024 open 1 KiB messages on Ryzen 9700X / EPYC 9255, November 22, 2026, BLAKE3 #470, and taking a third of the time for 64 open messages of 1 MiB). It holds a room of 64 KiB for each open message. This call stays the right one:

  • on one thread;
  • for more open messages than the program has 64 KiB each for (a Hasher is 26 KiB, and touches only what it uses);
  • where a message’s running state is wanted: a clone, a finalize part-way;
  • for a small burst after a gap, about 256 KiB a turn, where the Pipeline’s workers must first wake: there it read from 6% faster to 20% slower than this call on the same two machines.

§Key-dependent timing

Hashers may use different secret keys. Grouping, kernel selection, and temporary storage sizes are independent of key values and key equality. See the key-independence contract.

use blake3_servil_core::{convenience::Hasher, throughput};
let mut hashers = vec![Hasher::new(blake3_servil_core::Mode::Plain, None), Hasher::new(blake3_servil_core::Mode::Plain, None)];
throughput::update_each(&mut hashers, &[(0, b"ab"), (1, b"xyz"), (0, b"c")]);
let mut out = [[0u8; 32]; 2];
throughput::finalize_each(&mut hashers, &[0, 1], &mut out);
let hash = blake3_servil_core::convenience::hash;
assert_eq!((&out[0], &out[1]), (hash(b"abc").as_bytes(), hash(b"xyz").as_bytes()));