Wire codec deep-dive & throughput (L2/L3)¶
Where wire codec round-trip proves byte-identity, this is the
anatomy and performance companion. It builds a POINT TLV carrying two VALUE
children with a CRC trailer, prints the encoded size and the raw header bytes, then
times encode (model → bytes), decode (bytes → borrowed tree), and the full
round-trip over 50,000 iterations. See the
frame codec module and the
bit-level walkthrough for the byte layout.
What to notice¶
The header is tiny and fixed — the example prints the first bytes (type,
opt, the fixed-width length); aPOINT{VALUE,VALUE}with a CRC trailer is 24 bytes total.Decode is zero-copy — each child’s payload is a
std::spanborrowing the encoded buffer; the example checks the payload address lies insidewire.Encode/decode are separately timed — decode (validate + build the borrowed tree) is typically cheaper than encode (materialize the byte vector + CRC); the
RESULTline reports both plus the round-trip rate.
Note
The absolute nanoseconds come from whatever build ran (CI builds the examples in a debug configuration); treat them as a shape, not a spec number. The canonical, release-build, CI-published codec figures — including the cross-core cpp/ts/rust comparison — live on the performance page.
Source¶
1/*
2 * SPDX-License-Identifier: Apache-2.0
3 * SPDX-FileCopyrightText: Copyright 2026 avatarsd LLC
4 */
5
6/**
7 * @file
8 * @brief Wire codec deep-dive — build a structured frame, inspect its bytes, and
9 * measure encode / decode / round-trip throughput.
10 *
11 * Where `wire_roundtrip` proves byte-identity, this example is the performance and
12 * anatomy companion (`docs/modules/frame-codec.md`, `docs/modules/wire-format-bits.md`).
13 * It builds a POINT TLV carrying two VALUE children with a CRC trailer, prints the
14 * encoded size and the raw header bytes, then times three things over many
15 * iterations: `encode` (model → bytes), `decode` (bytes → borrowed tree), and the
16 * full round-trip. It also confirms the decode is zero-copy (payload spans borrow
17 * the encoded buffer) and that re-encoding is byte-identical.
18 *
19 * RESULT perf lines are informational (CI never flakes on timing); the self-checks
20 * guard correctness. Runs under ctest as `example_wire_codec`.
21 */
22
23#include <chrono>
24#include <cstddef>
25#include <cstdint>
26#include <cstdio>
27#include <span>
28#include <vector>
29
30#include "libtracer/tracer.hpp"
31
32namespace {
33
34using clock_t_ = std::chrono::steady_clock;
35using tr::wire::opt_t;
36using tr::wire::tlv_t;
37using tr::wire::type_t;
38
39/** @brief A VALUE TLV borrowing @p bytes (little-endian payload; must outlive the TLV). */
40tlv_t value_tlv(std::span<const std::byte> bytes) {
41 tlv_t t;
42 t.type = type_t::VALUE;
43 t.payload = bytes;
44 return t;
45}
46
47/** @brief Record a failed expectation on @p ok and report it. */
48void check(bool& ok, bool cond, const char* what) {
49 if (!cond) {
50 std::printf(" [FAIL] %s\n", what);
51 ok = false;
52 }
53}
54
55} // namespace
56
57int main() {
58 // Two 4-byte little-endian VALUE payloads; their bytes outlive every TLV below.
59 const std::vector<std::byte> a = {std::byte{0x17}, std::byte{0x00}, std::byte{0x00},
60 std::byte{0x00}};
61 const std::vector<std::byte> b = {std::byte{0x2A}, std::byte{0x00}, std::byte{0x00},
62 std::byte{0x00}};
63
64 // A structured POINT (opt.pl = payload-is-children) with a CRC-32C trailer.
65 tlv_t point;
66 point.type = type_t::POINT;
67 point.opt = opt_t{.pl = true, .cr = true};
68 point.children = {value_tlv(a), value_tlv(b)};
69
70 const std::vector<std::byte> wire = tr::wire::encode(point);
71 std::printf("encoded POINT{VALUE,VALUE}+CRC: %zu bytes\n", wire.size());
72 std::printf(" header bytes:");
73 for (std::size_t i = 0; i < wire.size() && i < 6; ++i)
74 std::printf(" %02X", static_cast<unsigned>(std::to_integer<std::uint8_t>(wire[i])));
75 std::printf(" (type, opt, length, …)\n");
76
77 bool ok = true;
78 auto decoded = tr::wire::decode(std::span<const std::byte>(wire));
79 check(ok, decoded.has_value(), "decode succeeds (CRC verifies)");
80 if (decoded) {
81 check(ok, decoded->type == type_t::POINT, "root is a POINT");
82 check(ok, decoded->children.size() == 2, "POINT has two VALUE children");
83 check(ok, decoded->trailer && decoded->trailer->crc.has_value(),
84 "CRC trailer present and verified");
85 if (decoded->children.size() == 2) {
86 const auto c0 = decoded->children[0].payload;
87 check(ok, c0.data() >= wire.data() && c0.data() < wire.data() + wire.size(),
88 "decoded payload borrows the encoded buffer (zero copy)");
89 }
90 check(ok, tr::wire::encode(*decoded) == wire, "encode(decode(bytes)) == bytes");
91 }
92
93 // --- perf: encode / decode / round-trip over the same frame ---
94 constexpr int kIters = 50000;
95 std::size_t sink = 0;
96
97 auto t0 = clock_t_::now();
98 for (int i = 0; i < kIters; ++i) sink += tr::wire::encode(point).size();
99 auto t1 = clock_t_::now();
100 for (int i = 0; i < kIters; ++i) {
101 auto d = tr::wire::decode(std::span<const std::byte>(wire));
102 sink += d ? d->children.size() : 0;
103 }
104 auto t2 = clock_t_::now();
105
106 const double enc_ns = std::chrono::duration<double, std::nano>(t1 - t0).count() / kIters;
107 const double dec_ns = std::chrono::duration<double, std::nano>(t2 - t1).count() / kIters;
108 const double rt_Mps = 1.0 / ((enc_ns + dec_ns) * 1e-9) / 1e6;
109 std::printf(
110 "RESULT wire_codec bytes=%zu encode_ns=%.0f decode_ns=%.0f "
111 "roundtrip_Mps=%.2f (sink=%zu)\n",
112 wire.size(), enc_ns, dec_ns, rt_Mps, sink);
113
114 std::printf("%s\n", ok ? "wire codec OK" : "wire codec FAILED");
115 return ok ? 0 : 1;
116}
See also: frame-codec module · wire codec round-trip · bit-level wire walkthrough.