🐎 6% faster sendfunds

This commit is contained in:
EntireTwix 2021-06-19 15:19:44 -07:00
parent a08d53bf34
commit 2dc1c0c369

View file

@ -75,81 +75,78 @@ int_fast8_t Bank::SendFunds(const std::string &a_name, const std::string &b_name
return ErrorResponse::InvalidRequest; return ErrorResponse::InvalidRequest;
} }
//as first modify_if checks a_name and grabs unique lock //as first modify_if checks a_name and grabs unique lock
if (!Contains(b_name)) if (!Contains(b_name) || !Contains(a_name))
{ {
return ErrorResponse::UserNotFound; return ErrorResponse::UserNotFound;
} }
int_fast8_t state = false; int_fast8_t state = false;
{ {
std::shared_lock<std::shared_mutex> lock{send_funds_l}; //because SendFunds requires 3 locking operations if constexpr (max_log_size > 0)
if (!users.modify_if(a_name, [&state, amount, &attempt](User &a) { {
//if A exists, A can afford it, and A's password matches Transaction temp(a_name, b_name, amount);
std::shared_lock<std::shared_mutex> lock{send_funds_l};
users.modify_if(a_name, [&temp, &state, amount, &attempt](User &a) {
//if A can afford it and A's password matches attempt
if (a.balance < amount) if (a.balance < amount)
{ {
state = ErrorResponse::InsufficientFunds; state = ErrorResponse::InsufficientFunds;
} }
else if (a.password != XXH3_64bits(attempt.data(), attempt.size()))
{
state = ErrorResponse::WrongPassword;
}
else else
{ {
if (a.password != XXH3_64bits(attempt.data(), attempt.size())) a.balance -= amount;
{ a.log.AddTrans(std::forward<Transaction>(temp));
state = ErrorResponse::WrongPassword; state = true;
}
else
{
a.balance -= amount;
state = true;
}
} }
})) });
{ if (state > 0)
return ErrorResponse::UserNotFound;
}
else
{
if (!state > 0)
{ {
return state; users.modify_if(b_name, [&a_name, &b_name, &temp, amount](User &b) {
b.balance += amount;
b.log.AddTrans(std::move(temp));
});
return true;
} }
else else
{ {
if constexpr (max_log_size > 0) return state;
}
}
else
{
std::shared_lock<std::shared_mutex> lock{send_funds_l};
users.modify_if(a_name, [&state, amount, &attempt](User &a) {
//if A can afford it and A's password matches attempt
if (a.balance < amount)
{ {
Transaction temp; state = ErrorResponse::InsufficientFunds;
//if B does exist }
if (!users.modify_if(b_name, [&a_name, &b_name, &temp, amount](User &b) { else if (a.password != XXH3_64bits(attempt.data(), attempt.size()))
temp = Transaction(a_name, b_name, amount); {
b.balance += amount;
b.log.AddTrans(std::forward<Transaction>(temp)); state = ErrorResponse::WrongPassword;
}))
{
//attempt to refund if A exist
users.modify_if(a_name, [amount](User &a) {
a.balance += amount;
});
return ErrorResponse::UserNotFound; //because had to refund transaction
}
else
{
users.modify_if(a_name, [&temp](User &a) {
a.log.AddTrans(std::move(temp));
});
return true;
}
} }
else else
{ {
if (!users.modify_if(b_name, [amount](User &b) { a.balance -= amount;
b.balance += amount; state = true;
}))
{
//attempt to refund if A exist
users.modify_if(a_name, [amount](User &a) {
a.balance += amount;
});
return ErrorResponse::UserNotFound; //because had to refund transaction
}
} }
});
if (state > 0)
{
users.modify_if(b_name, [&a_name, &b_name, amount](User &b) {
b.balance += amount;
});
return true;
}
else
{
return state;
} }
} }
} }