cc: add a temperature calculation cache

since it queries a largeish number of database entries
This commit is contained in:
Crypto City 2020-11-15 01:43:29 +00:00
parent ca1d37ba7d
commit fb02b0b0de

View File

@ -1,3 +1,4 @@
#include <boost/thread/mutex.hpp>
#include "misc_log_ex.h"
#include "int-util.h"
#include "blockchain_db/blockchain_db.h"
@ -132,6 +133,14 @@ int32_t get_actual_temperature(const std::vector<std::pair<crypto::hash, int32_t
int32_t get_actual_temperature(const cryptonote::BlockchainDB &db, uint64_t height, uint32_t city)
{
static boost::mutex m;
boost::unique_lock<boost::mutex> lock(m);
static crypto::hash cached_hash = crypto::null_hash;
static int32_t cached_temperature = 0;
const crypto::hash hash = db.get_block_hash_from_height(height);
if (hash == cached_hash)
return cached_temperature;
std::vector<std::pair<uint64_t, int32_t>> samples;
get_temperature_anomaly_samples(height, samples);
@ -149,7 +158,11 @@ int32_t get_actual_temperature(const cryptonote::BlockchainDB &db, uint64_t heig
const bool cold_snap = !sed.empty() && sed.back().special_event == cc::SPECIAL_EVENT_COLD_SNAP && sed.back().duration == 0;
const bool heat_wave = !sed.empty() && sed.back().special_event == cc::SPECIAL_EVENT_HEAT_WAVE && sed.back().duration == 0;
const bool microclimate = (cd.specializations >> cc::CITY_SPECIALIZATION_MICRO_CLIMATE) & 1;
return get_actual_temperature(samples2, height, cold_snap, heat_wave, microclimate);
const int32_t actual_temperature = get_actual_temperature(samples2, height, cold_snap, heat_wave, microclimate);
cached_temperature = actual_temperature;
cached_hash = hash;
return actual_temperature;
}
std::string print_temperature(int32_t temperature)