From 2b715bb0deccefdbd7545bbd6a8e5b57b4fa0bea Mon Sep 17 00:00:00 2001 From: WerWolv Date: Fri, 6 Dec 2024 10:15:32 +0100 Subject: [PATCH] impr: Batch fills into larger chunks --- .../source/content/views/view_hex_editor.cpp | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/plugins/builtin/source/content/views/view_hex_editor.cpp b/plugins/builtin/source/content/views/view_hex_editor.cpp index 95f88ae67..188ebdb25 100644 --- a/plugins/builtin/source/content/views/view_hex_editor.cpp +++ b/plugins/builtin/source/content/views/view_hex_editor.cpp @@ -21,8 +21,10 @@ #include #include #include +#include using namespace std::literals::string_literals; +using namespace wolv::literals; namespace hex::plugin::builtin { @@ -449,9 +451,24 @@ namespace hex::plugin::builtin { auto provider = ImHexApi::Provider::get(); u32 patchCount = 0; - for (u64 i = 0; i < size; i += bytes.size()) { - auto remainingSize = std::min(size - i, bytes.size()); - provider->write(provider->getBaseAddress() + address + i, bytes.data(), remainingSize); + + // Group the fill pattern into a larger chunk + constexpr static auto BatchFillSize = 1_MiB; + std::vector batchData; + if (bytes.size() < BatchFillSize) { + batchData.resize(std::min(alignTo(BatchFillSize, bytes.size()), size)); + for (u64 i = 0; i < batchData.size(); i += bytes.size()) { + auto remainingSize = std::min(batchData.size() - i, bytes.size()); + std::copy_n(bytes.begin(), remainingSize, batchData.begin() + i); + } + } else { + batchData = std::move(bytes); + } + + const auto startAddress = provider->getBaseAddress() + address; + for (u64 i = 0; i < size; i += batchData.size()) { + auto remainingSize = std::min(size - i, batchData.size()); + provider->write(startAddress + i, batchData.data(), remainingSize); patchCount += 1; } provider->getUndoStack().groupOperations(patchCount, "hex.builtin.undo_operation.fill");