From b28ccc9ad6649f696d2b4a260afa6ae273310d07 Mon Sep 17 00:00:00 2001 From: R Date: Mon, 26 Oct 2026 11:41:58 -0400 Subject: [PATCH 1/4] trystero: AGENTS.md sends the reader to the superproject's coding guidelines The superproject's rules and design decisions moved to CODING-GUIDELINES.md, whose chapter 15 gives this library's decisions with their reasons; its AGENTS.md says how to work in the tree. Both are read before this file. --- AGENTS.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/AGENTS.md b/AGENTS.md index 451da68..c721dc3 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -1,6 +1,9 @@ # Working on trystero webcpp's rules apply here: read the superproject's +[CODING-GUIDELINES.md](../../CODING-GUIDELINES.md) +(), whose +chapter 15 gives trystero's design decisions with their reasons, and its [AGENTS.md](../../AGENTS.md) () first. This file holds only what is specific to trystero. From 0ef164171f149b60bc23a39fc22bf2c7201e9d63 Mon Sep 17 00:00:00 2001 From: R Date: Tue, 27 Oct 2026 02:38:39 -0400 Subject: [PATCH 2/4] trystero: app_impl's and room_impl's sink parameters are exempt, as app's are Analysed against libstdc++, as the CI's Linux lint does, clang-tidy's performance-unnecessary-value-param reports pc and channel of share_or_adopt and app of room_impl::make as used only as const references, although each is moved into the object std::allocate_shared builds; against libc++ it reports nothing. They are sinks, exempt with that reason as app::make's parameters already are. --- include/webcpp/trystero/detail/app_impl.hpp | 2 ++ include/webcpp/trystero/room.hpp | 2 ++ 2 files changed, 4 insertions(+) diff --git a/include/webcpp/trystero/detail/app_impl.hpp b/include/webcpp/trystero/detail/app_impl.hpp index d4a99bd..f59e6d2 100644 --- a/include/webcpp/trystero/detail/app_impl.hpp +++ b/include/webcpp/trystero/detail/app_impl.hpp @@ -309,9 +309,11 @@ class app_impl : public std::enable_shared_from_this { }; /** Closes the new connection and returns the live one to `peer`, if any; else adopts it. */ + // NOLINTBEGIN(performance-unnecessary-value-param): sinks, moved on std::shared_ptr share_or_adopt(std::string_view peer, std::shared_ptr pc, std::shared_ptr channel) { + // NOLINTEND(performance-unnecessary-value-param) if (auto existing = find(peer)) { channel->set_handlers({}); pc->set_handlers({}); diff --git a/include/webcpp/trystero/room.hpp b/include/webcpp/trystero/room.hpp index a7f96b2..eb9c62d 100644 --- a/include/webcpp/trystero/room.hpp +++ b/include/webcpp/trystero/room.hpp @@ -482,8 +482,10 @@ class room_impl final : public std::enable_shared_from_this, public r room_impl& operator=(const room_impl&) = delete; /** A room of `app`, in its chosen resource, joined: `start` has run. */ + // NOLINTBEGIN(performance-unnecessary-value-param): a sink, moved on static std::shared_ptr make(std::shared_ptr app, room_options options, std::string_view room_id) { + // NOLINTEND(performance-unnecessary-value-param) std::pmr::memory_resource* const memory = options.memory != nullptr ? options.memory : app->memory(); auto impl = std::allocate_shared(std::pmr::polymorphic_allocator<>(memory), From 9120af639dcad75dfb88c0e2c7cd68d58e3f0dc4 Mon Sep 17 00:00:00 2001 From: R Date: Tue, 27 Oct 2026 03:43:15 -0400 Subject: [PATCH 3/4] trystero: the allocation test names what an unexpected global count saw On MSVC 14.3 and 14.5 a room counts 6 global allocations where 2 are expected, and two rooms of an app 12 where 4 are, which the test reported as numbers alone. It now keeps the size of each global allocation it counts, without allocating, and writes them to the standard error with MSVC's iterator debugging level when a count is not what it expects, so that a failure says what was allocated. --- test/allocation_test.cpp | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/test/allocation_test.cpp b/test/allocation_test.cpp index 5eb1ae8..3f9a13c 100644 --- a/test/allocation_test.cpp +++ b/test/allocation_test.cpp @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include @@ -59,6 +60,10 @@ namespace { // nothing else. // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) std::size_t global_allocations = 0; +// The sizes of the first global allocations counted, kept without allocating, so that a count +// the test did not expect names what it saw. +// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) +std::array global_sizes{}; // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) std::size_t c_allocations = 0; // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) @@ -307,6 +312,9 @@ void release(void* pointer) noexcept { */ void* allocate(std::size_t size, std::size_t alignment) { if (on_counted_thread() && counting) { + if (global_allocations < global_sizes.size()) { + global_sizes.at(global_allocations) = size; + } ++global_allocations; } alignment = alignment < sizeof(void*) ? sizeof(void*) : alignment; @@ -319,6 +327,26 @@ void* allocate(std::size_t size, std::size_t alignment) { return pointer; } +/** + * Writes the size of each global allocation counted to the standard error when they are not + * `expected` in number, and, under MSVC, the iterator debugging level, whose debug runtime gives + * a standard container a proxy it allocates even when empty: a failed count then says what it saw. + */ +void explain_global_allocations(std::size_t expected) { + if (global_allocations == expected) { + return; + } + std::cerr << "global allocations: " << global_allocations << ", where " << expected + << " were expected; their sizes:"; + for (std::size_t i = 0; i < global_allocations && i < global_sizes.size(); ++i) { + std::cerr << ' ' << global_sizes.at(i); + } +#ifdef _ITERATOR_DEBUG_LEVEL + std::cerr << " (_ITERATOR_DEBUG_LEVEL " << _ITERATOR_DEBUG_LEVEL << ')'; +#endif + std::cerr << '\n'; +} + /** * A resource that counts what it gives, from a buffer made before the exchange, and what is * given back: how many bytes are still out, and how many returns name memory it never gave, @@ -399,6 +427,7 @@ void the_counts_see_an_allocation() { void* pointer = ::operator new(40); counting = false; ::operator delete(pointer); + explain_global_allocations(1U); BOOST_TEST_EQ(global_allocations, 1U); #ifdef WEBCPP_TEST_TRYSTERO_COUNTS_C_ALLOCATIONS // The C library's allocation, on purpose. @@ -486,6 +515,7 @@ void exchange_allocates_through_the_rooms_resources(Make make) { const bool arrived = run_until(io, [&] { return received == 2; }); counting = false; BOOST_TEST(arrived); + explain_global_allocations(0U); BOOST_TEST_EQ(global_allocations, 0U); BOOST_TEST_EQ(c_allocations, 0U); BOOST_TEST_GT(a_memory.allocations + z_memory.allocations, before); @@ -641,6 +671,7 @@ void a_room_allocates_through_its_resource_from_join_to_destruction() { BOOST_TEST(farewell); BOOST_TEST_GT(a_lines, 0U); BOOST_TEST_GT(z_lines, 0U); + explain_global_allocations(2U); BOOST_TEST_EQ(global_allocations, 2U); BOOST_TEST_EQ(c_allocations, 0U); BOOST_TEST_GT(network_memory.allocations, 0U); @@ -723,6 +754,7 @@ void rooms_of_one_app_allocate_through_their_resources() { counting = false; trystero::pmr_bytes(&a_memory).swap(to_shared); trystero::pmr_bytes(&a_room_memory).swap(to_own); + explain_global_allocations(4U); BOOST_TEST_EQ(global_allocations, 4U); // the four rooms' JSON storage counters BOOST_TEST_EQ(c_allocations, 0U); for (const auto* m : {&a_memory, &a_room_memory, &z_memory, &z_room_memory}) { From 10c8866513e0fe6f2266cd3d18ed38861f7c9354 Mon Sep 17 00:00:00 2001 From: R Date: Tue, 27 Oct 2026 04:16:40 -0400 Subject: [PATCH 4/4] trystero: the test backend's queue and the allocation test's objects keep to their resources MSVC counted, for each room, a global allocation of 16 bytes and one of 56: its std::list allocates a sentinel node when constructed, and in a debug build every container allocates an iterator proxy, and the memory backend's data channel queued its frames in a std::list of std::allocator. The queue now takes its nodes from the channel's resource, through an allocator that leaves each frame in the resource it was made in. The second test also checked its resources while its moved-from configurations and its payloads were alive, of which MSVC's debug containers still hold proxies: they are now destroyed before the check. --- test/allocation_test.cpp | 114 +++++++++++++++++--------------- test/support/memory_backend.hpp | 39 ++++++++++- 2 files changed, 95 insertions(+), 58 deletions(-) diff --git a/test/allocation_test.cpp b/test/allocation_test.cpp index 3f9a13c..57f3ddb 100644 --- a/test/allocation_test.cpp +++ b/test/allocation_test.cpp @@ -694,66 +694,70 @@ void rooms_of_one_app_allocate_through_their_resources() { counting_resource z_memory; counting_resource z_room_memory; std::size_t lines = 0; - // Construct configurations in their resources before the counting window. - auto a_config = app_config_in(a_memory, "AAAAAAAAAAAAAAAAAAAA", "wss://relay.test", lines); - auto z_config = app_config_in(z_memory, "zzzzzzzzzzzzzzzzzzzz", "wss://relay.test", lines); - const auto a_own = options_in(a_room_memory); - const auto z_own = options_in(z_room_memory); - // An inline payload of a size no other allocation has: its frame is at most 256 bytes longer. - constexpr std::size_t payload_size = 12'345; - trystero::pmr_bytes to_shared(payload_size, std::uint8_t{1}, &a_memory); - trystero::pmr_bytes to_own(payload_size, std::uint8_t{2}, &a_room_memory); { - boost::asio::io_context io; // after the resources: destroyed before them - trystero::test::memory_network network{io, 1, &network_memory}; - network.relay("wss://relay.test"); - warm(io); - counting = true; + // Construct configurations in their resources before the counting window. They, and the + // payloads, are destroyed before the resources are checked: a moved-from object may still + // hold memory of its resource (MSVC's debug containers keep an iterator proxy). + auto a_config = app_config_in(a_memory, "AAAAAAAAAAAAAAAAAAAA", "wss://relay.test", lines); + auto z_config = app_config_in(z_memory, "zzzzzzzzzzzzzzzzzzzz", "wss://relay.test", lines); + const auto a_own = options_in(a_room_memory); + const auto z_own = options_in(z_room_memory); + // An inline payload of a size no other allocation has: its frame is at most 256 bytes + // longer. + constexpr std::size_t payload_size = 12'345; + const trystero::pmr_bytes to_shared(payload_size, std::uint8_t{1}, &a_memory); + const trystero::pmr_bytes to_own(payload_size, std::uint8_t{2}, &a_room_memory); { - trystero::app a(io.get_executor(), network, std::move(a_config)); - trystero::app z(io.get_executor(), network, std::move(z_config)); - auto a1 = join(a, "shared", options_in(a_memory)); - auto a2 = join(a, "own", a_own); - auto z1 = join(z, "shared", options_in(z_memory)); - auto z2 = join(z, "own", z_own); - std::size_t got = 0; - auto in_shared = z1.make_action("b"); - auto in_own = z2.make_action("b"); - in_shared.on_message( - [&](const trystero::pmr_bytes& data, const trystero::message_info&) { - got += data.get_allocator().resource() == &z_memory ? 1 : 0; - }); - in_own.on_message([&](const trystero::pmr_bytes& data, const trystero::message_info&) { - got += data.get_allocator().resource() == &z_room_memory ? 1 : 0; - }); - BOOST_TEST(run_until(io, [&] { - return a1.peers().size() == 1 && a2.peers().size() == 1 && z1.peers().size() == 1 && - z2.peers().size() == 1; - })); - for (auto* m : {&z_memory, &z_room_memory}) { - m->sized_from = payload_size; - m->sized_to = payload_size + 256; + boost::asio::io_context io; // after the resources: destroyed before them + trystero::test::memory_network network{io, 1, &network_memory}; + network.relay("wss://relay.test"); + warm(io); + counting = true; + { + trystero::app a(io.get_executor(), network, std::move(a_config)); + trystero::app z(io.get_executor(), network, std::move(z_config)); + auto a1 = join(a, "shared", options_in(a_memory)); + auto a2 = join(a, "own", a_own); + auto z1 = join(z, "shared", options_in(z_memory)); + auto z2 = join(z, "own", z_own); + std::size_t got = 0; + auto in_shared = z1.make_action("b"); + auto in_own = z2.make_action("b"); + in_shared.on_message( + [&](const trystero::pmr_bytes& data, const trystero::message_info&) { + got += data.get_allocator().resource() == &z_memory ? 1 : 0; + }); + in_own.on_message( + [&](const trystero::pmr_bytes& data, const trystero::message_info&) { + got += data.get_allocator().resource() == &z_room_memory ? 1 : 0; + }); + BOOST_TEST(run_until(io, [&] { + return a1.peers().size() == 1 && a2.peers().size() == 1 && + z1.peers().size() == 1 && z2.peers().size() == 1; + })); + for (auto* m : {&z_memory, &z_room_memory}) { + m->sized_from = payload_size; + m->sized_to = payload_size + 256; + } + a2.make_action("b").async_send( + trystero::pmr_bytes(to_own, &a_room_memory), + boost::asio::bind_allocator(std::pmr::polymorphic_allocator<>(&a_room_memory), + [](boost::system::error_code) {})); + BOOST_TEST(run_until(io, [&] { return got == 1; })); + BOOST_TEST_EQ(z_room_memory.sized, 1U); // copied once, out of the frame + BOOST_TEST_EQ(z_memory.sized, 1U); // the received frame, and nothing more + z_memory.sized = 0; + a1.make_action("b").async_send( + trystero::pmr_bytes(to_shared, &a_memory), + boost::asio::bind_allocator(std::pmr::polymorphic_allocator<>(&a_memory), + [](boost::system::error_code) {})); + BOOST_TEST(run_until(io, [&] { return got == 2; })); + BOOST_TEST_EQ(z_memory.sized, 2U); // the frame, the payload taken out and moved on } - a2.make_action("b").async_send( - trystero::pmr_bytes(to_own, &a_room_memory), - boost::asio::bind_allocator(std::pmr::polymorphic_allocator<>(&a_room_memory), - [](boost::system::error_code) {})); - BOOST_TEST(run_until(io, [&] { return got == 1; })); - BOOST_TEST_EQ(z_room_memory.sized, 1U); // copied once, out of the frame - BOOST_TEST_EQ(z_memory.sized, 1U); // the received frame, and nothing more - z_memory.sized = 0; - a1.make_action("b").async_send( - trystero::pmr_bytes(to_shared, &a_memory), - boost::asio::bind_allocator(std::pmr::polymorphic_allocator<>(&a_memory), - [](boost::system::error_code) {})); - BOOST_TEST(run_until(io, [&] { return got == 2; })); - BOOST_TEST_EQ(z_memory.sized, 2U); // the frame, the payload taken out and moved on + run_for(io, 300); // the io_context runs out of the rooms' work } - run_for(io, 300); // the io_context runs out of the rooms' work + counting = false; } - counting = false; - trystero::pmr_bytes(&a_memory).swap(to_shared); - trystero::pmr_bytes(&a_room_memory).swap(to_own); explain_global_allocations(4U); BOOST_TEST_EQ(global_allocations, 4U); // the four rooms' JSON storage counters BOOST_TEST_EQ(c_allocations, 0U); diff --git a/test/support/memory_backend.hpp b/test/support/memory_backend.hpp index 131e911..e95a103 100644 --- a/test/support/memory_backend.hpp +++ b/test/support/memory_backend.hpp @@ -381,11 +381,43 @@ class memory_peer_connection; */ using connection_registry = std::pmr::map; +/** + * An allocator that takes a container's memory from a resource and constructs its elements as + * std::allocator does: an element keeps the allocator it was made with, where a pmr container would + * copy it into its own resource. MSVC's containers allocate even when empty (a list's sentinel + * node, and, in a debug build, every container's iterator proxy), which this keeps in the resource. + */ +template +struct node_allocator { + using value_type = T; + + explicit node_allocator(std::pmr::memory_resource* memory) noexcept : memory(memory) {} + + // Converting, as std::allocator's is: a container rebinds it to its nodes. + template + node_allocator(const node_allocator& other) noexcept // NOLINT(misc-explicit-constructor) + : memory(other.memory) {} + + T* allocate(std::size_t n) { + return static_cast(memory->allocate(n * sizeof(T), alignof(T))); + } + + void deallocate(T* p, std::size_t n) noexcept { + memory->deallocate(p, n * sizeof(T), alignof(T)); + } + + friend bool operator==(const node_allocator& a, const node_allocator& b) noexcept { + return a.memory == b.memory; + } + + std::pmr::memory_resource* memory; +}; + class memory_data_channel : public backend::data_channel, public std::enable_shared_from_this { public: memory_data_channel(boost::asio::io_context& io, std::pmr::memory_resource* memory) - : io_(io), memory_(memory) {} + : io_(io), memory_(memory), queue_(node_allocator(memory)) {} void set_handlers(handlers h) override { h_ = std::move(h); } @@ -459,8 +491,9 @@ class memory_data_channel : public backend::data_channel, // Alive exactly while the network is: a channel that outlives it sends nothing. std::weak_ptr registry_; std::size_t pc_ = 0; - // The frames sent and not yet received, and how much of the first one has drained. - std::list queue_; + // The frames sent and not yet received, each in the resource it was made in, and how much of + // the first one has drained. + std::list> queue_; std::size_t head_drained_ = 0; std::size_t buffered_ = 0; std::size_t drain_per_post_ = 1;