From a2287a4d369df6d6db05c427627576097c71b624 Mon Sep 17 00:00:00 2001 From: doggo Date: Thu, 3 Jun 2021 16:40:08 -0500 Subject: [PATCH 1/5] Logging update, adding transactions optimized --- include/log.hpp | 31 ++++++++----------------------- include/log_consts.hpp | 6 ++++-- 2 files changed, 12 insertions(+), 25 deletions(-) diff --git a/include/log.hpp b/include/log.hpp index 6393ce3..d36f795 100644 --- a/include/log.hpp +++ b/include/log.hpp @@ -7,39 +7,24 @@ struct Log { std::vector data; - void AddTrans(Transaction &&v) + void AddTrans(Transaction &&t) { - if (data.capacity() == data.size() && data.size() < max_log_size) //if memory reserved is full and max isnt reached - { - if (data.size() + pre_log_size > max_log_size) //if prefetched memory is larger then max - { - //std::cout << "allocating " << max_log_size << '\n'; - data.reserve(max_log_size); //just allocate max - } - else - { - //std::cout << "allocating " << data.size() + pre_log_size << '\n'; - data.reserve(data.size() + pre_log_size); //prefetching memory - } - } if (data.size() == max_log_size) { - for (size_t i = data.size() - 1; i > 0; --i) + for (auto i = data.size() - 1; i > 0; i--) { - data[i] = std::move(data[i - 1]); + data[i - 1] == std::move(data[i]) } - data[0] = std::move(v); } - else - { - data.push_back(std::move(v)); + else if (data.size() == data.capacity()) { + data.reserve(data.capacity() + pre_alloc) } - //std::cout << "size is " << data.size() << '\n'; + data[data.size() - 1] = std::move(t) } Json::Value Serialize() const { Json::Value res; - for (uint32_t i = 0; i < data.size(); ++i) + for (uint32_t i = 0; i < end; ++i) { res[i]["to"] = data[i].to; res[i]["from"] = data[i].from; @@ -48,4 +33,4 @@ struct Log } return res; } -}; \ No newline at end of file +}; diff --git a/include/log_consts.hpp b/include/log_consts.hpp index 614b9c7..ea041e3 100644 --- a/include/log_consts.hpp +++ b/include/log_consts.hpp @@ -1,3 +1,5 @@ #pragma once -constexpr auto max_log_size = 100; // -constexpr auto pre_log_size = 10; //amount allocated in advance (for example 5 would allocate every 5 logs) \ No newline at end of file + +// `max_log_size` must be divisible by `pre_alloc` +constexpr auto max_log_size = 100; +constexpr auto pre_alloc = 10; From af51335aa7b416c817855c4896340ffcaea7791c Mon Sep 17 00:00:00 2001 From: doggo Date: Thu, 3 Jun 2021 16:50:13 -0500 Subject: [PATCH 2/5] Code comments for logs --- include/log.hpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/include/log.hpp b/include/log.hpp index d36f795..e3d7bdb 100644 --- a/include/log.hpp +++ b/include/log.hpp @@ -9,17 +9,18 @@ struct Log std::vector data; void AddTrans(Transaction &&t) { - if (data.size() == max_log_size) + if (data.size() == max_log_size) // If we hit the max size { - for (auto i = data.size() - 1; i > 0; i--) + for (auto i = data.size() - 1; i > 0; i--) // Make room at the back { data[i - 1] == std::move(data[i]) } } - else if (data.size() == data.capacity()) { - data.reserve(data.capacity() + pre_alloc) + else if (data.size() == data.capacity()) // If we haven't hit the max but hit capacity + { + data.reserve(data.capacity() + pre_alloc) // Reserve more memory } - data[data.size() - 1] = std::move(t) + data[data.size() - 1] = std::move(t) // In any case, place new at the back } Json::Value Serialize() const { From ab1110161150fc90a5de632c9b651a63e88a805a Mon Sep 17 00:00:00 2001 From: doggo Date: Thu, 3 Jun 2021 17:36:44 -0500 Subject: [PATCH 3/5] Idiot changed the variable name --- include/log_consts.hpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/log_consts.hpp b/include/log_consts.hpp index ea041e3..55dc7f5 100644 --- a/include/log_consts.hpp +++ b/include/log_consts.hpp @@ -1,5 +1,5 @@ #pragma once -// `max_log_size` must be divisible by `pre_alloc` -constexpr auto max_log_size = 100; -constexpr auto pre_alloc = 10; +// `max_log_size` must be divisible by `pre_log_size` +constexpr auto max_log_size = 100; // Setting to 0 does not compile logging +constexpr auto pre_log_size = 10; From 69121557a962b30b6bcc9338c1b9672bf1f31210 Mon Sep 17 00:00:00 2001 From: doggo Date: Thu, 3 Jun 2021 19:11:32 -0500 Subject: [PATCH 4/5] User constructor now enforces logging rule --- include/log.hpp | 2 +- include/user.hpp | 22 ++++++++++------------ 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/include/log.hpp b/include/log.hpp index e3d7bdb..af2d55a 100644 --- a/include/log.hpp +++ b/include/log.hpp @@ -13,7 +13,7 @@ struct Log { for (auto i = data.size() - 1; i > 0; i--) // Make room at the back { - data[i - 1] == std::move(data[i]) + data[i - 1] == std::move(data[i]) // Shifts everything left } } else if (data.size() == data.capacity()) // If we haven't hit the max but hit capacity diff --git a/include/user.hpp b/include/user.hpp index 2874728..d685a91 100644 --- a/include/user.hpp +++ b/include/user.hpp @@ -1,6 +1,7 @@ #pragma once #include #include +#include #include "log.hpp" struct User @@ -35,19 +36,16 @@ struct User { if (log_j.size()) { - if (max_log_size > (log_j.size() + pre_log_size)) //if current imported log's size + prefetch amount is less then max + auto size = ((log_j.size() / pre_log_size) + 1) * pre_log_size; // Ensures that we have a log size aligned on a multiple of `pre_log_size` + log.data.reserve(std::min(size, max_log_size)); // Ensures that the log size is under `max_log_size` + for (uint32_t i = 0; i < log.size(); i++) // Matches the logs { - //std::cout << "allocating " << log_j.size() + pre_log_size << '\n'; - log.data.reserve(log_j.size() + pre_log_size); //allocate that amount - } - else - { - //std::cout << "allocating " << max_log_size << '\n'; - log.data.reserve(max_log_size); //allocate max amount - } - for (uint32_t i = 0; i < log_j.size(); ++i) - { - log.data.push_back(std::move(Transaction(log_j[i]["from"].asCString(), log_j[i]["to"].asCString(), log_j[i]["amount"].asUInt(), log_j[i]["time"].asUInt64()))); + log.data[i] = std::move(Transaction( + log_j[i]["from"].asCString(), + log_j[i]["to"].asCString(), + log_j[i]["amount"].asUInt(), + log_j[i]["time"].asUInt64() + )); } } } From ae8ef4ae4eb5becc07e59a6f0b3295998ea940a6 Mon Sep 17 00:00:00 2001 From: doggo Date: Thu, 3 Jun 2021 19:21:26 -0500 Subject: [PATCH 5/5] Added a static assertation --- main.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/main.cpp b/main.cpp index b12b4a9..3932b2a 100644 --- a/main.cpp +++ b/main.cpp @@ -22,7 +22,9 @@ void SaveSig(int s) int main(int argc, char **argv) { - static_assert(pre_log_size < max_log_size); + static_assert(pre_log_size < max_log_size, "`max_log_size` must be larger than `pre_log_size`."); + static_assert(!(max_log_size % pre_log_size), "`max_log_size` must be a multiple of `pre_log_size`."); + if (argc != 4) { std::cerr << "Usage: sudo ./bank \n";