Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/dataset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ struct randomx_cache {
randomx::DatasetInitFunc* datasetInit;
randomx::SuperscalarProgram programs[RANDOMX_CACHE_ACCESSES];
std::vector<uint64_t> reciprocalCache;
std::string cacheData;

bool isInitialized() {
return programs[0].getSize() != 0;
Expand Down
12 changes: 10 additions & 2 deletions src/randomx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 data;
data.assign((const char *)key, keySize);
if (cache->cacheData != data) {
cache->initialize(cache, key, keySize);
cache->cacheData = data;
}
}

void randomx_release_cache(randomx_cache* cache) {
Expand Down Expand Up @@ -293,7 +298,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->cacheData != cache->cacheData) {
machine->setCache(cache);
machine->cacheData = cache->cacheData;
}
}

void randomx_vm_set_dataset(randomx_vm *machine, randomx_dataset *dataset) {
Expand Down
4 changes: 3 additions & 1 deletion src/randomx.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 3 additions & 1 deletion src/virtual_machine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ class randomx_vm {
randomx_dataset* datasetPtr;
};
uint64_t datasetOffset;
public:
std::string cacheData;
};

namespace randomx {
Expand All @@ -80,4 +82,4 @@ namespace randomx {
void generateProgram(void* seed);
};

}
}