From ed2c81ed95be71bace897e62a0c241068cfde7be Mon Sep 17 00:00:00 2001 From: moneromooo-monero Date: Mon, 16 Apr 2018 00:16:02 +0100 Subject: replace std::list with std::vector on some hot paths also use reserve where appropriate --- src/rpc/core_rpc_server.cpp | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) (limited to 'src/rpc/core_rpc_server.cpp') diff --git a/src/rpc/core_rpc_server.cpp b/src/rpc/core_rpc_server.cpp index 83cf5ba3c..b921e1438 100644 --- a/src/rpc/core_rpc_server.cpp +++ b/src/rpc/core_rpc_server.cpp @@ -226,7 +226,7 @@ namespace cryptonote if (use_bootstrap_daemon_if_necessary(invoke_http_mode::BIN, "/getblocks.bin", req, res, r)) return r; - std::list > > bs; + std::vector > > bs; if(!m_core.find_blockchain_supplement(req.start_height, req.block_ids, bs, res.current_height, res.start_height, req.prune, COMMAND_RPC_GET_BLOCKS_FAST_MAX_COUNT)) { @@ -235,6 +235,8 @@ namespace cryptonote } size_t pruned_size = 0, unpruned_size = 0, ntxes = 0; + res.blocks.reserve(bs.size()); + res.output_indices.reserve(bs.size()); for(auto& bd: bs) { res.blocks.resize(res.blocks.size()+1); @@ -266,7 +268,9 @@ namespace cryptonote } size_t txidx = 0; ntxes += bd.second.size(); - for (std::list::iterator i = bd.second.begin(); i != bd.second.end(); ++i) + res.blocks.back().txs.reserve(bd.second.size()); + res.output_indices.back().indices.reserve(bd.second.size()); + for (std::vector::iterator i = bd.second.begin(); i != bd.second.end(); ++i) { unpruned_size += i->size(); res.blocks.back().txs.push_back(std::move(*i)); @@ -295,7 +299,7 @@ namespace cryptonote if (use_bootstrap_daemon_if_necessary(invoke_http_mode::JON, "/get_alt_blocks_hashes", req, res, r)) return r; - std::list blks; + std::vector blks; if(!m_core.get_alternative_blocks(blks)) { @@ -337,8 +341,8 @@ namespace cryptonote res.status = "Error retrieving block at height " + std::to_string(height); return true; } - std::list txs; - std::list missed_txs; + std::vector txs; + std::vector missed_txs; m_core.get_transactions(blk.tx_hashes, txs, missed_txs); res.blocks.resize(res.blocks.size() + 1); res.blocks.back().block = block_to_blob(blk); @@ -553,8 +557,8 @@ namespace cryptonote } vh.push_back(*reinterpret_cast(b.data())); } - std::list missed_txs; - std::list txs; + std::vector missed_txs; + std::vector txs; bool r = m_core.get_transactions(vh, txs, missed_txs); if(!r) { @@ -575,25 +579,26 @@ namespace cryptonote if(r) { // sort to match original request - std::list sorted_txs; + std::vector sorted_txs; std::vector::const_iterator i; + unsigned txs_processed = 0; for (const crypto::hash &h: vh) { if (std::find(missed_txs.begin(), missed_txs.end(), h) == missed_txs.end()) { - if (txs.empty()) + if (txs.size() == txs_processed) { res.status = "Failed: internal error - txs is empty"; return true; } // core returns the ones it finds in the right order - if (get_transaction_hash(txs.front()) != h) + if (get_transaction_hash(txs[txs_processed]) != h) { res.status = "Failed: tx hash mismatch"; return true; } - sorted_txs.push_back(std::move(txs.front())); - txs.pop_front(); + sorted_txs.push_back(std::move(txs[txs_processed])); + ++txs_processed; } else if ((i = std::find_if(pool_tx_info.begin(), pool_tx_info.end(), [h](const tx_info &txi) { return epee::string_tools::pod_to_hex(h) == txi.id_hash; })) != pool_tx_info.end()) { @@ -604,7 +609,7 @@ namespace cryptonote return true; } sorted_txs.push_back(tx); - missed_txs.remove(h); + missed_txs.erase(std::find(missed_txs.begin(), missed_txs.end(), h)); pool_tx_hashes.insert(h); const std::string hash_string = epee::string_tools::pod_to_hex(h); for (const auto &ti: pool_tx_info) @@ -623,7 +628,7 @@ namespace cryptonote LOG_PRINT_L2("Found " << found_in_pool << "/" << vh.size() << " transactions in the pool"); } - std::list::const_iterator txhi = req.txs_hashes.begin(); + std::vector::const_iterator txhi = req.txs_hashes.begin(); std::vector::const_iterator vhi = vh.begin(); for(auto& tx: txs) { @@ -1664,10 +1669,10 @@ namespace cryptonote PERF_TIMER(on_flush_txpool); bool failed = false; - std::list txids; + std::vector txids; if (req.txids.empty()) { - std::list pool_txs; + std::vector pool_txs; bool r = m_core.get_pool_transactions(pool_txs); if (!r) { -- cgit v1.2.3