From f03fe34009218532dbc966721c0b512723ec3560 Mon Sep 17 00:00:00 2001 From: Richard Stanway Date: Sun, 14 Jun 2020 03:04:29 +0200 Subject: [PATCH] UI/updater: Use 1 MB static memory for hashing Allocating a vector for the hundreds of small files and only reading 64k at a time was a bottleneck on systems that were not I/O bound. --- UI/win-update/updater/hash.cpp | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/UI/win-update/updater/hash.cpp b/UI/win-update/updater/hash.cpp index 936d84b3..a081b2a9 100644 --- a/UI/win-update/updater/hash.cpp +++ b/UI/win-update/updater/hash.cpp @@ -45,6 +45,7 @@ void StringToHash(const wchar_t *in, BYTE *out) bool CalculateFileHash(const wchar_t *path, BYTE *hash) { + static BYTE hashBuffer[1048576]; blake2b_state blake2; if (blake2b_init(&blake2, BLAKE2_HASH_LENGTH) != 0) return false; @@ -54,19 +55,16 @@ bool CalculateFileHash(const wchar_t *path, BYTE *hash) if (handle == INVALID_HANDLE_VALUE) return false; - vector buf; - buf.resize(65536); - for (;;) { DWORD read = 0; - if (!ReadFile(handle, buf.data(), (DWORD)buf.size(), &read, + if (!ReadFile(handle, hashBuffer, sizeof(hashBuffer), &read, nullptr)) return false; if (!read) break; - if (blake2b_update(&blake2, buf.data(), read) != 0) + if (blake2b_update(&blake2, hashBuffer, read) != 0) return false; } -- GitLab