From 01648b3bd6d1f9ed3dfe508de6dc1643a2b8a4fd Mon Sep 17 00:00:00 2001 From: hyc Date: Fri, 4 Oct 2019 07:31:32 +0100 Subject: [PATCH] Remember cache input data (#133) Make init_cache and set_cache no-ops if fed the same data as before --- src/dataset.hpp | 1 + src/randomx.cpp | 16 +++++++++++++--- src/randomx.h | 4 +++- src/virtual_machine.hpp | 4 +++- 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/dataset.hpp b/src/dataset.hpp index a8a2ae1..d80945c 100644 --- a/src/dataset.hpp +++ b/src/dataset.hpp @@ -50,6 +50,7 @@ struct randomx_cache { randomx::DatasetInitFunc* datasetInit; randomx::SuperscalarProgram programs[RANDOMX_CACHE_ACCESSES]; std::vector reciprocalCache; + std::string cacheKey; bool isInitialized() { return programs[0].getSize() != 0; diff --git a/src/randomx.cpp b/src/randomx.cpp index 8b32894..9c743d7 100644 --- a/src/randomx.cpp +++ b/src/randomx.cpp @@ -93,7 +93,12 @@ extern "C" { void randomx_init_cache(randomx_cache *cache, const void *key, size_t keySize) { assert(cache != nullptr); assert(keySize == 0 || key != nullptr); - cache->initialize(cache, key, keySize); + std::string cacheKey; + cacheKey.assign((const char *)key, keySize); + if (cache->cacheKey != cacheKey || !cache->isInitialized()) { + cache->initialize(cache, key, keySize); + cache->cacheKey = cacheKey; + } } void randomx_release_cache(randomx_cache* cache) { @@ -274,8 +279,10 @@ extern "C" { UNREACHABLE; } - if(cache != nullptr) + if(cache != nullptr) { vm->setCache(cache); + vm->cacheKey = cache->cacheKey; + } if(dataset != nullptr) vm->setDataset(dataset); @@ -293,7 +300,10 @@ extern "C" { void randomx_vm_set_cache(randomx_vm *machine, randomx_cache* cache) { assert(machine != nullptr); assert(cache != nullptr && cache->isInitialized()); - machine->setCache(cache); + if (machine->cacheKey != cache->cacheKey) { + machine->setCache(cache); + machine->cacheKey = cache->cacheKey; + } } void randomx_vm_set_dataset(randomx_vm *machine, randomx_dataset *dataset) { diff --git a/src/randomx.h b/src/randomx.h index 1293800..5054c23 100644 --- a/src/randomx.h +++ b/src/randomx.h @@ -71,6 +71,7 @@ RANDOMX_EXPORT randomx_cache *randomx_alloc_cache(randomx_flags flags); /** * Initializes the cache memory and SuperscalarHash using the provided key value. + * Does nothing if called again with the same key value. * * @param cache is a pointer to a previously allocated randomx_cache structure. Must not be NULL. * @param key is a pointer to memory which contains the key value. Must not be NULL. @@ -162,7 +163,8 @@ RANDOMX_EXPORT randomx_vm *randomx_create_vm(randomx_flags flags, randomx_cache /** * Reinitializes a virtual machine with a new Cache. This function should be called anytime - * the Cache is reinitialized with a new key. + * the Cache is reinitialized with a new key. Does nothing if called with a Cache containing + * the same key value as already set. * * @param machine is a pointer to a randomx_vm structure that was initialized * without RANDOMX_FLAG_FULL_MEM. Must not be NULL. diff --git a/src/virtual_machine.hpp b/src/virtual_machine.hpp index 41f1134..d662c89 100644 --- a/src/virtual_machine.hpp +++ b/src/virtual_machine.hpp @@ -65,6 +65,8 @@ protected: randomx_dataset* datasetPtr; }; uint64_t datasetOffset; +public: + std::string cacheKey; }; namespace randomx { @@ -80,4 +82,4 @@ namespace randomx { void generateProgram(void* seed); }; -} \ No newline at end of file +}