avoid some val copies

This commit is contained in:
Howard Chu 2016-03-03 21:10:28 +00:00
parent 5843f89364
commit 2abdb2c9fd
2 changed files with 21 additions and 21 deletions

View File

@ -666,7 +666,7 @@ void BlockchainLMDB::remove_transaction_data(const crypto::hash& tx_hash, const
LOG_PRINT_L3("BlockchainLMDB::" << __func__); LOG_PRINT_L3("BlockchainLMDB::" << __func__);
check_open(); check_open();
MDB_val_copy<crypto::hash> val_h(tx_hash); MDB_val val_h = {sizeof(tx_hash), (void *)&tx_hash};
MDB_val unused; MDB_val unused;
if (mdb_get(*m_write_txn, m_txs, &val_h, &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")); 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)) 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")); 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); auto result = mdb_del(*m_write_txn, m_tx_outputs, &val_h, NULL);
if (result == MDB_NOTFOUND) 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++; 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__); LOG_PRINT_L3("BlockchainLMDB::" << __func__);
mdb_txn_cursors *m_cursors = &m_wcursors; mdb_txn_cursors *m_cursors = &m_wcursors;
MDB_val_copy<crypto::hash> k(tx_hash);
MDB_val v; MDB_val v;
CURSOR(tx_outputs) 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) if (result == MDB_NOTFOUND)
{ {
LOG_PRINT_L2("tx has no outputs, so no global output indices"); 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_size_t num_elems = 0;
mdb_cursor_count(m_cur_tx_outputs, &num_elems); 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) for (uint64_t i = num_elems; i > 0; --i)
{ {
const tx_out tx_output = tx.vout[i-1]; 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) 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; 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__); LOG_PRINT_L3("BlockchainLMDB::" << __func__);
check_open(); check_open();
MDB_val_copy<uint64_t> k(out_index); auto result = mdb_del(*m_write_txn, m_output_indices, (MDB_val *)out_index, NULL);
auto result = mdb_del(*m_write_txn, m_output_indices, &k, NULL);
if (result == MDB_NOTFOUND) if (result == MDB_NOTFOUND)
{ {
LOG_PRINT_L0("Unexpected: global output index not found in m_output_indices"); 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())); 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) // if (result != 0 && result != MDB_NOTFOUND)
// throw1(DB_ERROR("Error adding removal of output tx hash to db transaction")); // throw1(DB_ERROR("Error adding removal of output tx hash to db transaction"));
if (result == MDB_NOTFOUND) 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())); 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) if (result == MDB_NOTFOUND)
{ {
LOG_PRINT_L0("Unexpected: global output index not found in m_output_keys"); 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--; 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__); LOG_PRINT_L3("BlockchainLMDB::" << __func__);
check_open(); 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); mdb_cursor_get(m_cur_output_amounts, &k, &v, MDB_LAST_DUP);
uint64_t amount_output_index = 0; uint64_t amount_output_index = 0;
uint64_t goi = 0;
bool found_index = false; bool found_index = false;
for (uint64_t i = num_elems; i > 0; --i) for (uint64_t i = num_elems; i > 0; --i)
{ {
mdb_cursor_get(m_cur_output_amounts, &k, &v, MDB_GET_CURRENT); mdb_cursor_get(m_cur_output_amounts, &k, &v, MDB_GET_CURRENT);
goi = *(const uint64_t *)v.mv_data; if (!memcmp(v.mv_data, global_output_index->mv_data, sizeof(uint64_t)))
if (goi == global_output_index)
{ {
amount_output_index = i-1; amount_output_index = i-1;
found_index = true; found_index = true;

View File

@ -302,10 +302,10 @@ private:
virtual void remove_output(const tx_out& tx_output); 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_output(const MDB_val *out_index, const uint64_t amount);
void remove_amount_output_index(const uint64_t amount, const uint64_t global_output_index); 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); virtual void add_spent_key(const crypto::key_image& k_image);