Zero-copy encode: pack response body in place, single shared tx_buf, drain rx loop
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
#include <cstdint>
|
||||
#include <cstddef>
|
||||
#include <span>
|
||||
|
||||
namespace halfsiphash {
|
||||
|
||||
@@ -34,7 +35,7 @@ inline void sipround(uint32_t &v0, uint32_t &v1, uint32_t &v2, uint32_t &v3) {
|
||||
} // namespace detail
|
||||
|
||||
// Compute HalfSipHash-2-4 with an 8-byte key, returning a 32-bit hash.
|
||||
inline uint32_t hash32(const uint8_t *data, size_t len, const uint8_t key[8]) {
|
||||
inline uint32_t hash32(std::span<const uint8_t> data, const uint8_t key[8]) {
|
||||
using namespace detail;
|
||||
|
||||
uint32_t k0 = load_le32(key);
|
||||
@@ -45,8 +46,8 @@ inline uint32_t hash32(const uint8_t *data, size_t len, const uint8_t key[8]) {
|
||||
uint32_t v2 = UINT32_C(0x6c796765) ^ k0;
|
||||
uint32_t v3 = UINT32_C(0x74656462) ^ k1;
|
||||
|
||||
const uint8_t *end = data + len - (len % 4);
|
||||
for (const uint8_t *p = data; p != end; p += 4) {
|
||||
const uint8_t *end = data.data() + data.size() - (data.size() % 4);
|
||||
for (const uint8_t *p = data.data(); p != end; p += 4) {
|
||||
uint32_t m = load_le32(p);
|
||||
v3 ^= m;
|
||||
sipround(v0, v1, v2, v3);
|
||||
@@ -54,8 +55,8 @@ inline uint32_t hash32(const uint8_t *data, size_t len, const uint8_t key[8]) {
|
||||
v0 ^= m;
|
||||
}
|
||||
|
||||
uint32_t b = static_cast<uint32_t>(len) << 24;
|
||||
switch (len & 3) {
|
||||
uint32_t b = static_cast<uint32_t>(data.size()) << 24;
|
||||
switch (data.size() & 3) {
|
||||
case 3: b |= static_cast<uint32_t>(end[2]) << 16; [[fallthrough]];
|
||||
case 2: b |= static_cast<uint32_t>(end[1]) << 8; [[fallthrough]];
|
||||
case 1: b |= static_cast<uint32_t>(end[0]); break;
|
||||
|
||||
@@ -236,6 +236,12 @@ public:
|
||||
return *this;
|
||||
}
|
||||
|
||||
pack_result pack_uint32_fixed(uint32_t n) {
|
||||
m_buf.push_back(format::UINT32);
|
||||
push_big_endian(n);
|
||||
return *this;
|
||||
}
|
||||
|
||||
pack_result pack_float(float n) {
|
||||
m_buf.push_back(format::FLOAT32);
|
||||
push_big_endian(n);
|
||||
@@ -322,6 +328,19 @@ public:
|
||||
return *this;
|
||||
}
|
||||
|
||||
pack_result pack_ext16_header(char type, uint16_t len) {
|
||||
m_buf.push_back(format::EXT16);
|
||||
push_big_endian(len);
|
||||
m_buf.push_back(static_cast<uint8_t>(type));
|
||||
return *this;
|
||||
}
|
||||
|
||||
pack_result pack_bin16_header(uint16_t len) {
|
||||
m_buf.push_back(format::BIN16);
|
||||
push_big_endian(len);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <class Range>
|
||||
pack_result pack_ext(char type, const Range &r) {
|
||||
auto sz = static_cast<size_t>(std::distance(std::begin(r), std::end(r)));
|
||||
|
||||
@@ -15,4 +15,4 @@ using net_handler = std::function<size_t(std::span<const uint8_t> payload, span_
|
||||
bool net_init();
|
||||
const net_state& net_get_state();
|
||||
void net_set_handler(net_handler handler);
|
||||
void net_poll();
|
||||
void net_poll(std::span<uint8_t> tx);
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <span>
|
||||
|
||||
class span_writer {
|
||||
uint8_t *m_data;
|
||||
@@ -10,6 +11,7 @@ class span_writer {
|
||||
|
||||
public:
|
||||
span_writer(uint8_t *data, size_t capacity) : m_data(data), m_capacity(capacity) {}
|
||||
span_writer(std::span<uint8_t> buf) : m_data(buf.data()), m_capacity(buf.size()) {}
|
||||
|
||||
void push_back(uint8_t v) {
|
||||
if (m_size < m_capacity) m_data[m_size++] = v;
|
||||
@@ -32,4 +34,12 @@ public:
|
||||
uint8_t *end() { return m_data + m_size; }
|
||||
const uint8_t *begin() const { return m_data; }
|
||||
const uint8_t *end() const { return m_data + m_size; }
|
||||
|
||||
span_writer subspan(size_t offset) {
|
||||
return span_writer(m_data + offset, m_capacity - offset);
|
||||
}
|
||||
|
||||
span_writer subspan(size_t offset, size_t len) {
|
||||
return span_writer(m_data + offset, len);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -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()));
|
||||
|
||||
Reference in New Issue
Block a user