Small string_view optimizations

Using string_view is slightly faster as it lets us get a view directly
into nlohmann's string value, rather than making an intermediate copy
into a temporary string.
This commit is contained in:
Jason Rhinelander 2023-01-10 22:26:28 -04:00
parent 64fbdb8c7f
commit 78192856ab
No known key found for this signature in database
GPG key ID: C4992CE7A88D4262

View file

@ -223,7 +223,7 @@ namespace {
std::ostringstream os;
const auto tvc = res["tvc"];
if (auto got = tvc.find("m_verbose_error"); got != tvc.end()) os << res["tvc"]["m_verbose_error"].get<std::string>() << "\n";
if (auto got = tvc.find("m_verbose_error"); got != tvc.end()) os << res["tvc"]["m_verbose_error"].get<std::string_view>() << "\n";
if (auto got = tvc.find("m_verifivation_failed"); got != tvc.end()) os << "Verification failed, connection should be dropped, "; //bad tx, should drop connection
if (auto got = tvc.find("m_verifivation_impossible"); got != tvc.end()) os << "Verification impossible, related to alt chain, "; //the transaction is related with an alternative blockchain
if (auto got = tvc.find("m_should_be_relayed"); got == tvc.end()) os << "TX should NOT be relayed, ";
@ -905,9 +905,9 @@ bool get_pruned_tx(const nlohmann::json& entry, cryptonote::transaction &tx, cry
if (entry["pruned"] && entry["prunable_hash"])
{
crypto::hash ph;
CHECK_AND_ASSERT_MES(tools::hex_to_type(entry["prunable_hash"].get<std::string>(), ph), false, "Failed to parse prunable hash");
CHECK_AND_ASSERT_MES(oxenc::is_hex(entry["pruned"].get<std::string>()), false, "Invalid pruned tx entry");
bd = oxenc::from_hex(entry["pruned"].get<std::string>());
CHECK_AND_ASSERT_MES(tools::hex_to_type(entry["prunable_hash"].get<std::string_view>(), ph), false, "Failed to parse prunable hash");
CHECK_AND_ASSERT_MES(oxenc::is_hex(entry["pruned"].get<std::string_view>()), false, "Invalid pruned tx entry");
bd = oxenc::from_hex(entry["pruned"].get<std::string_view>());
CHECK_AND_ASSERT_MES(parse_and_validate_tx_base_from_blob(bd, tx), false, "Invalid base tx data");
// only v2 txes can calculate their txid after pruned
if (bd[0] > 1)
@ -917,7 +917,7 @@ bool get_pruned_tx(const nlohmann::json& entry, cryptonote::transaction &tx, cry
else
{
// for v1, we trust the dameon
CHECK_AND_ASSERT_MES(tools::hex_to_type(entry["tx_hash"].get<std::string>(), tx_hash), false, "Failed to parse tx hash");
CHECK_AND_ASSERT_MES(tools::hex_to_type(entry["tx_hash"].get<std::string_view>(), tx_hash), false, "Failed to parse tx hash");
}
return true;
}
@ -5845,7 +5845,7 @@ void wallet2::trim_hashchain()
if (res["status"] == rpc::STATUS_OK)
{
crypto::hash hash;
tools::hex_to_type(res["block_header"]["hash"].get<std::string>(), hash);
tools::hex_to_type(res["block_header"]["hash"].get<std::string_view>(), hash);
m_blockchain.refill(hash);
}
else
@ -6650,7 +6650,6 @@ bool wallet2::is_transfer_unlocked(uint64_t unlock_time, uint64_t block_height,
return true;
{
std::optional<std::string> failed;
// FIXME: can just check one here by adding a is_key_image_blacklisted
auto [success, blacklist] = m_node_rpc_proxy.get_service_node_blacklisted_key_images();
if (!success)
@ -6663,7 +6662,7 @@ bool wallet2::is_transfer_unlocked(uint64_t unlock_time, uint64_t block_height,
for (auto const& entry : blacklist)
{
crypto::key_image check_image;
if(!tools::hex_to_type(entry["key_image"].get<std::string>(), check_image))
if(!tools::hex_to_type(entry["key_image"].get<std::string_view>(), check_image))
{
log::error(logcat, "Failed to parse hex representation of key image: {}", entry["key_image"]);
break;
@ -6676,7 +6675,6 @@ bool wallet2::is_transfer_unlocked(uint64_t unlock_time, uint64_t block_height,
{
const std::string primary_address = get_address_as_str();
std::optional<std::string> failed;
auto [success, service_nodes_states] = m_node_rpc_proxy.get_contributed_service_nodes(primary_address);
if (!success)
{
@ -8147,7 +8145,7 @@ wallet2::stake_result wallet2::check_stake_allowed(const crypto::public_key& sn_
for (const auto& contributor : snode_info["contributors"])
{
address_parse_info info;
if (!cryptonote::get_account_address_from_str(info, m_nettype, contributor["address"].get<std::string>()))
if (!cryptonote::get_account_address_from_str(info, m_nettype, contributor["address"].get<std::string_view>()))
continue;
if (info.address == addr_info.address)
@ -8544,7 +8542,7 @@ wallet2::request_stake_unlock_result wallet2::can_request_stake_unlock(const cry
for (auto const &contributor : node_info["contributors"])
{
address_parse_info address_info = {};
cryptonote::get_account_address_from_str(address_info, nettype(), contributor["address"].get<std::string>());
cryptonote::get_account_address_from_str(address_info, nettype(), contributor["address"].get<std::string_view>());
if (address_info.address != primary_address)
continue;
@ -8586,7 +8584,7 @@ wallet2::request_stake_unlock_result wallet2::can_request_stake_unlock(const cry
result.msg.append("Key image: ");
result.msg.append(contribution["key_image"]);
result.msg.append(" has already been requested to be unlocked, unlocking at height: ");
result.msg.append(node_info["requested_unlock_height"].get<std::string>());
result.msg.append(node_info["requested_unlock_height"].get<std::string_view>());
result.msg.append(" (about ");
result.msg.append(tools::get_human_readable_timespan(std::chrono::seconds((node_info["requested_unlock_height"].get<uint64_t>() - curr_height) * TARGET_BLOCK_TIME)));
result.msg.append(")");
@ -8629,7 +8627,7 @@ wallet2::request_stake_unlock_result wallet2::can_request_stake_unlock(const cry
result.msg.append(tools::get_human_readable_timespan(std::chrono::seconds((unlock_height - curr_height) * TARGET_BLOCK_TIME)));
result.msg.append(")");
if(!tools::hex_to_type(contribution["key_image"].get<std::string>(), unlock.key_image))
if(!tools::hex_to_type(contribution["key_image"].get<std::string_view>(), unlock.key_image))
{
result.msg = tr("Failed to parse hex representation of key image: ") + contribution["key_image"].get<std::string>();
return result;
@ -8763,7 +8761,7 @@ static ons_prepared_args prepare_tx_extra_oxen_name_system_values(wallet2 const
if ((*response)["entries"].size())
{
if (!tools::hex_to_type((*response)["entries"][0]["txid"].get<std::string>(), result.prev_txid))
if (!tools::hex_to_type((*response)["entries"][0]["txid"].get<std::string_view>(), result.prev_txid))
{
if (reason) *reason = "Failed to convert response txid=" + (*response)["entries"][0]["txid"].get<std::string>() + " from the daemon into a 32 byte hash, it must be a 64 char hex string";
return result;
@ -8782,7 +8780,7 @@ static ons_prepared_args prepare_tx_extra_oxen_name_system_values(wallet2 const
cryptonote::address_parse_info curr_backup_owner_parsed = {};
auto& rowner = (*response)["entries"].front()["owner"];
std::string* rbackup_owner = (*response)["entries"].front().value("backup_owner", nullptr);;
bool curr_owner = cryptonote::get_account_address_from_str(curr_owner_parsed, wallet.nettype(), rowner.get<std::string>());
bool curr_owner = cryptonote::get_account_address_from_str(curr_owner_parsed, wallet.nettype(), rowner.get<std::string_view>());
bool curr_backup_owner = rbackup_owner && cryptonote::get_account_address_from_str(curr_backup_owner_parsed, wallet.nettype(), *rbackup_owner);
if (!try_generate_ons_signature(wallet, rowner, owner, backup_owner, result))
{
@ -12019,7 +12017,9 @@ bool wallet2::get_tx_key(const crypto::hash &txid, crypto::secret_key &tx_key, s
std::string tx_data;
crypto::hash tx_prefix_hash{};
const auto& res_tx = res["txs"].front();
std::string tx_blob_hex = res_tx["pruned"].get<std::string>() + (res_tx["prunable"] ? res_tx["prunable"].get<std::string>() : ""s);
std::string tx_blob_hex = res_tx["pruned"].get<std::string>();
if (res_tx["prunable"])
tx_blob_hex.append(res_tx["prunable"].get<std::string_view>());
THROW_WALLET_EXCEPTION_IF(not oxenc::is_hex(tx_blob_hex), error::wallet_internal_error, "Failed to parse transaction from daemon");
tx_data = oxenc::from_hex(tx_blob_hex);
THROW_WALLET_EXCEPTION_IF(!cryptonote::parse_and_validate_tx_from_blob(tx_data, tx, tx_hash, tx_prefix_hash),