Skip to content

Commit

Permalink
Merge pull request #452 from evoskuil/master
Browse files Browse the repository at this point in the history
Implement conditional compile for block allocator.
  • Loading branch information
evoskuil authored Jan 30, 2025
2 parents 516682b + 4353287 commit 1833aa6
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 8 deletions.
5 changes: 4 additions & 1 deletion include/bitcoin/network/async/desubscriber.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,12 @@ class desubscriber final
/// Invoke each handler in order, with default arguments, then drop all.
void stop_default(const code& ec) NOEXCEPT;

/// The map size.
/// Subscriber map size.
size_t size() const NOEXCEPT;

/// True if no subscribers.
bool empty() const NOEXCEPT;

private:
// This is thread safe.
asio::strand& strand_;
Expand Down
5 changes: 4 additions & 1 deletion include/bitcoin/network/async/subscriber.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,12 @@ class subscriber final
/// Invoke each handler in order, with default arguments, then drop all.
void stop_default(const code& ec) NOEXCEPT;

/// The queue size.
/// Subscriber queue size.
size_t size() const NOEXCEPT;

/// True if no subscribers.
bool empty() const NOEXCEPT;

private:
// This is thread safe.
asio::strand& strand_;
Expand Down
5 changes: 4 additions & 1 deletion include/bitcoin/network/async/unsubscriber.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,12 @@ class unsubscriber final
/// Invoke each handler in order, with default arguments, then drop all.
void stop_default(const code& ec) NOEXCEPT;

/// The list size.
/// Subscriber list size.
size_t size() const NOEXCEPT;

/// True if no subscribers.
bool empty() const NOEXCEPT;

private:
// This is thread safe.
asio::strand& strand_;
Expand Down
8 changes: 8 additions & 0 deletions include/bitcoin/network/impl/async/desubscriber.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,14 @@ size() const NOEXCEPT
return map_.size();
}

template <typename Key, typename... Args>
bool desubscriber<Key, Args...>::
empty() const NOEXCEPT
{
BC_ASSERT_MSG(strand_.running_in_this_thread(), "strand");
return map_.empty();
}

} // namespace network
} // namespace libbitcoin

Expand Down
8 changes: 8 additions & 0 deletions include/bitcoin/network/impl/async/subscriber.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ size() const NOEXCEPT
return queue_.size();
}

template <typename... Args>
bool subscriber<Args...>::
empty() const NOEXCEPT
{
BC_ASSERT_MSG(strand_.running_in_this_thread(), "strand");
return queue_.empty();
}

} // namespace network
} // namespace libbitcoin

Expand Down
8 changes: 8 additions & 0 deletions include/bitcoin/network/impl/async/unsubscriber.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,14 @@ size() const NOEXCEPT
return queue_.size();
}

template <typename... Args>
bool unsubscriber<Args...>::
empty() const NOEXCEPT
{
BC_ASSERT_MSG(strand_.running_in_this_thread(), "strand");
return queue_.empty();
}

} // namespace network
} // namespace libbitcoin

Expand Down
2 changes: 1 addition & 1 deletion include/bitcoin/network/net/distributor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class BCT_API distributor
const system::data_chunk& data) NOEXCEPT
{
// Avoid deserialization if there are no subscribers for the type.
if (!is_zero(subscriber.size()))
if (!subscriber.empty())
{
// Subscribers are notified only with stop code or error::success.
const auto ptr = messages::deserialize<Message>(data, version);
Expand Down
2 changes: 2 additions & 0 deletions src/messages/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ typename block::cptr block::deserialize(arena& arena, uint32_t version,

// Set starting address of block allocation (nullptr if not detachable).
const auto memory = pointer_cast<uint8_t>(arena.start(data.size()));
if (is_null(memory))
return nullptr;

istream source{ data };
byte_reader reader{ source, &arena };
Expand Down
21 changes: 18 additions & 3 deletions src/net/distributor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,15 @@
#include <bitcoin/network/memory.hpp>
#include <bitcoin/network/messages/messages.hpp>

// Set false to use default block allocation.
constexpr bool use_block_allocator = true;

namespace libbitcoin {
namespace network {

// Compiler can't see is_null(arena).
BC_PUSH_WARNING(NO_UNGUARDED_POINTERS)

using namespace system;

#define SUBSCRIBER(name) name##_subscriber_
Expand Down Expand Up @@ -159,22 +165,31 @@ code distributor::do_notify<messages::block>(
distributor::block_subscriber& subscriber, uint32_t version,
const system::data_chunk& data) NOEXCEPT
{
if (!is_zero(subscriber.size()))
if (subscriber.empty())
return error::success;

if constexpr (use_block_allocator)
{
const auto arena = memory_.get_arena();
if (arena == nullptr)
if (is_null(arena))
return error::operation_failed;

const auto ptr = messages::block::deserialize(*arena, version, data);
if (!ptr)
return error::invalid_message;

subscriber.notify(error::success, ptr);
return error::success;
}
else
{
return do_notify<messages::block>(subscriber, version, data);
}

return error::success;
}

BC_POP_WARNING()

#undef SUBSCRIBER
#undef MAKE_SUBSCRIBER
#undef CASE_NOTIFY
Expand Down
3 changes: 3 additions & 0 deletions test/async/desubscriber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,19 @@ BOOST_AUTO_TEST_CASE(desubscriber__subscribe__stopped__subscriber_stopped)
std::pair<code, size_t> retry_result;
boost::asio::post(strand, [&]() NOEXCEPT
{
result &= instance.empty();
result &= is_zero(instance.size());
result &= !instance.subscribe([&](code value, size_t size) NOEXCEPT
{
stop_result = { value, size };
return true;
}, 0);

result &= !instance.empty();
result &= is_one(instance.size());
instance.stop(ec, expected);

result &= instance.empty();
result &= is_zero(instance.size());
result &= (instance.subscribe([&](code value, size_t size) NOEXCEPT
{
Expand Down
3 changes: 3 additions & 0 deletions test/async/subscriber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,18 @@ BOOST_AUTO_TEST_CASE(subscriber__subscribe__stopped__subscriber_stopped)
std::pair<code, size_t> retry_result;
boost::asio::post(strand, [&]() NOEXCEPT
{
result &= instance.empty();
result &= is_zero(instance.size());
result &= !instance.subscribe([&](code value, size_t size) NOEXCEPT
{
stop_result = { value, size };
});

result &= !instance.empty();
result &= is_one(instance.size());
instance.stop(ec, expected);

result &= instance.empty();
result &= is_zero(instance.size());
result &= (instance.subscribe([&](code value, size_t size) NOEXCEPT
{
Expand Down
7 changes: 6 additions & 1 deletion test/async/unsubscriber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@ BOOST_AUTO_TEST_CASE(unsubscriber__subscribe__stopped__subscriber_stopped)
stop_result = { value, size };
return true;
});


result &= !instance.empty();
result &= is_one(instance.size());
instance.stop(ec, expected);

result &= instance.empty();
result &= is_zero(instance.size());
result &= (instance.subscribe([&](code value, size_t size) NOEXCEPT
{
Expand Down Expand Up @@ -116,6 +118,7 @@ BOOST_AUTO_TEST_CASE(unsubscriber__subscribe__removed__expected)
std::pair<code, size_t> second_result;
boost::asio::post(strand, [&]() NOEXCEPT
{
result &= instance.empty();
result &= is_zero(instance.size());
result &= !instance.subscribe([&](code value, size_t size) NOEXCEPT
{
Expand All @@ -131,8 +134,10 @@ BOOST_AUTO_TEST_CASE(unsubscriber__subscribe__removed__expected)
return true;
});

result &= !instance.empty();
result &= is_one(instance.size());
instance.stop(ec2, expected2);
result &= instance.empty();
result &= is_zero(instance.size());
});

Expand Down

0 comments on commit 1833aa6

Please sign in to comment.