From 6b01735655b7b4ac5076f0f7c59ad7bd7c5b3121 Mon Sep 17 00:00:00 2001 From: ITotalJustice <47043333+ITotalJustice@users.noreply.github.com> Date: Wed, 28 May 2025 15:16:32 +0100 Subject: [PATCH] multi-threaded hasher. --- sphaira/source/hasher.cpp | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/sphaira/source/hasher.cpp b/sphaira/source/hasher.cpp index 624a6ae..da48249 100644 --- a/sphaira/source/hasher.cpp +++ b/sphaira/source/hasher.cpp @@ -1,5 +1,6 @@ #include "hasher.hpp" #include "app.hpp" +#include "threaded_file_transfer.hpp" #include namespace sphaira::hash { @@ -136,22 +137,18 @@ private: }; Result Hash(ui::ProgressBox* pbox, std::unique_ptr hash, std::shared_ptr source, std::string& out) { - s64 size; - R_TRY(source->Size(&size)); + s64 file_size; + R_TRY(source->Size(&file_size)); - s64 offset{}; - std::vector chunk(1024 * 512); - while (offset < size) { - R_TRY(pbox->ShouldExitResult()); - const auto rsize = std::min(chunk.size(), size - offset); - - u64 bytes_read; - R_TRY(source->Read(chunk.data(), offset, rsize, &bytes_read)); - hash->Update(chunk.data(), bytes_read); - - offset += bytes_read; - pbox->UpdateTransfer(offset, size); - } + R_TRY(thread::Transfer(pbox, file_size, + [&](void* data, s64 off, s64 size, u64* bytes_read) -> Result { + return source->Read(data, off, size, bytes_read); + }, + [&](const void* data, s64 off, s64 size) -> Result { + hash->Update(data, size); + R_SUCCEED(); + } + )); hash->Get(out); R_SUCCEED();