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
30 changes: 21 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
155 changes: 155 additions & 0 deletions test/footprint-tu/consumer.c
Original file line number Diff line number Diff line change
@@ -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 <stdint.h>
#include <string.h>

/* 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;
}
137 changes: 137 additions & 0 deletions test/footprint-tu/consumer.cpp
Original file line number Diff line number Diff line change
@@ -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 <cstdint>

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<const std::uint8_t> 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<std::uint16_t>(3u));

const size_t used = os.bytesUsed();

sofab::IStreamObject<Parent> 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<std::uint32_t>(d.count)
+ static_cast<std::uint32_t>(d.flag)
+ static_cast<std::uint32_t>(d.ratio)
+ static_cast<std::uint32_t>(d.child.data_.value)
+ static_cast<std::uint32_t>(
static_cast<unsigned char>(d.tag.data()[0]));

return 0;
}
Loading
Loading