mirror of
https://github.com/Expand-sys/CCash
synced 2026-03-23 04:57:09 +11:00
Made allocation/adding of logs more effecient
Logging update, adding transactions optimized
This commit is contained in:
commit
fa95322032
4 changed files with 26 additions and 38 deletions
|
|
@ -7,39 +7,25 @@
|
||||||
struct Log
|
struct Log
|
||||||
{
|
{
|
||||||
std::vector<Transaction> data;
|
std::vector<Transaction> 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() == max_log_size) // If we hit the max size
|
||||||
{
|
{
|
||||||
if (data.size() + pre_log_size > max_log_size) //if prefetched memory is larger then max
|
for (auto i = data.size() - 1; i > 0; i--) // Make room at the back
|
||||||
{
|
{
|
||||||
//std::cout << "allocating " << max_log_size << '\n';
|
data[i - 1] == std::move(data[i]) // Shifts everything left
|
||||||
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)
|
else if (data.size() == data.capacity()) // If we haven't hit the max but hit capacity
|
||||||
{
|
{
|
||||||
for (size_t i = data.size() - 1; i > 0; --i)
|
data.reserve(data.capacity() + pre_alloc) // Reserve more memory
|
||||||
{
|
|
||||||
data[i] = std::move(data[i - 1]);
|
|
||||||
}
|
}
|
||||||
data[0] = std::move(v);
|
data[data.size() - 1] = std::move(t) // In any case, place new at the back
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
data.push_back(std::move(v));
|
|
||||||
}
|
|
||||||
//std::cout << "size is " << data.size() << '\n';
|
|
||||||
}
|
}
|
||||||
Json::Value Serialize() const
|
Json::Value Serialize() const
|
||||||
{
|
{
|
||||||
Json::Value res;
|
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]["to"] = data[i].to;
|
||||||
res[i]["from"] = data[i].from;
|
res[i]["from"] = data[i].from;
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
#pragma once
|
#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)
|
// `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;
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
#include <json/json.h>
|
#include <json/json.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <algorithm>
|
||||||
#include "log.hpp"
|
#include "log.hpp"
|
||||||
|
|
||||||
struct User
|
struct User
|
||||||
|
|
@ -35,19 +36,16 @@ struct User
|
||||||
{
|
{
|
||||||
if (log_j.size())
|
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[i] = std::move(Transaction(
|
||||||
log.data.reserve(log_j.size() + pre_log_size); //allocate that amount
|
log_j[i]["from"].asCString(),
|
||||||
}
|
log_j[i]["to"].asCString(),
|
||||||
else
|
log_j[i]["amount"].asUInt(),
|
||||||
{
|
log_j[i]["time"].asUInt64()
|
||||||
//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())));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
4
main.cpp
4
main.cpp
|
|
@ -22,7 +22,9 @@ void SaveSig(int s)
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
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)
|
if (argc != 4)
|
||||||
{
|
{
|
||||||
std::cerr << "Usage: sudo ./bank <admin password> <saving frequency in minutes> <threads>\n";
|
std::cerr << "Usage: sudo ./bank <admin password> <saving frequency in minutes> <threads>\n";
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue