From 2abdb2c9fd834f5e12c085cf72aa24beb579dd02 Mon Sep 17 00:00:00 2001 From: Howard Chu Date: Thu, 3 Mar 2016 21:10:28 +0000 Subject: [PATCH] avoid some val copies --- src/blockchain_db/lmdb/db_lmdb.cpp | 36 +++++++++++++++--------------- src/blockchain_db/lmdb/db_lmdb.h | 6 ++--- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/blockchain_db/lmdb/db_lmdb.cpp b/src/blockchain_db/lmdb/db_lmdb.cpp index 69673215c..86a8282c8 100644 --- a/src/blockchain_db/lmdb/db_lmdb.cpp +++ b/src/blockchain_db/lmdb/db_lmdb.cpp @@ -666,7 +666,7 @@ void BlockchainLMDB::remove_transaction_data(const crypto::hash& tx_hash, const LOG_PRINT_L3("BlockchainLMDB::" << __func__); check_open(); - MDB_val_copy val_h(tx_hash); + MDB_val val_h = {sizeof(tx_hash), (void *)&tx_hash}; MDB_val unused; if (mdb_get(*m_write_txn, m_txs, &val_h, &unused)) throw1(TX_DNE("Attempting to remove transaction that isn't in the db")); @@ -678,7 +678,7 @@ void BlockchainLMDB::remove_transaction_data(const crypto::hash& tx_hash, const if (mdb_del(*m_write_txn, m_tx_heights, &val_h, NULL)) throw1(DB_ERROR("Failed to add removal of tx block height to db transaction")); - remove_tx_outputs(tx_hash, tx); + remove_tx_outputs(&val_h, tx); auto result = mdb_del(*m_write_txn, m_tx_outputs, &val_h, NULL); if (result == MDB_NOTFOUND) @@ -741,16 +741,15 @@ void BlockchainLMDB::add_output(const crypto::hash& tx_hash, const tx_out& tx_ou m_num_outputs++; } -void BlockchainLMDB::remove_tx_outputs(const crypto::hash& tx_hash, const transaction& tx) +void BlockchainLMDB::remove_tx_outputs(const MDB_val *tx_hash, const transaction& tx) { LOG_PRINT_L3("BlockchainLMDB::" << __func__); mdb_txn_cursors *m_cursors = &m_wcursors; - MDB_val_copy k(tx_hash); MDB_val v; CURSOR(tx_outputs) - auto result = mdb_cursor_get(m_cur_tx_outputs, &k, &v, MDB_SET); + auto result = mdb_cursor_get(m_cur_tx_outputs, (MDB_val *)tx_hash, &v, MDB_SET); if (result == MDB_NOTFOUND) { LOG_PRINT_L2("tx has no outputs, so no global output indices"); @@ -764,15 +763,20 @@ void BlockchainLMDB::remove_tx_outputs(const crypto::hash& tx_hash, const transa mdb_size_t num_elems = 0; mdb_cursor_count(m_cur_tx_outputs, &num_elems); - mdb_cursor_get(m_cur_tx_outputs, &k, &v, MDB_LAST_DUP); + mdb_cursor_get(m_cur_tx_outputs, (MDB_val *)tx_hash, &v, MDB_LAST_DUP); for (uint64_t i = num_elems; i > 0; --i) { const tx_out tx_output = tx.vout[i-1]; - remove_output(*(const uint64_t*)v.mv_data, tx_output.amount); +#ifndef MISALIGNED_OK + uint64_t tv; + memcpy(&tv, v.mv_data, sizeof(uint64_t)); + v.mv_data = &tv; +#endif + remove_output((const MDB_val *)&v, tx_output.amount); if (i > 1) { - mdb_cursor_get(m_cur_tx_outputs, &k, &v, MDB_PREV_DUP); + mdb_cursor_get(m_cur_tx_outputs, (MDB_val *)tx_hash, &v, MDB_PREV_DUP); } } } @@ -785,14 +789,12 @@ void BlockchainLMDB::remove_output(const tx_out& tx_output) return; } -void BlockchainLMDB::remove_output(const uint64_t& out_index, const uint64_t amount) +void BlockchainLMDB::remove_output(const MDB_val *out_index, const uint64_t amount) { LOG_PRINT_L3("BlockchainLMDB::" << __func__); check_open(); - MDB_val_copy k(out_index); - - auto result = mdb_del(*m_write_txn, m_output_indices, &k, NULL); + auto result = mdb_del(*m_write_txn, m_output_indices, (MDB_val *)out_index, NULL); if (result == MDB_NOTFOUND) { LOG_PRINT_L0("Unexpected: global output index not found in m_output_indices"); @@ -802,7 +804,7 @@ void BlockchainLMDB::remove_output(const uint64_t& out_index, const uint64_t amo throw1(DB_ERROR(lmdb_error("Error adding removal of output tx index to db transaction", result).c_str())); } - result = mdb_del(*m_write_txn, m_output_txs, &k, NULL); + result = mdb_del(*m_write_txn, m_output_txs, (MDB_val *)out_index, NULL); // if (result != 0 && result != MDB_NOTFOUND) // throw1(DB_ERROR("Error adding removal of output tx hash to db transaction")); if (result == MDB_NOTFOUND) @@ -814,7 +816,7 @@ void BlockchainLMDB::remove_output(const uint64_t& out_index, const uint64_t amo throw1(DB_ERROR(lmdb_error("Error adding removal of output tx hash to db transaction", result).c_str())); } - result = mdb_del(*m_write_txn, m_output_keys, &k, NULL); + result = mdb_del(*m_write_txn, m_output_keys, (MDB_val *)out_index, NULL); if (result == MDB_NOTFOUND) { LOG_PRINT_L0("Unexpected: global output index not found in m_output_keys"); @@ -827,7 +829,7 @@ void BlockchainLMDB::remove_output(const uint64_t& out_index, const uint64_t amo m_num_outputs--; } -void BlockchainLMDB::remove_amount_output_index(const uint64_t amount, const uint64_t global_output_index) +void BlockchainLMDB::remove_amount_output_index(const uint64_t amount, const MDB_val *global_output_index) { LOG_PRINT_L3("BlockchainLMDB::" << __func__); check_open(); @@ -849,13 +851,11 @@ void BlockchainLMDB::remove_amount_output_index(const uint64_t amount, const uin mdb_cursor_get(m_cur_output_amounts, &k, &v, MDB_LAST_DUP); uint64_t amount_output_index = 0; - uint64_t goi = 0; bool found_index = false; for (uint64_t i = num_elems; i > 0; --i) { mdb_cursor_get(m_cur_output_amounts, &k, &v, MDB_GET_CURRENT); - goi = *(const uint64_t *)v.mv_data; - if (goi == global_output_index) + if (!memcmp(v.mv_data, global_output_index->mv_data, sizeof(uint64_t))) { amount_output_index = i-1; found_index = true; diff --git a/src/blockchain_db/lmdb/db_lmdb.h b/src/blockchain_db/lmdb/db_lmdb.h index e58643efa..06d583f75 100644 --- a/src/blockchain_db/lmdb/db_lmdb.h +++ b/src/blockchain_db/lmdb/db_lmdb.h @@ -302,10 +302,10 @@ private: virtual void remove_output(const tx_out& tx_output); - void remove_tx_outputs(const crypto::hash& tx_hash, const transaction& tx); + void remove_tx_outputs(const MDB_val *tx_hash, const transaction& tx); - void remove_output(const uint64_t& out_index, const uint64_t amount); - void remove_amount_output_index(const uint64_t amount, const uint64_t global_output_index); + void remove_output(const MDB_val *out_index, const uint64_t amount); + void remove_amount_output_index(const uint64_t amount, const MDB_val *global_output_index); virtual void add_spent_key(const crypto::key_image& k_image);