Zero-copy encode: pack response body in place, single shared tx_buf, drain rx loop

This commit is contained in:
Ian Gulliver
2026-04-10 22:48:28 +09:00
parent 58db392bf3
commit 8408603390
8 changed files with 98 additions and 57 deletions

View File

@@ -97,27 +97,39 @@ struct DecodedMessage {
std::vector<uint8_t> payload;
};
inline size_t pack_envelope_into(span_writer &out, uint32_t message_id, const uint8_t *payload, size_t payload_len) {
uint32_t checksum = halfsiphash::hash32(payload, payload_len, hash_key);
uint8_t env_buf[512];
span_writer env_body(env_buf, sizeof(env_buf));
msgpack::packer env_p(env_body);
env_p.pack_array(3);
env_p.pack(message_id);
env_p.pack(checksum);
env_p.pack_bin(std::span<const uint8_t>{payload, payload_len});
msgpack::packer outer(out);
outer.pack_ext(Envelope::ext_id, env_body);
return out.size();
}
static constexpr size_t ext16_header_len = 4;
static constexpr size_t array3_header_len = 1;
static constexpr size_t uint32_fixed_len = 5;
static constexpr size_t bin16_header_len = 3;
static constexpr size_t envelope_hdr_len =
ext16_header_len + array3_header_len +
uint32_fixed_len + uint32_fixed_len + bin16_header_len;
static constexpr size_t response_prefix_len = envelope_hdr_len + ext16_header_len;
template <typename T>
inline size_t encode_response_into(span_writer &out, uint32_t message_id, const T &msg) {
uint8_t inner_buf[256];
msgpack::packer inner(inner_buf, sizeof(inner_buf));
inner.pack(msg);
auto &pl = inner.get_payload();
return pack_envelope_into(out, message_id, pl.data(), pl.size());
auto body = out.subspan(response_prefix_len);
msgpack::packer body_p(body);
body_p.pack(msg.as_tuple());
auto inner_ext = out.subspan(envelope_hdr_len, ext16_header_len);
msgpack::packer(inner_ext).pack_ext16_header(T::ext_id, static_cast<uint16_t>(body.size()));
size_t bin_len = inner_ext.size() + body.size();
uint32_t checksum = halfsiphash::hash32({inner_ext.data(), bin_len}, hash_key);
auto env_hdr = out.subspan(0, envelope_hdr_len);
size_t env_body_len = array3_header_len + uint32_fixed_len + uint32_fixed_len + bin16_header_len + bin_len;
msgpack::packer hdr(env_hdr);
hdr.pack_ext16_header(Envelope::ext_id, static_cast<uint16_t>(env_body_len));
hdr.pack_array(3);
hdr.pack_uint32_fixed(message_id);
hdr.pack_uint32_fixed(checksum);
hdr.pack_bin16_header(static_cast<uint16_t>(bin_len));
return response_prefix_len + body.size();
}
inline msgpack::result<DecodedMessage> try_decode(const uint8_t *data, size_t len) {
@@ -127,7 +139,7 @@ inline msgpack::result<DecodedMessage> try_decode(const uint8_t *data, size_t le
auto r = msgpack::unpack(p, env);
if (!r) return std::unexpected(r.error());
uint32_t expected = halfsiphash::hash32(env.payload.data(), env.payload.size(), hash_key);
uint32_t expected = halfsiphash::hash32(env.payload, hash_key);
if (env.checksum != expected) return std::unexpected(msgpack::error_code::invalid);
msgpack::parser inner(env.payload.data(), static_cast<int>(env.payload.size()));
@@ -154,7 +166,7 @@ inline msgpack::result<T> decode_response(const uint8_t *data, size_t len) {
auto r = msgpack::unpack(p, env);
if (!r) return std::unexpected(r.error());
uint32_t expected = halfsiphash::hash32(env.payload.data(), env.payload.size(), hash_key);
uint32_t expected = halfsiphash::hash32(env.payload, hash_key);
if (env.checksum != expected) return std::unexpected(msgpack::error_code::invalid);
msgpack::parser inner(env.payload.data(), static_cast<int>(env.payload.size()));