@@ -307,11 +307,31 @@ static void start(HashAlgorithm ha, Ctx & ctx)
307307 else if (ha == HashAlgorithm::SHA512) SHA512_Init (&ctx.sha512 );
308308}
309309
310+ // BLAKE3 data size threshold beyond which parallel hashing with TBB is likely faster.
311+ //
312+ // NOTE: This threshold is based on the recommended rule-of-thumb from the official BLAKE3 documentation for typical
313+ // x86_64 hardware as of 2025. In the future it may make sense to allow the user to tune this through nix.conf.
314+ const size_t blake3TbbThreshold = 128000 ;
315+
316+ // Decide which BLAKE3 update strategy to use based on some heuristics. Currently this just checks the data size but in
317+ // the future it might also take into consideration available system resources or the presence of a shared-memory
318+ // capable GPU for a heterogenous compute implementation.
319+ void blake3_hasher_update_with_heuristics (blake3_hasher * blake3, std::string_view data)
320+ {
321+ #ifdef BLAKE3_USE_TBB
322+ if (data.size () >= blake3TbbThreshold) {
323+ blake3_hasher_update_tbb (blake3, data.data (), data.size ());
324+ } else
325+ #endif
326+ {
327+ blake3_hasher_update (blake3, data.data (), data.size ());
328+ }
329+ }
310330
311331static void update (HashAlgorithm ha, Ctx & ctx,
312332 std::string_view data)
313333{
314- if (ha == HashAlgorithm::BLAKE3) blake3_hasher_update (&ctx.blake3 , data. data (), data. size () );
334+ if (ha == HashAlgorithm::BLAKE3) blake3_hasher_update_with_heuristics (&ctx.blake3 , data);
315335 else if (ha == HashAlgorithm::MD5) MD5_Update (&ctx.md5 , data.data (), data.size ());
316336 else if (ha == HashAlgorithm::SHA1) SHA1_Update (&ctx.sha1 , data.data (), data.size ());
317337 else if (ha == HashAlgorithm::SHA256) SHA256_Update (&ctx.sha256 , data.data (), data.size ());
0 commit comments