Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions doc/allocation.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -300,24 +300,23 @@ nothing.
completion in a function object of its recycling cache (measured: six
`operator new` and four of its blocks for a text sent and its echo
received);
* in a page, for each relay connection, the results webcpp::asio's page
resolver makes with `new`, which takes no allocator: Asio's list of one
entry, its shared count and the entry's storage, three allocations, and two
copies of the host when it is longer than a short string holds (10 bytes
in a page: `relay.damus.io`, of 14, is longer); and, for each event of the page's
`WebSocket`, its open, each message and its close, the C library's
allocation embind makes for the `std::string` into which webcpp::beast's
page stream reads the event's type (`event["type"].as<std::string>()` in
its `deliver`), and for a message a second, for the type of its data
* in a page, for each event of a relay's `WebSocket`, its open, each message
and its close, the C library's allocation embind makes for the
`std::string` into which webcpp::beast's page stream reads the event's
type (`event["type"].as<std::string>()` in its `deliver`), and for a
message a second, for the type of its data
(`data.typeOf().as<std::string>()` in its `receive_message`), each freed
before the call that made it returns: two for each message a relay sends.
The test `platform_page_relay_allocation` counts exactly these, no more
and no fewer, from a connection's call to its open, for one message and
for ten, and from its close to the page's close event. The stream's own
state, its operations, the messages it holds and the read buffer are in
the room's resource, and the page's `WebSocket` makes the handshake;
avoiding embind's strings, and the resolver's results, needs a change to
webcpp::beast's page stream and to webcpp::asio's page resolver;
webcpp::asio's page resolver holds its results, the host and the service,
in place, and allocates nothing, whatever the host's length. Embind's
strings stay: they are freed at once, and avoiding them would need
webcpp::beast's page stream to read an event's type without a
`std::string`;
* the payload types `std::string`, `bytes`, `webcpp::document::document` and
`payload`, with `std::allocator`, the user's choice;
* on Emscripten, the C library's `select()`, which Asio's reactor calls on
Expand Down
4 changes: 2 additions & 2 deletions doc/guarantees.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ construction until its `io_context` has drained, neither the global
and write their JSON texts and exchange text, binary and JSON payloads of
the `std::pmr` types and `webcpp::document::pmr::document`; over the
platform backend's relays, natively a message adds nothing, and in a page
each of a relay's connections adds its resolver's results and each event of
its `WebSocket` embind's string of the event's type, two for a message.
each event of a relay's `WebSocket` adds embind's string of the event's
type, two for a message.
_Held by_ `allocation`, natively and on emscripten; for a page's sealer,
`platform_page_sealer_allocation`; and for the relays,
`platform_websocket_allocation` natively and
Expand Down
5 changes: 2 additions & 3 deletions doc/platform.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,8 @@ Asio and OpenSSL allocate of their own, which takes no allocator (their
streams' state, the resolver's, the handshake's request and response, the
verify callback, their timers' waits and each keep-alive ping's frame), and
a message allocates nothing more than the wait of the idle timer, from
Asio's per-thread cache; in a page, for each connection, the resolver's
results, and for each event of the page's `WebSocket` the string embind makes
of its type, two for a message. A write that fails ends the connection,
Asio's per-thread cache; in a page, for each event of the page's
`WebSocket`, the string embind makes of its type, two for a message. A write that fails ends the connection,
which reports its close at once. A relay's messages are texts (NIP-01): a
binary one is dropped, on both targets. The WebSockets are Beast's rather
than libdatachannel's own, so the relays run on the room's executor like
Expand Down
16 changes: 7 additions & 9 deletions include/webcpp/trystero/platform/websocket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,11 +319,9 @@ inline std::optional<url_parts> parse_url(
* cache (the TCP stream's deadline over the connection and the TLS handshake, the WebSocket's
* handshake, idle and close timeouts, the idle one started again at each read); and each
* keep-alive ping's frame. On Emscripten the page's stream keeps its state in the room's resource
* too, and its `WebSocket` makes the handshake; what stays outside is, for each connection, the
* results webcpp::asio's page resolver makes with `new` (Asio's list of one entry, its shared count
* and the entry's storage, and two copies of the host when it is longer than a short string holds,
* 10 bytes in a page), and, for each event of the page's `WebSocket` (its open, each message and
* its close), the C library's allocation embind makes for the `std::string` into which
* too, its `WebSocket` makes the handshake, and webcpp::asio's page resolver holds its results in
* place; what stays outside is, for each event of the page's `WebSocket` (its open, each message
* and its close), the C library's allocation embind makes for the `std::string` into which
* webcpp::beast's page stream reads the event's type, and for a message a second, for the type of
* its data, each freed before the call that made it returns.
*/
Expand Down Expand Up @@ -785,10 +783,10 @@ class websocket_handle : public backend::websocket {
* response, the verify callback, their timers' waits, the idle timer's again at each read, and
* each keep-alive ping's frame; a relay that answers the handshake with a status other than 101
* has its response's header read, at most 8 KiB, and no byte of its body, through webcpp::beast's
* bounded handshake layer, before the connection reports its close. In a page, the page
* resolver's results, made with `new`, and, for each event of the page's `WebSocket`, the open,
* each message and the close, the C library's allocation embind makes for its type, and for a
* message a second, for the type of its data, each freed before the call that made it returns.
* bounded handshake layer, before the connection reports its close. In a page, for each event of
* the page's `WebSocket`, the open, each message and the close, the C library's allocation embind
* makes for its type, and for a message a second, for the type of its data, each freed before the
* call that made it returns.
*
* @param ex The executor every handler runs on.
* @param tls The TLS context of `wss://`, which must outlive the connection, or null when the
Expand Down
24 changes: 13 additions & 11 deletions test/platform_page_relay_allocation_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@
// exactly what the guide's "What stays outside the resource" lists, no more and no fewer, so that a
// change either way is seen:
//
// - a connection, from the call that makes it to its open: three `operator new` of the page's
// resolver for its results (Asio's list of one entry, its shared count and the entry's storage),
// two more for the host's two copies when it is longer than a short string holds (10 bytes in a
// page), and one C library allocation, embind's for the type of the open event, which
// webcpp::beast's page stream reads as a `std::string`;
// - a connection, from the call that makes it to its open: one C library allocation, embind's for
// the type of the open event, which webcpp::beast's page stream reads as a `std::string`, and no
// `operator new`, whatever the host's length: webcpp::asio's page resolver holds its results'
// one entry, the host and the service, in place;
// - each message the relay sends, to its arrival in the room: two C library allocations, embind's
// for the event's type and for the type of its data, which the stream reads as `std::string`s;
// - the close, from the call to the page's close event, delivered after the room was told: one C
Expand Down Expand Up @@ -331,7 +330,10 @@ struct connection_counts {
counts close;
};

/** The relays of the rounds: the first warms up, uncounted; the third's host is a long string. */
/**
* The relays of the rounds: the first warms up, uncounted; the third's host is longer than a short
* string holds (10 bytes in a page), which the resolver's results hold in place too.
*/
constexpr std::array<std::string_view, 3> relays{
"wss://relay.test",
"wss://relay.test",
Expand Down Expand Up @@ -472,9 +474,9 @@ void expect(const counts& got, std::size_t global, std::size_t c_library, const
BOOST_TEST_GT(got.room, 0U);
}

/** Checks a counted round, whose host's two copies take `host_copies` more `operator new`. */
void expect_round(const connection_counts& got, std::size_t host_copies) {
expect(got.open, 3 + host_copies, 1, "a connection did not open");
/** Checks a counted round. */
void expect_round(const connection_counts& got) {
expect(got.open, 0, 1, "a connection did not open");
expect(got.one_message, 0, 2, "a message did not reach the room");
expect(got.ten_messages, 0, 20, "ten messages did not reach the room");
expect(got.close, 0, 1, "a connection did not close");
Expand All @@ -492,7 +494,7 @@ int main() {
if (!s.done) {
BOOST_ERROR("the rounds stopped before the last");
}
expect_round(s.rounds.at(1), 0); // relay.test, a short string
expect_round(s.rounds.at(2), 2); // relay.example.org, longer
expect_round(s.rounds.at(1)); // relay.test, a short string
expect_round(s.rounds.at(2)); // relay.example.org, longer
return webcpp::test::exit_status(boost::report_errors());
}
Loading