Skip to content

feat(cpp): IStreamInto decodes into a message the caller owns - #197

Merged
Andste82 merged 1 commit into
mainfrom
perf/istream-into-744
Oct 14, 2026
Merged

Andste82 merged 1 commit into
mainfrom
perf/istream-into-744

Conversation

@Andste82

Copy link
Copy Markdown
Member

Prerequisite for sofa-buffers/generator#744.

What

IStreamObject<T> holds the message it decodes into. A caller that already has one — which every generated try_decode on this profile does — can therefore only decode into the stream's instance and copy it across afterwards:

sofab::IStreamObject<Example> _in;
auto _r = _in.feed(_data, _len);
if (_r.ok()) { _out = *_in; }

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.

IStreamInto<T> is IStreamObject<T> with the instance taken out: it binds the message handed to its constructor and holds a pointer where the other holds a MessageType.

Telemetry msg;                         // kept across messages, reused
msg.reset();
sofab::IStreamInto<Telemetry> in{msg};
auto r = in.feed(bytes, len);

Why not IStreamInline

That is the shape corelib-cpp uses for the same job, and it is exactly what kept this profile staging-and-copying: IStreamInline holds a std::function (hosted) — and freestanding has no std::function at all, so the generated lambda shape cannot even compile here. IStreamInto has none: same C-ABI field callback as IStreamObject, same two assignments in the constructor, same dispatch. The footprint argument no longer buys the copy anything.

The contract, stated

Decoding in place costs the strong guarantee, so the doc says it plainly:

  • a rejected input leaves the fields decoded before the error in the bound object;
  • 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

ARMv6-m, -Os, whole image linked with --gc-sections, 5-field example message (sizeof 504 B), generated code from sofa-buffers/generator#744:

stack, reset() frame .text
IStreamObject + copy 1832 B 3484 B
IStreamInto 1288 B (-544) 3564 B (+80)

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: RAM for a little flash, on the profile where the stack is the scarce one. .bss is unchanged.

Tests

  • test/cpp/test_istream.cpp — 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.
  • test/cpp-freestanding/freestanding.cpp — the same bytes decoded through IStreamInto into a caller-owned object, in the TU that has no std::function: a decoder that needed one would not compile there.

No existing API changed; IStreamObject is untouched and still what decode() (which returns by value) uses.

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.
@Andste82
Andste82 merged commit 5af4ede into main Oct 14, 2026
26 checks passed
@Andste82
Andste82 deleted the perf/istream-into-744 branch October 14, 2026 05:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant