diff --git a/README.md b/README.md index 20c1751..c38ebc2 100644 --- a/README.md +++ b/README.md @@ -809,15 +809,27 @@ to the archive in every configuration, so no row here moves when it changes. Its cost lands in **your** translation unit instead, and it is paid per TU, because the templates instantiate per TU. -For one representative message (five scalar fields, a nested sequence and a -string) on ARMv6-m at the same `-Os`: - -| Consumer translation unit | `.text` | -| - | -: | -| C API directly | 338 B | -| C++ wrapper, freestanding | 834 B | -| C++ wrapper, hosted | 884 B | -| C++ wrapper, hosted, RTTI on | 1034 B | +For one representative message — five scalar fields, a nested sequence holding +two scalars, and a string — on ARMv6-m at the same `-Os`. The measured +translation units are [`test/footprint-tu/`](test/footprint-tu/): `consumer.c` +and `consumer.cpp` encode and decode the *same* message, one through the C API +and one through the wrapper, so the rows are comparable. Regenerated by +[`tools/footprint-tu.sh`](tools/footprint-tu.sh), which compiles each alone +(`-c`) and reads `size` off the object, so library calls stay external +references and only the TU's own code is counted: + +| Consumer translation unit | `.text` | over the C API | +| - | -: | -: | +| C API directly | 440 B | — | +| C++ wrapper, freestanding | 962 B | +522 B | +| C++ wrapper, hosted | 1180 B | +740 B | +| C++ wrapper, hosted, RTTI on | 1447 B | +1007 B | + +The same TU is used for all three wrapper rows, and it reaches only for the +heap-free surface — which exists in both modes — so the 218 B from +freestanding to hosted is what the header *gates*, not what the consumer asked +for, and the 267 B on top is the RTTI the virtual bases emit. A +freestanding MCU consumer therefore pays 962 B per TU, not 1447. The two *Minimal* rows above are **C-only by construction**: the wrapper rejects `SOFAB_DISABLE_FIXLEN_SUPPORT`, `..._SEQUENCE_SUPPORT` and diff --git a/test/footprint-tu/consumer.c b/test/footprint-tu/consumer.c new file mode 100644 index 0000000..02dc905 --- /dev/null +++ b/test/footprint-tu/consumer.c @@ -0,0 +1,155 @@ +/*! + * @file consumer.c + * @brief The reference consumer translation unit, written against the C API. + * + * SPDX-License-Identifier: MIT + * + * This file and its C++ twin (consumer.cpp) exist for one purpose: to make the + * README's per-TU footprint table reproducible. That table reports what a + * consumer's own translation unit costs -- not the library, which is measured by + * tools/footprint.sh -- and the cost is paid per TU because the C++ wrapper is + * header-only and its templates instantiate into the caller. + * + * The two files encode and decode the SAME message, so the numbers are + * comparable: five scalar fields (u32, u16, bool, fp32 and the u32 that opens + * the message), one nested sequence holding two scalars, and one string. Keep + * them in step -- a field added to one and not the other silently turns the + * table into a comparison of two different messages. + * + * Measured by tools/footprint-tu.sh, which compiles this file alone (-c) and + * reports `size` on the object. Nothing links it, so the library calls stay + * external references and only this TU's own code is counted. + * + * The round trip is `extern` and its results are consumed through a volatile, + * so -Os cannot delete the work and leave an empty function behind. + */ + +#include "sofab/istream.h" +#include "sofab/ostream.h" + +#include +#include + +/* What the decode produced, read back through a volatile so the optimiser has + * to keep every store that feeds it. */ +volatile uint32_t g_sink_u32; +volatile size_t g_flushed; + +/* --- the message ---------------------------------------------------------- */ + +typedef struct +{ + uint32_t id; + float value; +} child_t; + +typedef struct +{ + uint32_t header; /* id 1 */ + child_t child; /* id 2, a nested sequence */ + uint32_t footer; /* id 3 */ + bool flag; /* id 4 */ + float ratio; /* id 5 */ + char tag[17]; /* id 6, a string (16 + terminator) */ + uint16_t count; /* id 7 */ +} parent_t; + +/* --- decode --------------------------------------------------------------- */ + +static void child_cb (sofab_istream_t *ctx, sofab_id_t id, size_t size, + size_t count, void *usrptr) +{ + child_t *c = (child_t *)usrptr; + + (void)size; + (void)count; + + switch (id) + { + case 1: sofab_istream_read_u32(ctx, &c->id); break; + case 2: sofab_istream_read_fp32(ctx, &c->value); break; + default: break; + } +} + +static void parent_cb (sofab_istream_t *ctx, sofab_id_t id, size_t size, + size_t count, void *usrptr) +{ + parent_t *p = (parent_t *)usrptr; + /* One decoder per nested sequence, as sofab_istream_read_sequence requires. + * Static rather than automatic because the decoder must outlive this + * callback: the nested fields arrive on later feed() calls. */ + static sofab_istream_decoder_t child_decoder; + + (void)count; + + switch (id) + { + case 1: sofab_istream_read_u32(ctx, &p->header); break; + case 2: + sofab_istream_read_sequence(ctx, &child_decoder, &child_cb, + &p->child); + break; + case 3: sofab_istream_read_u32(ctx, &p->footer); break; + case 4: sofab_istream_read_bool(ctx, &p->flag); break; + case 5: sofab_istream_read_fp32(ctx, &p->ratio); break; + case 6: + if (size < sizeof(p->tag)) + { + sofab_istream_read_string(ctx, p->tag, sizeof(p->tag)); + } + break; + case 7: sofab_istream_read_u16(ctx, &p->count); break; + default: break; + } +} + +/* --- encode --------------------------------------------------------------- */ + +static void on_flush (sofab_ostream_t *ctx, const uint8_t *data, size_t len, + void *usrptr) +{ + (void)ctx; + (void)data; + (void)usrptr; + + g_flushed = len; +} + +extern int sofab_tu_roundtrip (void) +{ + static uint8_t buffer[256]; + sofab_ostream_t os; + sofab_istream_t is; + parent_t decoded; + + memset(&decoded, 0, sizeof(decoded)); + + sofab_ostream_init(&os, buffer, sizeof(buffer), 0, &on_flush, NULL); + + sofab_ostream_write_unsigned(&os, 1, 7u); + sofab_ostream_write_sequence_begin_lazy(&os, 2); + sofab_ostream_write_unsigned(&os, 1, 42u); + sofab_ostream_write_fp32(&os, 2, 3.1415f); + sofab_ostream_write_sequence_end(&os); + sofab_ostream_write_unsigned(&os, 3, 99u); + sofab_ostream_write_boolean(&os, 4, true); + sofab_ostream_write_fp32(&os, 5, 2.5f); + sofab_ostream_write_string(&os, 6, "tag42"); + sofab_ostream_write_unsigned(&os, 7, 3u); + + const size_t used = sofab_ostream_bytes_used(&os); + + sofab_istream_init(&is, &parent_cb, &decoded); + if (sofab_istream_feed(&is, buffer, used) != SOFAB_RET_OK) + { + return -1; + } + + g_sink_u32 = decoded.header + decoded.footer + decoded.child.id + + (uint32_t)decoded.count + (uint32_t)decoded.flag + + (uint32_t)decoded.ratio + (uint32_t)decoded.child.value + + (uint32_t)(unsigned char)decoded.tag[0]; + + return 0; +} diff --git a/test/footprint-tu/consumer.cpp b/test/footprint-tu/consumer.cpp new file mode 100644 index 0000000..293d236 --- /dev/null +++ b/test/footprint-tu/consumer.cpp @@ -0,0 +1,137 @@ +/*! + * @file consumer.cpp + * @brief The reference consumer translation unit, written against the C++ wrapper. + * + * SPDX-License-Identifier: MIT + * + * The C++ twin of consumer.c: the same message, the same round trip, through + * sofab/sofab.hpp instead of the C API. See consumer.c for why both exist and + * what has to stay in step between them. + * + * It uses only the heap-free surface (FixedString, OStreamInline, + * IStreamObject, a function-pointer flush callback), because that surface is + * the one that exists in BOTH wrapper modes -- so the freestanding and hosted + * rows of the README table differ by what the header gates, not by what this + * file asks for. tools/footprint-tu.sh compiles it in three modes: freestanding + * (-ffreestanding, SOFAB_CPP_HAVE_HOSTED off), hosted, and hosted with RTTI on. + */ + +#include "sofab/sofab.hpp" + +#include + +namespace +{ + +/*! @brief The nested sequence's two scalars (ids 1 and 2). */ +class Child : public sofab::IStreamMessage +{ +public: + struct Data + { + std::uint32_t id = 0; + float value = 0.0f; + } data_; + + void deserialize (sofab::IStreamImpl &is, sofab::id id, size_t, + size_t) noexcept override + { + switch (id) + { + case 1: is.read(data_.id); break; + case 2: is.read(data_.value); break; + default: break; + } + } +}; + +/*! @brief Five scalars, one nested sequence, one string -- the message the + * README's per-TU table is measured on. */ +class Parent : public sofab::IStreamMessage +{ +public: + struct Data + { + std::uint32_t header = 0; // id 1 + Child child; // id 2, a nested sequence + std::uint32_t footer = 0; // id 3 + bool flag = false; // id 4 + float ratio = 0.0f; // id 5 + sofab::FixedString<16> tag; // id 6, a string + std::uint16_t count = 0; // id 7 + } data_; + + void deserialize (sofab::IStreamImpl &is, sofab::id id, size_t size, + size_t) noexcept override + { + switch (id) + { + case 1: is.read(data_.header); break; + case 2: is.read(data_.child); break; + case 3: is.read(data_.footer); break; + case 4: is.read(data_.flag); break; + case 5: is.read(data_.ratio); break; + case 6: + data_.tag.set_len(size); + if (size) + { + is.read(data_.tag); + } + break; + case 7: is.read(data_.count); break; + default: break; + } + } +}; + +} // namespace + +/* Consumed through volatiles so -Os cannot delete the round trip. */ +volatile std::uint32_t g_sink_u32; +volatile size_t g_flushed; + +namespace +{ + +void onFlush (std::span bytes) noexcept +{ + g_flushed = bytes.size(); +} + +} // namespace + +extern "C" int sofab_tu_roundtrip (void) +{ + sofab::OStreamInline<256> os{&onFlush}; + + os.write(1, 7u) + .sequenceBeginLazy(2) + .write(1, 42u) + .write(2, 3.1415f) + .sequenceEnd() + .write(3, 99u) + .write(4, true) + .write(5, 2.5f) + .write(6, sofab::FixedString<16>{"tag42"}) + .write(7, static_cast(3u)); + + const size_t used = os.bytesUsed(); + + sofab::IStreamObject is; + if (is.feed(os.data(), used).code() != sofab::Error::None) + { + return -1; + } + + const Parent::Data &d = (*is).data_; + + g_sink_u32 = d.header + d.footer + d.child.data_.id + + static_cast(d.count) + + static_cast(d.flag) + + static_cast(d.ratio) + + static_cast(d.child.data_.value) + + static_cast( + static_cast(d.tag.data()[0])); + + return 0; +} diff --git a/tools/footprint-tu.sh b/tools/footprint-tu.sh new file mode 100755 index 0000000..ef91ae2 --- /dev/null +++ b/tools/footprint-tu.sh @@ -0,0 +1,117 @@ +#!/usr/bin/env bash +# +# footprint-tu.sh — reproduce the README "The C++ wrapper on bare metal" table. +# +# tools/footprint.sh measures the library. This script measures the OTHER half of +# the footprint story: what a consumer's own translation unit costs. That cost +# exists because the C++ wrapper is header-only -- it contributes 0 B to +# libsofabuffers.a in every configuration, and its templates instantiate into +# the caller instead, once per TU. +# +# The measured TUs live in test/footprint-tu/: consumer.c and consumer.cpp encode +# and decode the SAME message (five scalar fields, one nested sequence holding two +# scalars, one string), so the C row and the wrapper rows are comparable. Each is +# compiled alone (-c) and `size` is read off the object, so the library calls stay +# external references and only the TU's own code is counted. +# +# The four rows differ only in how the wrapper is configured: +# C API directly consumer.c, no wrapper at all +# C++ wrapper, freestanding -ffreestanding, so __STDC_HOSTED__ is 0 and +# sofab.hpp selects its no-heap surface +# (SOFAB_CPP_HAVE_HOSTED off) +# C++ wrapper, hosted the same TU without -ffreestanding, so the +# hosted convenience layer compiles in +# C++ wrapper, hosted, RTTI as hosted, but without -fno-rtti, which is what +# the top-level CMakeLists passes for C++ +# +# consumer.cpp deliberately uses only the heap-free surface, which exists in both +# wrapper modes -- so the freestanding and hosted rows differ by what the header +# gates, not by what the TU asks for. +# +# Flags mirror a real build of this project for Cortex-M0: the Release flags from +# the top-level CMakeLists.txt plus the toolchain flags from +# utils/cortex-m/toolchain-arm-none-eabi.cmake. ARMv6-m is the architecture the +# README table uses, being the smallest row of the library tables. +# +# Toolchain (Debian/Ubuntu): gcc-arm-none-eabi +# +# Usage: +# tools/footprint-tu.sh +# +set -euo pipefail + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)" +OUT="${OUT:-$(mktemp -d)}" +TU_DIR="${ROOT}/test/footprint-tu" + +# --- preflight ------------------------------------------------------------- +for tool in arm-none-eabi-gcc arm-none-eabi-g++ arm-none-eabi-size; do + command -v "${tool}" >/dev/null 2>&1 || { + echo "error: missing ${tool}" >&2 + echo "install it with:" >&2 + echo " sudo apt-get install -y gcc-arm-none-eabi" >&2 + exit 1 + } +done + +# --- flags ----------------------------------------------------------------- +# utils/cortex-m/toolchain-arm-none-eabi.cmake, minus -ffreestanding: that one is +# per-row below, because dropping it is exactly what selects the hosted wrapper. +ARCH=(-march=armv6-m -mtune=cortex-m0 -mthumb -mno-unaligned-access) + +# CMakeLists.txt: the Release block plus the size flags. -fno-exceptions is in +# both the C and C++ sets; -fno-rtti is C++-only and is per-row below. +OPT=(-Os -DNDEBUG -fno-asynchronous-unwind-tables -fno-exceptions + -ffunction-sections -fdata-sections) + +WARN=(-Wall -Wextra -Wpedantic -Werror) + +# --------------------------------------------------------------------------- +# text_of — the .text byte count of one object file. +# --------------------------------------------------------------------------- +text_of() { + arm-none-eabi-size "$1" | awk 'NR==2 {print $1}' +} + +declare -A TEXT + +echo ">> Consumer translation unit, ARMv6-m, -Os" + +# Row 1: the C API, no wrapper. -ffreestanding as the toolchain file sets it. +arm-none-eabi-gcc "${ARCH[@]}" -ffreestanding "${OPT[@]}" "${WARN[@]}" \ + -std=c99 -I"${ROOT}/src/include" \ + -c "${TU_DIR}/consumer.c" -o "${OUT}/c.o" +TEXT[c]="$(text_of "${OUT}/c.o")" + +# Rows 2-4: the wrapper, three configurations of the same TU. +# label|extra flags +for row in \ + "free|-ffreestanding -fno-rtti" \ + "hosted|-fno-rtti" \ + "hosted-rtti|" +do + label="${row%%|*}" + read -r -a extra <<<"${row#*|}" + arm-none-eabi-g++ "${ARCH[@]}" "${OPT[@]}" "${WARN[@]}" \ + -std=c++20 "${extra[@]}" -I"${ROOT}/src/include" \ + -c "${TU_DIR}/consumer.cpp" -o "${OUT}/${label}.o" + TEXT[${label}]="$(text_of "${OUT}/${label}.o")" +done + +printf ' %-32s %5s B\n' "C API directly" "${TEXT[c]}" +printf ' %-32s %5s B\n' "C++ wrapper, freestanding" "${TEXT[free]}" +printf ' %-32s %5s B\n' "C++ wrapper, hosted" "${TEXT[hosted]}" +printf ' %-32s %5s B\n' "C++ wrapper, hosted, RTTI on" "${TEXT[hosted-rtti]}" + +cat <