🐎 optimized for when log == 1

This commit is contained in:
EntireTwix 2021-07-04 20:34:49 -07:00
parent 6026e6aad5
commit 3edd25dbe8
2 changed files with 29 additions and 1 deletions

View file

@ -1,6 +1,6 @@
#pragma once #pragma once
#include <json/json.h> // to be removed later #include <json/json.h> // to be removed later
#include <array> #include <vector>
#include <algorithm> #include <algorithm>
#include "ccash_config.hpp" #include "ccash_config.hpp"
#include "change_flag.h" #include "change_flag.h"
@ -13,7 +13,11 @@ private:
Json::Value log_snapshot; Json::Value log_snapshot;
public: public:
#if MAX_LOG_SIZE == 1
Transaction data;
#else
std::vector<Transaction> data; std::vector<Transaction> data;
#endif
const Json::Value &GetLog() noexcept; const Json::Value &GetLog() noexcept;
void AddTrans(Transaction &&t) noexcept; void AddTrans(Transaction &&t) noexcept;

View file

@ -2,6 +2,9 @@
void Log::AddTrans(Transaction &&t) noexcept void Log::AddTrans(Transaction &&t) noexcept
{ {
#if MAX_LOG_SIZE == 1
data = std::move(t);
#else
if (data.size() == MAX_LOG_SIZE) // If we hit the max size if (data.size() == MAX_LOG_SIZE) // If we hit the max size
{ {
for (uint32_t i = 1; i < data.size(); i++) // Make room at the back for (uint32_t i = 1; i < data.size(); i++) // Make room at the back
@ -16,6 +19,7 @@ void Log::AddTrans(Transaction &&t) noexcept
data.reserve(data.capacity() + PRE_LOG_SIZE); // Reserve more memory data.reserve(data.capacity() + PRE_LOG_SIZE); // Reserve more memory
} }
data.push_back(std::move(t)); // In either case we have space under max length, move to new spot data.push_back(std::move(t)); // In either case we have space under max length, move to new spot
#endif
log_flag.SetChangesOn(); log_flag.SetChangesOn();
} }
@ -25,6 +29,15 @@ const Json::Value &Log::GetLog() noexcept
{ {
//re-generate snapshot //re-generate snapshot
Json::Value res; Json::Value res;
#if MAX_LOG_SIZE == 1
res[0]["to"] = data.to;
res[0]["from"] = data.from;
#ifdef _USE_32BIT_TIME_T
res[0]["time"] = (Json::UInt)data.time;
#else
res[0]["time"] = (Json::UInt64)data.time;
#endif
#else
for (uint32_t i = data.size(); i > 0; --i) for (uint32_t i = data.size(); i > 0; --i)
{ {
res[i - 1]["to"] = data[data.size() - i].to; res[i - 1]["to"] = data[data.size() - i].to;
@ -36,6 +49,7 @@ const Json::Value &Log::GetLog() noexcept
res[i - 1]["time"] = (Json::UInt64)data[data.size() - i].time; res[i - 1]["time"] = (Json::UInt64)data[data.size() - i].time;
#endif #endif
} }
#endif
log_flag.SetChangesOff(); log_flag.SetChangesOff();
log_snapshot = res; log_snapshot = res;
} }
@ -45,6 +59,15 @@ const Json::Value &Log::GetLog() noexcept
Json::Value Log::Serialize() const Json::Value Log::Serialize() const
{ {
Json::Value res; Json::Value res;
#if MAX_LOG_SIZE == 1
res[0]["to"] = data.to;
res[0]["from"] = data.from;
#ifdef _USE_32BIT_TIME_T
res[0]["time"] = (Json::UInt)data.time;
#else
res[0]["time"] = (Json::UInt64)data.time;
#endif
#else
for (uint32_t i = 0; i < data.size(); ++i) for (uint32_t i = 0; i < data.size(); ++i)
{ {
res[i]["to"] = data[i].to; res[i]["to"] = data[i].to;
@ -56,5 +79,6 @@ Json::Value Log::Serialize() const
res[i]["time"] = (Json::UInt64)data[i].time; res[i]["time"] = (Json::UInt64)data[i].time;
#endif #endif
} }
#endif
return res; return res;
} }