Skip to content

Commit

Permalink
Merge pull request #505 from evoskuil/master
Browse files Browse the repository at this point in the history
Expose and use hashmap::get(ptr, ...) for to_spend_set().
  • Loading branch information
evoskuil committed Jun 27, 2024
2 parents 85e0576 + 489fabf commit 81e1a31
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 42 deletions.
10 changes: 10 additions & 0 deletions include/bitcoin/database/impl/primitives/hashmap.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,16 @@ bool CLASS::get(const iterator& it, const Link& link,
return read(it.get(), link, element);
}

// static
TEMPLATE
template <typename Element, if_equal<Element::size, Size>>
bool CLASS::get(const memory_ptr& ptr, const Link& link,
Element& element) NOEXCEPT
{
// This override avoids deadlock when holding iterator to the same table.
return read(ptr, link, element);
}

TEMPLATE
template <typename Element, if_equal<Element::size, Size>>
bool CLASS::set(const Link& link, const Element& element) NOEXCEPT
Expand Down
2 changes: 1 addition & 1 deletion include/bitcoin/database/impl/query/archive.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -1065,7 +1065,7 @@ code CLASS::set_code(txs_link& out_fk, const transactions& txs,

// Clean allocation failure (e.g. disk full), see set_strong() comments.
// Transactor assures cannot be restored without txs, as required to unset.
if (strong && !set_strong(bc::seq, key, links, positive))
if (strong && !set_strong(key, links, positive))
return error::txs_confirm;

// Header link is the key for the txs table.
Expand Down
41 changes: 5 additions & 36 deletions include/bitcoin/database/impl/query/confirm.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -550,8 +550,8 @@ bool CLASS::is_strong_block(const header_link& link) const NOEXCEPT

// protected
TEMPLATE
bool CLASS::set_strong(const auto& execution, const header_link& link,
const tx_links& txs, bool positive) NOEXCEPT
bool CLASS::set_strong(const header_link& link, const tx_links& txs,
bool positive) NOEXCEPT
{
const auto set = [this, &link, positive](const tx_link& tx) NOEXCEPT
{
Expand All @@ -564,8 +564,7 @@ bool CLASS::set_strong(const auto& execution, const header_link& link,
});
};

// C++17 incomplete on GCC/CLang, so presently parallel only on MSVC++.
return std_all_of(execution, txs.begin(), txs.end(), set);
return std::all_of(txs.begin(), txs.end(), set);
}

TEMPLATE
Expand All @@ -579,7 +578,7 @@ bool CLASS::set_strong(const header_link& link) NOEXCEPT
const auto scope = store_.get_transactor();

// Clean allocation failure (e.g. disk full).
return set_strong(bc::seq, link, txs, true);
return set_strong(link, txs, true);
// ========================================================================
}

Expand All @@ -594,37 +593,7 @@ bool CLASS::set_unstrong(const header_link& link) NOEXCEPT
const auto scope = store_.get_transactor();

// Clean allocation failure (e.g. disk full).
return set_strong(bc::seq, link, txs, false);
// ========================================================================
}

TEMPLATE
bool CLASS::set_strong_parallel(const header_link& link) NOEXCEPT
{
const auto txs = to_transactions(link);
if (txs.empty())
return false;

// ========================================================================
const auto scope = store_.get_transactor();

// Clean allocation failure (e.g. disk full).
return set_strong(bc::par_unseq, link, txs, true);
// ========================================================================
}

TEMPLATE
bool CLASS::set_unstrong_parallel(const header_link& link) NOEXCEPT
{
const auto txs = to_transactions(link);
if (txs.empty())
return false;

// ========================================================================
const auto scope = store_.get_transactor();

// Clean allocation failure (e.g. disk full).
return set_strong(bc::par_unseq, link, txs, false);
return set_strong(link, txs, false);
// ========================================================================
}

Expand Down
6 changes: 5 additions & 1 deletion include/bitcoin/database/impl/query/translate.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -480,9 +480,13 @@ spend_set CLASS::to_spend_set(const tx_link& link) const NOEXCEPT

// This is not concurrent because to_spend_sets is (by tx).
table::spend::get_prevout_sequence get{};

// This reduced a no-bypass 840k sync/confirmable/confirm run by 8.3%.
const auto ptr = store_.spend.get_memory();

for (const auto& spend_fk: puts.spend_fks)
{
if (!store_.spend.get(spend_fk, get))
if (!store_.spend.get(ptr, spend_fk, get))
return {};

// Translate query to public struct.
Expand Down
5 changes: 5 additions & 0 deletions include/bitcoin/database/primitives/hashmap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ class hashmap
static bool get(const iterator& it, const Link& link,
Element& element) NOEXCEPT;

/// Get element at link using memory object, false if deserialize error.
template <typename Element, if_equal<Element::size, Size> = true>
static bool get(const memory_ptr& ptr, const Link& link,
Element& element) NOEXCEPT;

/// Set element into previously allocated link (follow with commit).
template <typename Element, if_equal<Element::size, Size> = true>
bool set(const Link& link, const Element& element) NOEXCEPT;
Expand Down
6 changes: 2 additions & 4 deletions include/bitcoin/database/query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -487,8 +487,6 @@ class query
/// Block association relies on strong (confirmed or pending).
bool set_strong(const header_link& link) NOEXCEPT;
bool set_unstrong(const header_link& link) NOEXCEPT;
bool set_strong_parallel(const header_link& link) NOEXCEPT;
bool set_unstrong_parallel(const header_link& link) NOEXCEPT;
code block_confirmable(const header_link& link) const NOEXCEPT;
code tx_confirmable(const tx_link& link, const context& ctx) const NOEXCEPT;
code unspent_duplicates(const tx_link& coinbase,
Expand Down Expand Up @@ -565,8 +563,8 @@ class query
error::error_t unspendable_prevout(const point_link& link,
uint32_t sequence, uint32_t version,
const context& ctx) const NOEXCEPT;
bool set_strong(const auto& execution, const header_link& link,
const tx_links& txs, bool positive) NOEXCEPT;
bool set_strong(const header_link& link, const tx_links& txs,
bool positive) NOEXCEPT;

/// context
/// -----------------------------------------------------------------------
Expand Down

0 comments on commit 81e1a31

Please sign in to comment.