From 2473736979b829e09130a7261b38eb8c1367a06d Mon Sep 17 00:00:00 2001 From: jbzdarkid Date: Wed, 24 Oct 2018 18:44:37 -0700 Subject: Perf: Cache memory offsets --- WitnessRandomizer/Memory.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'WitnessRandomizer/Memory.cpp') diff --git a/WitnessRandomizer/Memory.cpp b/WitnessRandomizer/Memory.cpp index c5852ac..f2bd8f1 100644 --- a/WitnessRandomizer/Memory.cpp +++ b/WitnessRandomizer/Memory.cpp @@ -58,16 +58,22 @@ void Memory::ThrowError() { uintptr_t Memory::ComputeOffset(std::vector offsets) { - uintptr_t cumulativeAddress = _baseAddress; - // Leave off the last offset, since it will be either read/write, and may not be of type unitptr_t. int final_offset = offsets.back(); offsets.pop_back(); + + auto search = _computedOffsets.find(offsets); + if (search != std::end(_computedOffsets)) { + return search->second + final_offset; + } + + uintptr_t cumulativeAddress = _baseAddress; for (int offset : offsets) { cumulativeAddress += offset; if (!ReadProcessMemory(_handle, (LPVOID)cumulativeAddress, &cumulativeAddress, sizeof(uintptr_t), NULL)) { ThrowError(); } } + _computedOffsets[offsets] = cumulativeAddress; return cumulativeAddress + final_offset; } -- cgit 1.4.1