From f7dbd4c3e003833dd8c361d1533f938b9c6f8400 Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Sun, 11 Jul 2021 00:55:24 -0700 Subject: [PATCH] :racehorse: made logs copy construct from thread_local --- include/log.h | 2 +- src/bank.cpp | 12 ++++++------ src/log.cpp | 6 +++--- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/include/log.h b/include/log.h index 0505d46..5552add 100644 --- a/include/log.h +++ b/include/log.h @@ -23,6 +23,6 @@ public: #endif const std::string &GetLogs() noexcept; - void AddTrans(Transaction &&t) noexcept; + void AddTrans(const Transaction &t) noexcept; Json::Value Serialize() const; // to be removed later }; diff --git a/src/bank.cpp b/src/bank.cpp index ae28496..1a5daf8 100644 --- a/src/bank.cpp +++ b/src/bank.cpp @@ -10,7 +10,7 @@ bool ValidUsername(const std::string &name) noexcept } for (const char &c : name) { - if (!((c > 96 && c < 122) || std::isdigit(c) || c == '_')) + if (!((c >= 97 && c <= 122) || std::isdigit(c) || c == '_')) { return false; } @@ -103,9 +103,9 @@ BankResponse Bank::SendFunds(const std::string &a_name, const std::string &b_nam BankResponse state; std::shared_lock lock{save_lock}; //about 10% of this function's cost #if MAX_LOG_SIZE > 0 - Transaction temp(a_name, b_name, amount); + static thread_local Transaction temp(a_name, b_name, amount); #endif - if (!users.modify_if(a_name, [&temp, &state, amount](User &a) { + if (!users.modify_if(a_name, [&state, amount](User &a) { //if A can afford it if (a.balance < amount) { @@ -115,7 +115,7 @@ BankResponse Bank::SendFunds(const std::string &a_name, const std::string &b_nam { a.balance -= amount; #if MAX_LOG_SIZE > 0 - a.log.AddTrans(Transaction(temp)); //about 40% of this function's cost + a.log.AddTrans(temp); //about 40% of this function's cost #endif state = BankResponse(k200OK, std::to_string(a.balance)); } @@ -126,9 +126,9 @@ BankResponse Bank::SendFunds(const std::string &a_name, const std::string &b_nam if (state.first == k200OK) { #if MAX_LOG_SIZE > 0 - users.modify_if(b_name, [&temp, amount](User &b) { + users.modify_if(b_name, [amount](User &b) { b.balance += amount; - b.log.AddTrans(std::move(temp)); + b.log.AddTrans(temp); }); //about 40% of this function's cost #else users.modify_if(b_name, [amount](User &b) { b.balance += amount; }); diff --git a/src/log.cpp b/src/log.cpp index 1317cf3..6915540 100644 --- a/src/log.cpp +++ b/src/log.cpp @@ -1,10 +1,10 @@ #include "log.h" -void Log::AddTrans(Transaction &&t) noexcept +void Log::AddTrans(const Transaction &t) noexcept { log_flag.SetChangesOn(); #if MAX_LOG_SIZE == 1 - data = std::move(t); + data = t; #else if (data.size() == MAX_LOG_SIZE) // If we hit the max size { @@ -15,7 +15,7 @@ void Log::AddTrans(Transaction &&t) noexcept data[data.size() - 1] = std::move(t); // Place new in opened spot return; } - data.push_back(std::move(t)); // In either case we have space under max length, move to new spot + data.push_back(t); // In either case we have space under max length, move to new spot #endif }