@@ -307,11 +307,28 @@ static void start(HashAlgorithm ha, Ctx & ctx)
307
307
else if (ha == HashAlgorithm::SHA512) SHA512_Init (&ctx.sha512 );
308
308
}
309
309
310
+ // BLAKE3 data size threshold beyond which parallel hashing with TBB is likely faster.
311
+ const size_t blake3TbbThreshold = 128000 ;
312
+
313
+ // Decide which BLAKE3 update strategy to use based on some heuristics. Currently this just checks the data size but in
314
+ // the future it might also take into consideration available system resources or the presence of a shared-memory
315
+ // capable GPU for a heterogenous compute implementation.
316
+ void blake3_hasher_update_with_heuristics (blake3_hasher * blake3, std::string_view data)
317
+ {
318
+ #ifdef BLAKE3_USE_TBB
319
+ if (data.size () >= blake3TbbThreshold) {
320
+ blake3_hasher_update_tbb (blake3, data.data (), data.size ());
321
+ } else
322
+ #endif
323
+ {
324
+ blake3_hasher_update (blake3, data.data (), data.size ());
325
+ }
326
+ }
310
327
311
328
static void update (HashAlgorithm ha, Ctx & ctx,
312
329
std::string_view data)
313
330
{
314
- if (ha == HashAlgorithm::BLAKE3) blake3_hasher_update (&ctx.blake3 , data. data (), data. size () );
331
+ if (ha == HashAlgorithm::BLAKE3) blake3_hasher_update_with_heuristics (&ctx.blake3 , data);
315
332
else if (ha == HashAlgorithm::MD5) MD5_Update (&ctx.md5 , data.data (), data.size ());
316
333
else if (ha == HashAlgorithm::SHA1) SHA1_Update (&ctx.sha1 , data.data (), data.size ());
317
334
else if (ha == HashAlgorithm::SHA256) SHA256_Update (&ctx.sha256 , data.data (), data.size ());
0 commit comments