From 596f0d6f48dab563bfd8ab796d734273c8b2f2a6 Mon Sep 17 00:00:00 2001 From: Andreas Steinbart Date: Tue, 13 Oct 2026 20:07:42 +0000 Subject: [PATCH] feat(cpp): IStreamInto decodes into a message the caller owns MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit IStreamObject holds the message it decodes into, so a caller that already has one -- which every generated try_decode on this profile does -- can only decode into the stream's instance and copy it across afterwards. On a heap-free target that is a second whole message on the stack of every decode plus a copy of it: 1096 B on the arena's example message (sofa-buffers/generator#744). IStreamInto is IStreamObject with the instance taken out: it binds the message handed to its constructor, through the same C-ABI field callback and the same two assignments, and holds a pointer where IStreamObject holds a MessageType. There is no std::function anywhere near it -- that is what kept the generator staging on this profile, since IStreamInline has one and freestanding has no std::function at all -- so the decoder that writes into the caller's object costs no more dispatch than the one that owns it. What it buys is paid for in the contract, and the doc says so: the bound object is written as the bytes arrive, so a rejected input leaves the fields decoded before the error in it, and a field the bytes do not carry keeps what the object already held (MESSAGE_SPEC §2 omits an all-default field). Both are why generated code calls reset() first. corelib-cpp's inline path has documented exactly this all along; the two C++ profiles now say the same thing about the same entry point. Measured on the generator's side of the pair, ARMv6-m -Os, the whole image gc-sectioned (5-field example message, sizeof 504 B): stack, reset() frame 1832 B -> 1288 B (-544 B) .text 3484 B -> 3564 B (+80 B) The flash goes up because the generated reset() is now inlined at the call site where the staged instance used to share one out-of-line constructor. That is the trade this class offers: RAM for a little flash, on the profile where the stack is the scarce one. Tests: the caller's object is the one written (it would stay empty if the decoder bound a copy), a bound object keeps what the next message does not carry, and a rejected message leaves what was decoded before the error. The freestanding TU decodes through it too, where a std::function would not compile. --- README.md | 3 +- src/include/sofab/sofab.hpp | 51 +++++++++++++++++++++++++ test/cpp-freestanding/freestanding.cpp | 16 ++++++++ test/cpp/test_istream.cpp | 52 ++++++++++++++++++++++++++ 4 files changed, 121 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 7e3ff56..106d597 100644 --- a/README.md +++ b/README.md @@ -112,7 +112,8 @@ Four use cases — serialize a message that fits one buffer, serialize one too l for it, deserialize a whole message, deserialize one arriving in chunks — plus the generated-code path that wraps them. The examples use the **C core**; the header-only **C++ wrapper** mirrors each through `sofab::OStreamInline` / -`sofab::IStreamObject`. +`sofab::IStreamObject` — or `sofab::IStreamInto`, which decodes into a message +you already hold instead of owning one. ### Serialize diff --git a/src/include/sofab/sofab.hpp b/src/include/sofab/sofab.hpp index d936a94..63a6418 100644 --- a/src/include/sofab/sofab.hpp +++ b/src/include/sofab/sofab.hpp @@ -3569,6 +3569,8 @@ namespace sofab friend class IStreamImpl; template friend class IStreamObject; + template + friend class IStreamInto; struct Context { @@ -3682,6 +3684,55 @@ namespace sofab } }; + /*! + * @brief Decoder that decodes into a message instance the CALLER owns. + * + * @ref IStreamObject with the instance taken out: it binds the message handed + * to the constructor instead of holding one, so fed bytes are dispatched to + * that object's @c deserialize and nothing is staged and copied. The whole + * difference is where the message lives -- same C-ABI field callback, same + * dispatch, no @c std::function -- so it costs a pointer where + * @ref IStreamObject costs a whole message, which on a heap-free target is + * the difference between one and two copies of it on the stack. + * + * The bound object is written as the bytes arrive, so a rejected input + * leaves the fields decoded before the error in it. Put it back where it + * belongs first if that matters -- generated code calls its @c reset() -- + * and note that a field the bytes do not carry keeps whatever the object + * already held (MESSAGE_SPEC §2 omits an all-default + * field), which is the same reason. + * + * The message must outlive the stream, and must not be bound to a second + * stream while this one is fed: binding is what the object's own callback + * context points at. + * + * ```cpp + * Telemetry msg; // kept across messages, reused + * msg.reset(); + * sofab::IStreamInto in{msg}; + * auto r = in.feed(bytes, len); + * ``` + * + * @tparam MessageType An @ref InputMessage type. + */ + template + class IStreamInto : public IStreamImpl + { + MessageType *data_; //!< The caller's message instance being decoded into. + + public: + /*! + * @brief Bind @p message to this stream. + * @param message Destination; must outlive the stream. + */ + explicit IStreamInto(MessageType &message) noexcept + : data_{&message} + { + data_->context_ = IStreamMessage::Context{this, data_}; + sofab_istream_init(&ctx_, MessageType::field_callback_, &data_->context_); + } + }; + /* ---------------------------------------------------------------------- */ /* Wrapper-sequence collectors and encode helpers */ /* */ diff --git a/test/cpp-freestanding/freestanding.cpp b/test/cpp-freestanding/freestanding.cpp index dc90e47..a10009d 100644 --- a/test/cpp-freestanding/freestanding.cpp +++ b/test/cpp-freestanding/freestanding.cpp @@ -203,5 +203,21 @@ extern "C" int sofab_freestanding_roundtrip(void) } #endif + // The same bytes again, decoded into an object the caller owns + // (generator#744). IStreamInto has to work here of all places: this build + // has no std::function, so a decoder that needed one would not compile. + Parent into; + sofab::IStreamInto is2{into}; + if (is2.feed(os.data(), used).code() != sofab::Error::None) + { + return -4; + } + + if (into.data_.header != 7u || into.data_.child.data_.id != 42u + || into.data_.footer != 99u || !into.data_.flag || into.data_.tag != "tag42") + { + return -5; + } + return 0; } diff --git a/test/cpp/test_istream.cpp b/test/cpp/test_istream.cpp index 4d524ae..0c23e50 100644 --- a/test/cpp/test_istream.cpp +++ b/test/cpp/test_istream.cpp @@ -389,6 +389,58 @@ TEST_CASE("IStream: object feed buffer") REQUIRE(istream->value == 3.1415f); } +TEST_CASE("IStream: IStreamInto decodes into the caller's object") +{ + SimpleObject2 object; + sofab::IStreamInto istream{object}; + + const uint8_t buffer[] = {0x08, 0x2a, 0x12, 0x20, 0x56, 0x0e, 0x49, 0x40}; + + auto result = istream.feed(buffer, sizeof(buffer)); + REQUIRE(result.code() == sofab::Error::None); + + // The caller's object, not a staged one copied over it afterwards. + REQUIRE(object->id == 42); + REQUIRE(object->value == 3.1415f); +} + +TEST_CASE("IStream: a bound object keeps what the next message does not carry") +{ + // The other half of decoding in place, and the reason generated code calls + // reset() first: MESSAGE_SPEC §2 leaves an all-default field out of the + // bytes, so a field these bytes do not carry keeps what the object held. + SimpleObject2 object; + + const uint8_t both[] = {0x08, 0x2a, 0x12, 0x20, 0x56, 0x0e, 0x49, 0x40}; + { + sofab::IStreamInto istream{object}; + REQUIRE(istream.feed(both, sizeof(both)).code() == sofab::Error::None); + } + + const uint8_t id_only[] = {0x08, 0x07}; + { + sofab::IStreamInto istream{object}; + REQUIRE(istream.feed(id_only, sizeof(id_only)).code() == sofab::Error::None); + } + + REQUIRE(object->id == 7); + REQUIRE(object->value == 3.1415f); +} + +TEST_CASE("IStream: a rejected message leaves what was decoded before the error") +{ + // The price of decoding in place, stated so it is a decision and not a + // surprise: a valid field, then an id varint that overflows. + SimpleObject2 object; + sofab::IStreamInto istream{object}; + + const uint8_t buffer[] = {0x08, 0x2a, 0xF8, 0xFF, 0xFF, 0xFF, 0x7F, 0x00}; + + auto result = istream.feed(buffer, sizeof(buffer)); + REQUIRE(result.code() == sofab::Error::InvalidMessage); + REQUIRE(object->id == 42); +} + // // Round-trip tests: serialize with the OStream API, feed the produced bytes // back through the IStream API and verify the decoded values. These exercise