From 3edd25dbe8b1311504e4cf704f74299e9f44fe2b Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Sun, 4 Jul 2021 20:34:49 -0700 Subject: [PATCH] :racehorse: optimized for when log == 1 --- include/log.h | 6 +++++- src/log.cpp | 24 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/include/log.h b/include/log.h index 2d1ef98..001fccc 100644 --- a/include/log.h +++ b/include/log.h @@ -1,6 +1,6 @@ #pragma once #include // to be removed later -#include +#include #include #include "ccash_config.hpp" #include "change_flag.h" @@ -13,7 +13,11 @@ private: Json::Value log_snapshot; public: +#if MAX_LOG_SIZE == 1 + Transaction data; +#else std::vector data; +#endif const Json::Value &GetLog() noexcept; void AddTrans(Transaction &&t) noexcept; diff --git a/src/log.cpp b/src/log.cpp index dd3b308..e37b58c 100644 --- a/src/log.cpp +++ b/src/log.cpp @@ -2,6 +2,9 @@ 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 { 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.push_back(std::move(t)); // In either case we have space under max length, move to new spot +#endif log_flag.SetChangesOn(); } @@ -25,6 +29,15 @@ const Json::Value &Log::GetLog() noexcept { //re-generate snapshot 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) { 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; #endif } +#endif log_flag.SetChangesOff(); log_snapshot = res; } @@ -45,6 +59,15 @@ const Json::Value &Log::GetLog() noexcept Json::Value Log::Serialize() const { 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) { res[i]["to"] = data[i].to; @@ -56,5 +79,6 @@ Json::Value Log::Serialize() const res[i]["time"] = (Json::UInt64)data[i].time; #endif } +#endif return res; }