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 ++++++++-- WitnessRandomizer/Memory.h | 2 ++ 2 files changed, 10 insertions(+), 2 deletions(-) (limited to 'WitnessRandomizer') 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; } diff --git a/WitnessRandomizer/Memory.h b/WitnessRandomizer/Memory.h index 6882c9c..dc92349 100644 --- a/WitnessRandomizer/Memory.h +++ b/WitnessRandomizer/Memory.h @@ -40,6 +40,8 @@ private: void ThrowError(); uintptr_t ComputeOffset(std::vector offsets); + + std::map, uintptr_t> _computedOffsets; uintptr_t _baseAddress; HANDLE _handle; }; \ No newline at end of file -- cgit 1.4.1