From d003f07c59933725b7e35ca758bbfdb2b11b4ea3 Mon Sep 17 00:00:00 2001 From: moneromooo-monero Date: Thu, 22 Mar 2018 17:51:58 +0000 Subject: [PATCH] speedup get_output_histogram for all amounts when min_count > 0 This skips the vast majority of "dust" output amounts with just one instance on the chain. Clocks in at 0.15% of the original time on testnet. --- src/blockchain_db/blockchain_db.h | 3 ++- src/blockchain_db/lmdb/db_lmdb.cpp | 11 +++++++---- src/blockchain_db/lmdb/db_lmdb.h | 3 ++- src/cryptonote_core/blockchain.cpp | 4 ++-- src/cryptonote_core/blockchain.h | 3 ++- src/rpc/core_rpc_server.cpp | 2 +- 6 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/blockchain_db/blockchain_db.h b/src/blockchain_db/blockchain_db.h index d7230f644..1a23a9ada 100644 --- a/src/blockchain_db/blockchain_db.h +++ b/src/blockchain_db/blockchain_db.h @@ -1490,10 +1490,11 @@ public: * @param amounts optional set of amounts to lookup * @param unlocked whether to restrict count to unlocked outputs * @param recent_cutoff timestamp to determine whether an output is recent + * @param min_count return only amounts with at least that many instances * * @return a set of amount/instances */ - virtual std::map> get_output_histogram(const std::vector &amounts, bool unlocked, uint64_t recent_cutoff) const = 0; + virtual std::map> get_output_histogram(const std::vector &amounts, bool unlocked, uint64_t recent_cutoff, uint64_t min_count) const = 0; /** * @brief is BlockchainDB in read-only mode? diff --git a/src/blockchain_db/lmdb/db_lmdb.cpp b/src/blockchain_db/lmdb/db_lmdb.cpp index 9a755fb0c..5672a34f6 100644 --- a/src/blockchain_db/lmdb/db_lmdb.cpp +++ b/src/blockchain_db/lmdb/db_lmdb.cpp @@ -3086,7 +3086,7 @@ void BlockchainLMDB::get_output_tx_and_index(const uint64_t& amount, const std:: LOG_PRINT_L3("db3: " << db3); } -std::map> BlockchainLMDB::get_output_histogram(const std::vector &amounts, bool unlocked, uint64_t recent_cutoff) const +std::map> BlockchainLMDB::get_output_histogram(const std::vector &amounts, bool unlocked, uint64_t recent_cutoff, uint64_t min_count) const { LOG_PRINT_L3("BlockchainLMDB::" << __func__); check_open(); @@ -3112,7 +3112,8 @@ std::map> BlockchainLMDB::get mdb_size_t num_elems = 0; mdb_cursor_count(m_cur_output_amounts, &num_elems); uint64_t amount = *(const uint64_t*)k.mv_data; - histogram[amount] = std::make_tuple(num_elems, 0, 0); + if (num_elems >= min_count) + histogram[amount] = std::make_tuple(num_elems, 0, 0); } } else @@ -3123,13 +3124,15 @@ std::map> BlockchainLMDB::get int ret = mdb_cursor_get(m_cur_output_amounts, &k, &v, MDB_SET); if (ret == MDB_NOTFOUND) { - histogram[amount] = std::make_tuple(0, 0, 0); + if (0 >= min_count) + histogram[amount] = std::make_tuple(0, 0, 0); } else if (ret == MDB_SUCCESS) { mdb_size_t num_elems = 0; mdb_cursor_count(m_cur_output_amounts, &num_elems); - histogram[amount] = std::make_tuple(num_elems, 0, 0); + if (num_elems >= min_count) + histogram[amount] = std::make_tuple(num_elems, 0, 0); } else { diff --git a/src/blockchain_db/lmdb/db_lmdb.h b/src/blockchain_db/lmdb/db_lmdb.h index 0aa4bb86e..9a20c0f1e 100644 --- a/src/blockchain_db/lmdb/db_lmdb.h +++ b/src/blockchain_db/lmdb/db_lmdb.h @@ -287,10 +287,11 @@ public: * @param amounts optional set of amounts to lookup * @param unlocked whether to restrict count to unlocked outputs * @param recent_cutoff timestamp to determine which outputs are recent + * @param min_count return only amounts with at least that many instances * * @return a set of amount/instances */ - std::map> get_output_histogram(const std::vector &amounts, bool unlocked, uint64_t recent_cutoff) const; + std::map> get_output_histogram(const std::vector &amounts, bool unlocked, uint64_t recent_cutoff, uint64_t min_count) const; private: void do_resize(uint64_t size_increase=0); diff --git a/src/cryptonote_core/blockchain.cpp b/src/cryptonote_core/blockchain.cpp index f02a1f8d6..25e97f71f 100644 --- a/src/cryptonote_core/blockchain.cpp +++ b/src/cryptonote_core/blockchain.cpp @@ -4322,9 +4322,9 @@ uint64_t Blockchain::get_difficulty_target() const return get_current_hard_fork_version() < 2 ? DIFFICULTY_TARGET_V1 : DIFFICULTY_TARGET_V2; } -std::map> Blockchain:: get_output_histogram(const std::vector &amounts, bool unlocked, uint64_t recent_cutoff) const +std::map> Blockchain:: get_output_histogram(const std::vector &amounts, bool unlocked, uint64_t recent_cutoff, uint64_t min_count) const { - return m_db->get_output_histogram(amounts, unlocked, recent_cutoff); + return m_db->get_output_histogram(amounts, unlocked, recent_cutoff, min_count); } std::list> Blockchain::get_alternative_chains() const diff --git a/src/cryptonote_core/blockchain.h b/src/cryptonote_core/blockchain.h index 4423199de..61096fb26 100644 --- a/src/cryptonote_core/blockchain.h +++ b/src/cryptonote_core/blockchain.h @@ -827,10 +827,11 @@ namespace cryptonote * @param amounts optional set of amounts to lookup * @param unlocked whether to restrict instances to unlocked ones * @param recent_cutoff timestamp to consider outputs as recent + * @param min_count return only amounts with at least that many instances * * @return a set of amount/instances */ - std::map> get_output_histogram(const std::vector &amounts, bool unlocked, uint64_t recent_cutoff) const; + std::map> get_output_histogram(const std::vector &amounts, bool unlocked, uint64_t recent_cutoff, uint64_t min_count = 0) const; /** * @brief perform a check on all key images in the blockchain diff --git a/src/rpc/core_rpc_server.cpp b/src/rpc/core_rpc_server.cpp index 79792e387..827d97da2 100644 --- a/src/rpc/core_rpc_server.cpp +++ b/src/rpc/core_rpc_server.cpp @@ -1728,7 +1728,7 @@ namespace cryptonote std::map> histogram; try { - histogram = m_core.get_blockchain_storage().get_output_histogram(req.amounts, req.unlocked, req.recent_cutoff); + histogram = m_core.get_blockchain_storage().get_output_histogram(req.amounts, req.unlocked, req.recent_cutoff, req.min_count); } catch (const std::exception &e) {