Skip to content

feat: IStreamInto decodes into a message the caller owns - #154

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, and the twin of sofa-buffers/corelib-c-cpp#197 — the point of doing both is that the two C++ profiles stop having two shapes for the same entry point.

What

IStreamObject<T> owns the message it fills. A caller who already has one — which every generated try_decode does — cannot hand it over, so the generator worked around it with an IStreamInline whose callback forwards to the destination. That shape needs the stream inside its own callback, so the lambda cannot capture the object it is being handed to:

_out.reset();
sofab::IStreamInline *_isp = nullptr;
sofab::IStreamInline _is{[&_out, &_isp](sofab::id _id, std::size_t _size, std::size_t _count) {
    _out.deserialize(*_isp, _id, _size, _count);
}, sofab::Limits{SIZE_MAX}};
_isp = &_is;                       // ← the knot
return _is.feed(_data, _len);

IStreamInto<T> is IStreamObject<T> with the instance taken out — it routes each top-level field into the message handed to its constructor, and holds a pointer where the other holds a MessageType. Same callback, same dispatch:

_out.reset();
sofab::IStreamInto<Example> _in{_out, sofab::Limits{SIZE_MAX}};
return _in.feed(_data, _len);

Those three lines are now what both C++ profiles generate, the fixed one included (corelib-c-cpp#197).

The contract

IStreamObject's, with one difference the doc states: the destination is the caller's, so putting it back between messages is the caller's too. reset() here resets the decoder alone — generated code calls the message's own reset(), which keeps the buffers it has already paid for, the reason to hand a destination in at all. A rejected input leaves the fields decoded before the error in it, as it always has for an object decoded into.

Tests

test/test_roundtrip.cpp gains decodeIntoCallerObject(): the caller's object is the one written, what the caller cleared stays cleared, an absent field keeps the previous value (§2 — stated because it is the contract, not a bug), and a rejected message leaves what it had decoded before the error.

Mutation-checked: giving IStreamInto an instance of its own and decoding into that fails 5 of the 5 new checks, with the suite's own messages.

README gains the three-line form beside the reset() section.

No existing API changed. IStreamObject is untouched and still what a caller without a destination uses.

IStreamObject owns the message it fills, so a caller who already has one --
which every generated try_decode does -- cannot hand it over. The generator
worked around that with an IStreamInline whose callback forwards to the
destination, and that shape needs the stream inside its own callback: the
lambda cannot capture the object it is being handed to, so the generated code
reached it through a pointer patched in right after construction.

IStreamInto is IStreamObject with the instance taken out: it routes each
top-level field into the message handed to its constructor and holds a pointer
where IStreamObject holds a MessageType. Same callback, same dispatch, no knot
-- try_decode becomes three lines, and the same three in both C++ profiles
(corelib-c-cpp#197 adds the counterpart there, sofa-buffers/generator#744).

The contract is IStreamObject's, with one difference the doc states: the
destination is the caller's, so putting it back between messages is the
caller's too. reset() here resets the DECODER alone -- generated code calls the
message's own reset(), which keeps the buffers it has already paid for, the
reason to hand a destination in at all. A rejected input leaves the fields
decoded before the error in it, as it always has for an object decoded into.

Tests: the caller's object is the one written (all five checks fail if the
stream stages in one of its own -- verified by mutation), what the caller
cleared stays cleared, an absent field keeps the previous value (§2, stated
because it is the contract), and a rejected message leaves what it had decoded.
README gains the three-line form beside the reset() section.
@Andste82
Andste82 merged commit ed6b70e into main Oct 14, 2026
9 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